/
GuildManagerImpl.java
386 lines (353 loc) · 12.1 KB
/
GuildManagerImpl.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
383
384
385
386
/*
* Copyright 2015 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.managers;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.managers.GuildManager;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.utils.Checks;
import okhttp3.RequestBody;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class GuildManagerImpl extends ManagerBase<GuildManager> implements GuildManager
{
protected Guild guild;
protected String name;
protected Icon icon, splash, banner;
protected String afkChannel, systemChannel, rulesChannel, communityUpdatesChannel;
protected String description;
protected int afkTimeout;
protected int mfaLevel;
protected int notificationLevel;
protected int explicitContentLevel;
protected int verificationLevel;
protected boolean boostProgressBarEnabled;
protected Set<String> features;
public GuildManagerImpl(Guild guild)
{
super(guild.getJDA(), Route.Guilds.MODIFY_GUILD.compile(guild.getId()));
this.guild = guild;
if (isPermissionChecksEnabled())
checkPermissions();
}
@Nonnull
@Override
public Guild getGuild()
{
Guild realGuild = api.getGuildById(guild.getIdLong());
if (realGuild != null)
guild = realGuild;
return guild;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl reset(long fields)
{
super.reset(fields);
if ((fields & NAME) == NAME)
this.name = null;
if ((fields & ICON) == ICON)
this.icon = null;
if ((fields & SPLASH) == SPLASH)
this.splash = null;
if ((fields & AFK_CHANNEL) == AFK_CHANNEL)
this.afkChannel = null;
if ((fields & SYSTEM_CHANNEL) == SYSTEM_CHANNEL)
this.systemChannel = null;
if ((fields & RULES_CHANNEL) == RULES_CHANNEL)
this.rulesChannel = null;
if ((fields & COMMUNITY_UPDATES_CHANNEL) == COMMUNITY_UPDATES_CHANNEL)
this.communityUpdatesChannel = null;
if ((fields & DESCRIPTION) == DESCRIPTION)
this.description = null;
if ((fields & BANNER) == BANNER)
this.banner = null;
if ((fields & FEATURES) == FEATURES)
this.features = null;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl reset(long... fields)
{
super.reset(fields);
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl reset()
{
super.reset();
this.name = null;
this.icon = null;
this.splash = null;
this.description = null;
this.banner = null;
this.afkChannel = null;
this.systemChannel = null;
this.features = null;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setName(@Nonnull String name)
{
Checks.notEmpty(name, "Name");
Checks.notLonger(name, 100, "Name");
this.name = name;
set |= NAME;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setIcon(Icon icon)
{
this.icon = icon;
set |= ICON;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setSplash(Icon splash)
{
checkFeature("INVITE_SPLASH");
this.splash = splash;
set |= SPLASH;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setAfkChannel(VoiceChannel afkChannel)
{
Checks.check(afkChannel == null || afkChannel.getGuild().equals(getGuild()), "Channel must be from the same guild");
this.afkChannel = afkChannel == null ? null : afkChannel.getId();
set |= AFK_CHANNEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setSystemChannel(TextChannel systemChannel)
{
Checks.check(systemChannel == null || systemChannel.getGuild().equals(getGuild()), "Channel must be from the same guild");
this.systemChannel = systemChannel == null ? null : systemChannel.getId();
set |= SYSTEM_CHANNEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setRulesChannel(TextChannel rulesChannel)
{
Checks.check(rulesChannel == null || rulesChannel.getGuild().equals(getGuild()), "Channel must be from the same guild");
this.rulesChannel = rulesChannel == null ? null : rulesChannel.getId();
set |= RULES_CHANNEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setCommunityUpdatesChannel(TextChannel communityUpdatesChannel)
{
Checks.check(communityUpdatesChannel == null || communityUpdatesChannel.getGuild().equals(getGuild()), "Channel must be from the same guild");
this.communityUpdatesChannel = communityUpdatesChannel == null ? null : communityUpdatesChannel.getId();
set |= COMMUNITY_UPDATES_CHANNEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setAfkTimeout(@Nonnull Guild.Timeout timeout)
{
Checks.notNull(timeout, "Timeout");
this.afkTimeout = timeout.getSeconds();
set |= AFK_TIMEOUT;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setVerificationLevel(@Nonnull Guild.VerificationLevel level)
{
Checks.notNull(level, "Level");
Checks.check(level != Guild.VerificationLevel.UNKNOWN, "Level must not be UNKNOWN");
this.verificationLevel = level.getKey();
set |= VERIFICATION_LEVEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setDefaultNotificationLevel(@Nonnull Guild.NotificationLevel level)
{
Checks.notNull(level, "Level");
Checks.check(level != Guild.NotificationLevel.UNKNOWN, "Level must not be UNKNOWN");
this.notificationLevel = level.getKey();
set |= NOTIFICATION_LEVEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setRequiredMFALevel(@Nonnull Guild.MFALevel level)
{
Checks.notNull(level, "Level");
Checks.check(level != Guild.MFALevel.UNKNOWN, "Level must not be UNKNOWN");
this.mfaLevel = level.getKey();
set |= MFA_LEVEL;
return this;
}
@Nonnull
@Override
@CheckReturnValue
public GuildManagerImpl setExplicitContentLevel(@Nonnull Guild.ExplicitContentLevel level)
{
Checks.notNull(level, "Level");
Checks.check(level != Guild.ExplicitContentLevel.UNKNOWN, "Level must not be UNKNOWN");
this.explicitContentLevel = level.getKey();
set |= EXPLICIT_CONTENT_LEVEL;
return this;
}
@Nonnull
@Override
public GuildManager setBanner(@Nullable Icon banner)
{
checkFeature("BANNER");
this.banner = banner;
set |= BANNER;
return this;
}
@Nonnull
@Override
public GuildManager setDescription(@Nullable String description)
{
checkFeature("VERIFIED");
this.description = description;
set |= DESCRIPTION;
return this;
}
@Nonnull
@Override
public GuildManager setBoostProgressBarEnabled(boolean enabled)
{
this.boostProgressBarEnabled = enabled;
set |= BOOST_PROGRESS_BAR_ENABLED;
return this;
}
@Nonnull
@Override
public GuildManager setFeatures(@Nonnull Collection<String> features)
{
Checks.noneNull(features, "Features");
this.features = features.stream()
.map(String::toUpperCase)
.collect(Collectors.toSet());
set |= FEATURES;
return this;
}
@Nonnull
@Override
public GuildManager addFeatures(@Nonnull Collection<String> features)
{
return updateFeatures(features, feature -> this.features.add(feature));
}
@Nonnull
@Override
public GuildManager removeFeatures(@Nonnull Collection<String> features)
{
return updateFeatures(features, feature -> this.features.remove(feature));
}
private GuildManager updateFeatures(Collection<String> changed, Consumer<String> op)
{
Checks.noneNull(changed, "Features");
if (this.features == null)
this.features = new HashSet<>(getGuild().getFeatures());
changed.stream()
.map(String::toUpperCase)
.forEach(op);
set |= FEATURES;
return this;
}
@Override
protected RequestBody finalizeData()
{
DataObject body = DataObject.empty().put("name", getGuild().getName());
if (shouldUpdate(NAME))
body.put("name", name);
if (shouldUpdate(AFK_TIMEOUT))
body.put("afk_timeout", afkTimeout);
if (shouldUpdate(ICON))
body.put("icon", icon == null ? null : icon.getEncoding());
if (shouldUpdate(SPLASH))
body.put("splash", splash == null ? null : splash.getEncoding());
if (shouldUpdate(AFK_CHANNEL))
body.put("afk_channel_id", afkChannel);
if (shouldUpdate(SYSTEM_CHANNEL))
body.put("system_channel_id", systemChannel);
if (shouldUpdate(RULES_CHANNEL))
body.put("rules_channel_id", rulesChannel);
if (shouldUpdate(COMMUNITY_UPDATES_CHANNEL))
body.put("public_updates_channel_id", communityUpdatesChannel);
if (shouldUpdate(VERIFICATION_LEVEL))
body.put("verification_level", verificationLevel);
if (shouldUpdate(NOTIFICATION_LEVEL))
body.put("default_message_notifications", notificationLevel);
if (shouldUpdate(MFA_LEVEL))
body.put("mfa_level", mfaLevel);
if (shouldUpdate(EXPLICIT_CONTENT_LEVEL))
body.put("explicit_content_filter", explicitContentLevel);
if (shouldUpdate(BANNER))
body.put("banner", banner == null ? null : banner.getEncoding());
if (shouldUpdate(DESCRIPTION))
body.put("description", description);
if (shouldUpdate(BOOST_PROGRESS_BAR_ENABLED))
body.put("premium_progress_bar_enabled", boostProgressBarEnabled);
if (shouldUpdate(FEATURES))
body.put("features", features);
reset(); //now that we've built our JSON object, reset the manager back to the non-modified state
return getRequestBody(body);
}
@Override
protected boolean checkPermissions()
{
if (!getGuild().getSelfMember().hasPermission(Permission.MANAGE_SERVER))
throw new InsufficientPermissionException(getGuild(), Permission.MANAGE_SERVER);
return super.checkPermissions();
}
private void checkFeature(String feature)
{
if (!getGuild().getFeatures().contains(feature))
throw new IllegalStateException("This guild doesn't have the " + feature + " feature enabled");
}
}