/
ChannelActionImpl.java
382 lines (346 loc) · 13.3 KB
/
ChannelActionImpl.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright 2015-2020 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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.internal.requests.restaction;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.api.requests.restaction.ChannelAction;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.utils.Checks;
import okhttp3.RequestBody;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
public class ChannelActionImpl<T extends GuildChannel> extends AuditableRestActionImpl<T> implements ChannelAction<T>
{
protected final TLongObjectMap<PermOverrideData> overrides = new TLongObjectHashMap<>();
protected final Guild guild;
protected final ChannelType type;
protected final Class<T> clazz;
protected String name;
protected Category parent;
protected Integer position;
// --text only--
protected String topic = null;
protected Boolean nsfw = null;
protected Integer slowmode = null;
protected Boolean news = null;
// --voice only--
protected Integer bitrate = null;
protected Integer userlimit = null;
public ChannelActionImpl(Class<T> clazz, String name, Guild guild, ChannelType type)
{
super(guild.getJDA(), Route.Guilds.CREATE_CHANNEL.compile(guild.getId()));
this.clazz = clazz;
this.guild = guild;
this.type = type;
this.name = name;
}
@Nonnull
@Override
public ChannelActionImpl<T> setCheck(BooleanSupplier checks)
{
return (ChannelActionImpl<T>) super.setCheck(checks);
}
@Nonnull
@Override
public ChannelActionImpl<T> timeout(long timeout, @Nonnull TimeUnit unit)
{
return (ChannelActionImpl<T>) super.timeout(timeout, unit);
}
@Nonnull
@Override
public ChannelActionImpl<T> deadline(long timestamp)
{
return (ChannelActionImpl<T>) super.deadline(timestamp);
}
@Nonnull
@Override
public Guild getGuild()
{
return guild;
}
@Nonnull
@Override
public ChannelType getType()
{
return type;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setName(@Nonnull String name)
{
Checks.notNull(name, "Channel name");
if (name.length() < 1 || name.length() > 100)
throw new IllegalArgumentException("Provided channel name must be 1 to 100 characters in length");
this.name = name;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setParent(Category category)
{
Checks.check(category == null || category.getGuild().equals(guild), "Category is not from same guild!");
this.parent = category;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setPosition(Integer position)
{
Checks.check(position == null || position >= 0, "Position must be >= 0!");
this.position = position;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setTopic(String topic)
{
if (type != ChannelType.TEXT)
throw new UnsupportedOperationException("Can only set the topic for a TextChannel!");
if (topic != null && topic.length() > 1024)
throw new IllegalArgumentException("Channel Topic must not be greater than 1024 in length!");
this.topic = topic;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setNSFW(boolean nsfw)
{
if (type != ChannelType.TEXT)
throw new UnsupportedOperationException("Can only set nsfw for a TextChannel!");
this.nsfw = nsfw;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setSlowmode(int slowmode)
{
if (type != ChannelType.TEXT)
throw new UnsupportedOperationException("Can only set slowmode on text channels");
Checks.check(slowmode <= TextChannel.MAX_SLOWMODE && slowmode >= 0, "Slowmode must be between 0 and %d (seconds)!", TextChannel.MAX_SLOWMODE);
this.slowmode = slowmode;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setNews(boolean news)
{
if (type != ChannelType.TEXT)
throw new UnsupportedOperationException("Can only set news for a TextChannel!");
if (news && !getGuild().getFeatures().contains("NEWS"))
throw new IllegalStateException("Can only set channel as news for guilds with NEWS feature");
this.news = news;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> addMemberPermissionOverride(long userId, long allow, long deny)
{
return addOverride(userId, PermOverrideData.MEMBER_TYPE, allow, deny);
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> addRolePermissionOverride(long roleId, long allow, long deny)
{
return addOverride(roleId, PermOverrideData.ROLE_TYPE, allow, deny);
}
@Nonnull
@Override
public ChannelAction<T> removePermissionOverride(long id)
{
overrides.remove(id);
return this;
}
@Nonnull
@Override
public ChannelAction<T> clearPermissionOverrides()
{
overrides.clear();
return this;
}
@Nonnull
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public ChannelAction<T> syncPermissionOverrides()
{
if (parent == null)
throw new IllegalStateException("Cannot sync overrides without parent category! Use setParent(category) first!");
clearPermissionOverrides();
Member selfMember = getGuild().getSelfMember();
boolean canSetRoles = selfMember.hasPermission(parent, Permission.MANAGE_ROLES);
//You can only set MANAGE_ROLES if you have ADMINISTRATOR or MANAGE_PERMISSIONS as an override on the channel
// That is why we explicitly exclude it here!
// This is by far the most complex and weird permission logic in the entire API...
long botPerms;
if (parent != null)
botPerms = Permission.getRaw(selfMember.getPermissions(parent)) & ~Permission.MANAGE_PERMISSIONS.getRawValue();
else
botPerms = Permission.getRaw(selfMember.getPermissions()) & ~Permission.MANAGE_PERMISSIONS.getRawValue();
parent.getRolePermissionOverrides().forEach(override -> {
long allow = override.getAllowedRaw();
long deny = override.getDeniedRaw();
if (!canSetRoles)
{
allow &= botPerms;
deny &= botPerms;
}
addRolePermissionOverride(override.getIdLong(), allow, deny);
});
parent.getMemberPermissionOverrides().forEach(override -> {
long allow = override.getAllowedRaw();
long deny = override.getDeniedRaw();
if (!canSetRoles)
{
allow &= botPerms;
deny &= botPerms;
}
addMemberPermissionOverride(override.getIdLong(), allow, deny);
});
return this;
}
private ChannelActionImpl<T> addOverride(long targetId, int type, long allow, long deny)
{
Checks.notNegative(allow, "Granted permissions value");
Checks.notNegative(deny, "Denied permissions value");
Checks.check(allow <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
Checks.check(deny <= Permission.ALL_PERMISSIONS, "Specified deny value may not be greater than a full permission set");
Member selfMember = getGuild().getSelfMember();
boolean canSetRoles = selfMember.hasPermission(Permission.ADMINISTRATOR);
if (!canSetRoles && parent != null) // You can also set MANAGE_ROLES if you have it on the category (apparently?)
canSetRoles = selfMember.hasPermission(parent, Permission.MANAGE_ROLES);
if (!canSetRoles)
{
// Prevent permission escalation
long botPerms;
if (parent != null)
botPerms = Permission.getRaw(selfMember.getPermissions(parent));
else
botPerms = Permission.getRaw(selfMember.getPermissions());
//You can only set MANAGE_ROLES if you have ADMINISTRATOR or MANAGE_PERMISSIONS as an override on the channel
// That is why we explicitly exclude it here!
// This is by far the most complex and weird permission logic in the entire API...
botPerms &= ~Permission.MANAGE_PERMISSIONS.getRawValue();
EnumSet<Permission> missingPerms = Permission.getPermissions((allow | deny) & ~botPerms);
if (!missingPerms.isEmpty())
throw new InsufficientPermissionException(guild, Permission.MANAGE_PERMISSIONS, "You must have Permission.MANAGE_PERMISSIONS on the channel explicitly in order to set permissions you don't already have!");
}
overrides.put(targetId, new PermOverrideData(type, targetId, allow, deny));
return this;
}
// --voice only--
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setBitrate(Integer bitrate)
{
if (type != ChannelType.VOICE)
throw new UnsupportedOperationException("Can only set the bitrate for a VoiceChannel!");
if (bitrate != null)
{
int maxBitrate = getGuild().getMaxBitrate();
if (bitrate < 8000)
throw new IllegalArgumentException("Bitrate must be greater than 8000.");
else if (bitrate > maxBitrate)
throw new IllegalArgumentException("Bitrate must be less than " + maxBitrate);
}
this.bitrate = bitrate;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> setUserlimit(Integer userlimit)
{
if (type != ChannelType.VOICE)
throw new UnsupportedOperationException("Can only set the userlimit for a VoiceChannel!");
if (userlimit != null && (userlimit < 0 || userlimit > 99))
throw new IllegalArgumentException("Userlimit must be between 0-99!");
this.userlimit = userlimit;
return this;
}
@Override
protected RequestBody finalizeData()
{
DataObject object = DataObject.empty();
object.put("name", name);
object.put("type", type.getId());
object.put("permission_overwrites", DataArray.fromCollection(overrides.valueCollection()));
if (position != null)
object.put("position", position);
switch (type)
{
case VOICE:
if (bitrate != null)
object.put("bitrate", bitrate);
if (userlimit != null)
object.put("user_limit", userlimit);
break;
case TEXT:
if (topic != null && !topic.isEmpty())
object.put("topic", topic);
if (nsfw != null)
object.put("nsfw", nsfw);
if (slowmode != null)
object.put("rate_limit_per_user", slowmode);
if (news != null)
object.put("type", news ? 5 : 0);
}
if (type != ChannelType.CATEGORY && parent != null)
object.put("parent_id", parent.getId());
return getRequestBody(object);
}
@Override
protected void handleSuccess(Response response, Request<T> request)
{
EntityBuilder builder = api.getEntityBuilder();
GuildChannel channel;
switch (type)
{
case VOICE:
channel = builder.createVoiceChannel(response.getObject(), guild.getIdLong());
break;
case TEXT:
channel = builder.createTextChannel(response.getObject(), guild.getIdLong());
break;
case CATEGORY:
channel = builder.createCategory(response.getObject(), guild.getIdLong());
break;
default:
request.onFailure(new IllegalStateException("Created channel of unknown type!"));
return;
}
request.onSuccess(clazz.cast(channel));
}
}