-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
WebhookManager.java
274 lines (255 loc) · 8.89 KB
/
WebhookManager.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
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.Permission;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Icon;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.Webhook;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.core.managers.impl.ManagerBase;
import net.dv8tion.jda.core.requests.Route;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.cache.UpstreamReference;
import okhttp3.RequestBody;
import org.json.JSONObject;
import javax.annotation.CheckReturnValue;
/**
* Manager providing functionality to update one or more fields for a {@link net.dv8tion.jda.core.entities.Webhook Webhook}.
*
* <p><b>Example</b>
* <pre>{@code
* manager.setName("GitHub Webhook")
* .setChannel(channel)
* .queue();
* manager.reset(WebhookManager.NAME | WebhookManager.AVATAR)
* .setName("Meme Feed")
* .setAvatar(null)
* .queue();
* }</pre>
*
* @see net.dv8tion.jda.core.entities.Webhook#getManager()
*/
public class WebhookManager extends ManagerBase
{
/** Used to reset the name field */
public static final long NAME = 0x1;
/** Used to reset the channel field */
public static final long CHANNEL = 0x2;
/** Used to reset the avatar field */
public static final long AVATAR = 0x4;
protected final UpstreamReference<Webhook> webhook;
protected String name;
protected String channel;
protected Icon avatar;
/**
* Creates a new WebhookManager instance
*
* @param webhook
* The target {@link net.dv8tion.jda.core.entities.Webhook Webhook} to modify
*/
public WebhookManager(Webhook webhook)
{
super(webhook.getJDA(), Route.Webhooks.MODIFY_WEBHOOK.compile(webhook.getId()));
this.webhook = new UpstreamReference<>(webhook);
if (isPermissionChecksEnabled())
checkPermissions();
}
/**
* The {@link net.dv8tion.jda.core.entities.Guild Guild} this Manager's
* {@link net.dv8tion.jda.core.entities.Webhook Webhook} is in.
* <br>This is logically the same as calling {@code getWebhook().getGuild()}
*
* @return The parent {@link net.dv8tion.jda.core.entities.Guild Guild}
*/
public Guild getGuild()
{
return getWebhook().getGuild();
}
/**
* The {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} this Manager's
* {@link net.dv8tion.jda.core.entities.Webhook Webhook} is in.
* <br>This is logically the same as calling {@code getWebhook().getChannel()}
*
* @return The parent {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
*/
public TextChannel getChannel()
{
return getWebhook().getChannel();
}
/**
* The target {@link net.dv8tion.jda.core.entities.Webhook Webhook}
* that will be modified by this manager
*
* @return The target {@link net.dv8tion.jda.core.entities.Webhook Webhook}
*/
public Webhook getWebhook()
{
return webhook.get();
}
/**
* Resets the fields specified by the provided bit-flag pattern.
* You can specify a combination by using a bitwise OR concat of the flag constants.
* <br>Example: {@code manager.reset(WebhookManager.CHANNEL | WebhookManager.NAME);}
*
* <p><b>Flag Constants:</b>
* <ul>
* <li>{@link #NAME}</li>
* <li>{@link #AVATAR}</li>
* <li>{@link #CHANNEL}</li>
* </ul>
*
* @param fields
* Integer value containing the flags to reset.
*
* @return WebhookManager for chaining convenience
*/
@Override
@CheckReturnValue
public WebhookManager reset(long fields)
{
super.reset(fields);
if ((fields & NAME) == NAME)
this.name = null;
if ((fields & CHANNEL) == CHANNEL)
this.channel = null;
if ((fields & AVATAR) == AVATAR)
this.avatar = null;
return this;
}
/**
* Resets the fields specified by the provided bit-flag patterns.
* You can specify a combination by using a bitwise OR concat of the flag constants.
* <br>Example: {@code manager.reset(WebhookManager.CHANNEL, WebhookManager.NAME);}
*
* <p><b>Flag Constants:</b>
* <ul>
* <li>{@link #NAME}</li>
* <li>{@link #AVATAR}</li>
* <li>{@link #CHANNEL}</li>
* </ul>
*
* @param fields
* Integer values containing the flags to reset.
*
* @return WebhookManager for chaining convenience
*/
@Override
@CheckReturnValue
public WebhookManager reset(long... fields)
{
super.reset(fields);
return this;
}
/**
* Resets all fields for this manager.
*
* @return WebhookManager for chaining convenience
*/
@Override
@CheckReturnValue
public WebhookManager reset()
{
super.reset();
this.name = null;
this.channel = null;
this.avatar = null;
return this;
}
/**
* Sets the <b><u>default name</u></b> of the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}.
*
* <p>A webhook name <b>must not</b> be {@code null} or blank!
*
* @param name
* The new default name for the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}
*
* @throws IllegalArgumentException
* If the provided name is {@code null} or blank
*
* @return WebhookManager for chaining convenience
*/
@CheckReturnValue
public WebhookManager setName(String name)
{
Checks.notBlank(name, "Name");
this.name = name;
set |= NAME;
return this;
}
/**
* Sets the <b><u>default avatar</u></b> of the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}.
*
* @param icon
* The new default avatar {@link net.dv8tion.jda.core.entities.Icon Icon}
* for the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}
* or {@code null} to reset
*
* @return WebhookManager for chaining convenience
*/
@CheckReturnValue
public WebhookManager setAvatar(Icon icon)
{
this.avatar = icon;
set |= AVATAR;
return this;
}
/**
* Sets the {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} of the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}.
*
* <p>A webhook channel <b>must not</b> be {@code null} and <b>must</b> be in the same {@link net.dv8tion.jda.core.entities.Guild Guild}!
*
* @param channel
* The new {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
* for the selected {@link net.dv8tion.jda.core.entities.Webhook Webhook}
*
* @throws net.dv8tion.jda.core.exceptions.InsufficientPermissionException
* If the currently logged in account does not have the Permission {@link net.dv8tion.jda.core.Permission#MANAGE_WEBHOOKS MANAGE_WEBHOOKS}
* in the specified TextChannel
* @throws IllegalArgumentException
* If the provided channel is {@code null} or from a different Guild
*
* @return WebhookManager for chaining convenience
*/
@CheckReturnValue
public WebhookManager setChannel(TextChannel channel)
{
Checks.notNull(channel, "Channel");
Checks.check(channel.getGuild().equals(getGuild()), "Channel is not from the same guild");
this.channel = channel.getId();
set |= CHANNEL;
return this;
}
@Override
protected RequestBody finalizeData()
{
JSONObject data = new JSONObject();
if (shouldUpdate(NAME))
data.put("name", name);
if (shouldUpdate(CHANNEL))
data.put("channel_id", channel);
if (shouldUpdate(AVATAR))
data.put("avatar", avatar == null ? JSONObject.NULL : avatar.getEncoding());
return getRequestBody(data);
}
@Override
protected boolean checkPermissions()
{
if (!getGuild().getSelfMember().hasPermission(getChannel(), Permission.MANAGE_WEBHOOKS))
throw new InsufficientPermissionException(Permission.MANAGE_WEBHOOKS);
return super.checkPermissions();
}
}