-
-
Notifications
You must be signed in to change notification settings - Fork 719
/
RoleImpl.java
334 lines (286 loc) · 8.28 KB
/
RoleImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright 2015-2016 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.entities.impl;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Channel;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.managers.RoleManager;
import net.dv8tion.jda.core.managers.RoleManagerUpdatable;
import net.dv8tion.jda.core.requests.Request;
import net.dv8tion.jda.core.requests.Response;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.Route;
import net.dv8tion.jda.core.utils.PermissionUtil;
import org.apache.http.util.Args;
import java.awt.*;
import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class RoleImpl implements Role
{
private final String id;
private final Guild guild;
private volatile RoleManager manager;
private volatile RoleManagerUpdatable managerUpdatable;
private Object mngLock = new Object();
private String name;
private Color color;
private boolean managed;
private boolean hoisted;
private boolean mentionable;
private long rawPermissions;
private int rawPosition;
public RoleImpl(String id, Guild guild)
{
this.id = id;
this.guild = guild;
}
@Override
public int getPosition()
{
if (this == guild.getPublicRole())
return -1;
//Subtract 1 to get into 0-index, and 1 to disregard the everyone role.
int i = guild.getRoles().size() - 2;
for (Role r : guild.getRoles())
{
if (r == this)
return i;
i--;
}
throw new RuntimeException("Somehow when determining position we never found the role in the Guild's roles? wtf?");
}
@Override
public int getPositionRaw()
{
return rawPosition;
}
@Override
public String getName()
{
return name;
}
@Override
public boolean isManaged()
{
return managed;
}
@Override
public boolean isHoisted()
{
return hoisted;
}
@Override
public boolean isMentionable()
{
return mentionable;
}
@Override
public long getPermissionsRaw()
{
return rawPermissions;
}
@Override
public List<Permission> getPermissions()
{
return Collections.unmodifiableList(
Permission.getPermissions(rawPermissions));
}
@Override
public List<Permission> getPermissions(Channel channel)
{
return null;
}
@Override
public Color getColor()
{
return color;
}
@Override
public boolean hasPermission(Permission... permissions)
{
for (Permission perm : permissions)
{
if (((rawPermissions >> perm.getOffset()) & 1) != 1)
return false;
}
return true;
}
@Override
public boolean hasPermission(Collection<Permission> permissions)
{
Args.notNull(permissions, "Permission Collection");
return hasPermission(permissions.toArray(new Permission[permissions.size()]));
}
//TODO: Implement this >.>
@Override
public boolean hasPermission(Channel channel, Permission... permissions)
{
return false;
}
@Override
public boolean hasPermission(Channel channel, Collection<Permission> permissions)
{
Args.notNull(permissions, "Permission Collection");
return hasPermission(channel, permissions.toArray(new Permission[permissions.size()]));
}
@Override
public Guild getGuild()
{
return guild;
}
@Override
public RoleManager getManager()
{
RoleManager mng = manager;
if (mng == null)
{
synchronized (mngLock)
{
mng = manager;
if (mng == null)
mng = manager = new RoleManager(this);
}
}
return mng;
}
@Override
public RoleManagerUpdatable getManagerUpdatable()
{
RoleManagerUpdatable mng = managerUpdatable;
if (mng == null)
{
synchronized (mngLock)
{
mng = managerUpdatable;
if (mng == null)
mng = managerUpdatable = new RoleManagerUpdatable(this);
}
}
return mng;
}
@Override
public RestAction<Void> delete()
{
if (!PermissionUtil.checkPermission(getGuild(), getGuild().getSelfMember(), Permission.MANAGE_PERMISSIONS))
throw new PermissionException(Permission.MANAGE_PERMISSIONS);
if(!PermissionUtil.canInteract(getGuild().getSelfMember(), this))
throw new PermissionException("Can't delete role >= highest self-role");
if (managed)
throw new UnsupportedOperationException("Cannot delete a Role that is managed. ");
Route.CompiledRoute route = Route.Roles.DELETE_ROLE.compile(guild.getId(), id);
return new RestAction<Void>(getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
@Override
public JDA getJDA()
{
return guild.getJDA();
}
@Override
public String getAsMention()
{
return "<@&" + getId() + '>';
}
@Override
public String getId()
{
return id;
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof Role))
return false;
Role oRole = (Role) o;
return this == oRole || this.getId().equals(oRole.getId());
}
@Override
public int hashCode()
{
return getId().hashCode();
}
@Override
public String toString()
{
return "R:" + getName() + '(' + getId() + ')';
}
@Override
public int compareTo(Role r)
{
if (this == r)
return 0;
if (this.getGuild() != r.getGuild())
throw new IllegalArgumentException("Cannot compare roles that aren't from the same guild!");
if (this.getPositionRaw() != r.getPositionRaw())
return this.getPositionRaw() - r.getPositionRaw();
OffsetDateTime thisTime = this.getCreationTime();
OffsetDateTime rTime = r.getCreationTime();
//We compare the provided role's time to this's time instead of the reverse as one would expect due to how
// discord deals with hierarchy. The more recent a role was created, the lower its hierarchy ranking when
// it shares the same position as another role.
return rTime.compareTo(thisTime);
}
// -- Setters --
public RoleImpl setName(String name)
{
this.name = name;
return this;
}
public RoleImpl setColor(Color color)
{
this.color = color;
return this;
}
public RoleImpl setManaged(boolean managed)
{
this.managed = managed;
return this;
}
public RoleImpl setHoisted(boolean hoisted)
{
this.hoisted = hoisted;
return this;
}
public RoleImpl setMentionable(boolean mentionable)
{
this.mentionable = mentionable;
return this;
}
public RoleImpl setRawPermissions(long rawPermissions)
{
this.rawPermissions = rawPermissions;
return this;
}
public RoleImpl setRawPosition(int rawPosition)
{
this.rawPosition = rawPosition;
return this;
}
}