-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
GuildManagerUpdatable.java
348 lines (302 loc) · 10.9 KB
/
GuildManagerUpdatable.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
/*
* 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.managers;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.Region;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Icon;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.exceptions.GuildUnavailableException;
import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.managers.fields.GuildField;
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.json.JSONObject;
public class GuildManagerUpdatable
{
protected final Guild guild;
protected GuildField<String> name;
protected GuildField<Guild.Timeout> timeout;
protected GuildField<Icon> icon;
protected GuildField<Icon> splash;
protected GuildField<Region> region;
protected GuildField<VoiceChannel> afkChannel;
protected GuildField<Guild.VerificationLevel> verificationLevel;
protected GuildField<Guild.NotificationLevel> defaultNotificationLevel;
protected GuildField<Guild.MFALevel> mfaLevel;
public GuildManagerUpdatable(Guild guild)
{
this.guild = guild;
setupFields();
}
public JDA getJDA()
{
return guild.getJDA();
}
public Guild getGuild()
{
return guild;
}
public GuildField<String> getNameField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return name;
}
public GuildField<Region> getRegionField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return region;
}
public GuildField<Icon> getIconField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return icon;
}
public GuildField<Icon> getSplashField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return splash;
}
public GuildField<VoiceChannel> getAfkChannelField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return afkChannel;
}
/**
* Valid timeouts are: 60, 300, 900, 1800, 3600.
*/
public GuildField<Guild.Timeout> getAfkTimeoutField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return timeout;
}
public GuildField<Guild.VerificationLevel> getVerificationLevelField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return verificationLevel;
}
public GuildField<Guild.NotificationLevel> getDefaultNotificationLevelField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return defaultNotificationLevel;
}
public GuildField<Guild.MFALevel> getRequiredMFALevelField()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
return mfaLevel;
}
/**
* Resets all queued updates. So the next call to {@link #update()} will change nothing.
*/
public void reset()
{
this.name.reset();
this.region.reset();
this.timeout.reset();
this.icon.reset();
this.splash.reset();
this.afkChannel.reset();
this.verificationLevel.reset();
this.defaultNotificationLevel.reset();
this.mfaLevel.reset();
}
/**
* This method will apply all accumulated changes received by setters
*
* @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException
* if the guild is temporarily unavailable
*/
public RestAction<Void> update()
{
checkAvailable();
checkPermission(Permission.MANAGE_SERVER);
if (!needToUpdate())
return new RestAction.EmptyRestAction<>(null);
JSONObject body = new JSONObject().put("name", guild.getName());
if (name.shouldUpdate())
body.put("name", name.getValue());
if (region.shouldUpdate())
body.put("region", region.getValue().getKey());
if (timeout.shouldUpdate())
body.put("afk_timeout", timeout.getValue().getSeconds());
if (icon.shouldUpdate())
body.put("icon", icon.getValue() == null ? JSONObject.NULL : icon.getValue().getEncoding());
if (splash.shouldUpdate())
body.put("splash", splash.getValue() == null ? JSONObject.NULL : splash.getValue().getEncoding());
if (afkChannel.shouldUpdate())
body.put("afk_channel_id", afkChannel.getValue() == null ? JSONObject.NULL : afkChannel.getValue().getId());
if (verificationLevel.shouldUpdate())
body.put("verification_level", verificationLevel.getValue().getKey());
if (defaultNotificationLevel.shouldUpdate())
body.put("default_notification_level", defaultNotificationLevel.getValue().getKey());
if (mfaLevel.shouldUpdate())
body.put("mfa_level", mfaLevel.getValue().getKey());
reset(); //now that we've built our JSON object, reset the manager back to the non-modified state
Route.CompiledRoute route = Route.Guilds.MODIFY_GUILD.compile(guild.getId());
return new RestAction<Void>(guild.getJDA(), route, body)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
protected boolean needToUpdate()
{
return name.shouldUpdate()
|| region.shouldUpdate()
|| timeout.shouldUpdate()
|| icon.shouldUpdate()
|| splash.shouldUpdate()
|| afkChannel.shouldUpdate()
|| verificationLevel.shouldUpdate()
|| defaultNotificationLevel.shouldUpdate()
|| mfaLevel.shouldUpdate();
}
protected void checkAvailable()
{
if (!guild.isAvailable())
throw new GuildUnavailableException();
}
protected void checkPermission(Permission perm)
{
if (!PermissionUtil.checkPermission(guild, guild.getSelfMember(), perm))
throw new PermissionException(perm);
}
protected void setupFields()
{
this.name = new GuildField<String>(this, guild::getName)
{
@Override
public void checkValue(String value)
{
checkNull(value, "guild name");
if (value.length() < 2 || value.length() > 100)
throw new IllegalArgumentException("Provided guild name must be 2 to 100 characters in length");
}
};
this.timeout = new GuildField<Guild.Timeout>(this, guild::getAfkTimeout)
{
@Override
public void checkValue(Guild.Timeout value)
{
checkNull(value, "Timeout");
}
};
this.icon = new GuildField<Icon>(this, null)
{
@Override
public void checkValue(Icon value)
{
checkNull(value, "guild icon");
}
@Override
public Icon getOriginalValue()
{
throw new UnsupportedOperationException("Cannot easily provide the original Icon. Use Guild#getIconUrl() and download it yourself.");
}
@Override
public boolean shouldUpdate()
{
return isSet();
}
};
this.splash = new GuildField<Icon>(this, null)
{
@Override
public void checkValue(Icon value)
{
checkNull(value, "guild splash");
}
@Override
public Icon getOriginalValue()
{
throw new UnsupportedOperationException("Cannot easily provide the original Splash. Use Guild#getSplashUrl() and download it yourself.");
}
@Override
public boolean shouldUpdate()
{
return isSet();
}
};
this.region = new GuildField<Region>(this, guild::getRegion)
{
@Override
public void checkValue(Region value)
{
checkNull(value, "Region");
if (value == Region.UNKNOWN)
throw new IllegalArgumentException("Cannot set Guild Region to UNKNOWN!");
}
};
this.afkChannel = new GuildField<VoiceChannel>(this, guild::getAfkChannel)
{
@Override
public void checkValue(VoiceChannel value)
{
if (value != null && !guild.equals(value.getGuild()))
throw new IllegalArgumentException("Provided AFK Channel is not from this Guild!");
}
};
this.verificationLevel = new GuildField<Guild.VerificationLevel>(this, guild::getVerificationLevel)
{
@Override
public void checkValue(Guild.VerificationLevel value)
{
checkNull(value, "VerificationLevel");
if (value == Guild.VerificationLevel.UNKNOWN)
throw new IllegalArgumentException("Cannot set Guild VerificationLevel to UNKNOWN");
}
};
this.defaultNotificationLevel = new GuildField<Guild.NotificationLevel>(this, guild::getDefaultNotificationLevel)
{
@Override
public void checkValue(Guild.NotificationLevel value)
{
checkNull(value, "NotificationLevel");
if (value == Guild.NotificationLevel.UNKNOWN)
throw new IllegalArgumentException("Cannot set NotificationLevel to UNKNOWN");
}
};
this.mfaLevel = new GuildField<Guild.MFALevel>(this, guild::getRequiredMFALevel)
{
@Override
public void checkValue(Guild.MFALevel value)
{
checkNull(value, "MFALevel");
if (value == Guild.MFALevel.UNKNOWN)
throw new IllegalArgumentException("Cannot set MFALevel to UNKNOWN");
}
};
}
}