/
MessageCreateRequest.java
416 lines (396 loc) · 15.5 KB
/
MessageCreateRequest.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* 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.api.utils.messages;
import net.dv8tion.jda.api.entities.EmbedType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.FileUpload;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* Specialized abstraction of setters and accumulators for creating messages throughout the API.
*
* @param <R>
* The return type for method chaining convenience
*
* @see MessageCreateBuilder
* @see MessageCreateData
* @see net.dv8tion.jda.api.requests.restaction.MessageCreateAction MessageCreateAction
*/
public interface MessageCreateRequest<R extends MessageCreateRequest<R>> extends MessageRequest<R>
{
/**
* Appends the content to the currently set content of this request.
* <br>Use {@link #setContent(String)} instead, to replace the content entirely.
*
* <p><b>Example</b><br>
* Sending a message with the content {@code "Hello World!"}:
* <pre>{@code
* channel.sendMessage("Hello ").addContent("World!").queue();
* }</pre>
*
* @param content
* The content to append
*
* @throws IllegalArgumentException
* If the provided content is {@code null} or the accumulated content is longer than {@value Message#MAX_CONTENT_LENGTH} characters
*
* @return The same instance for chaining
*/
@Nonnull
R addContent(@Nonnull String content);
/**
* Appends the provided {@link MessageEmbed MessageEmbeds} to the request.
* <br>Use {@link #setEmbeds(Collection)} instead, to replace the embeds entirely.
*
* <p><b>Example</b><br>
* Sending a message with multiple embeds:
* <pre>{@code
* channel.sendMessageEmbeds(embed1).addEmbeds(embed2).queue();
* }</pre>
*
* @param embeds
* The embeds to add
*
* @throws IllegalArgumentException
* If null is provided or the accumulated embed list is longer than {@value Message#MAX_EMBED_COUNT}
*
* @return The same instance for chaining
*/
@Nonnull
R addEmbeds(@Nonnull Collection<? extends MessageEmbed> embeds);
/**
* Appends the provided {@link MessageEmbed MessageEmbeds} to the request.
* <br>Use {@link #setEmbeds(Collection)} instead, to replace the embeds entirely.
*
* <p><b>Example</b><br>
* Sending a message with multiple embeds:
* <pre>{@code
* channel.sendMessageEmbeds(embed1).addEmbeds(embed2).queue();
* }</pre>
*
* @param embeds
* The embeds to add
*
* @throws IllegalArgumentException
* If null is provided or the accumulated embed list is longer than {@value Message#MAX_EMBED_COUNT}
*
* @return The same instance for chaining
*/
@Nonnull
default R addEmbeds(@Nonnull MessageEmbed... embeds)
{
return addEmbeds(Arrays.asList(embeds));
}
/**
* Appends the provided {@link LayoutComponent LayoutComponents} to the request.
* <br>Use {@link #setComponents(Collection)} instead, to replace the components entirely.
*
* <p><b>Example</b><br>
* Sending a message with multiple action rows:
* <pre>{@code
* channel.sendMessageComponents(ActionRow.of(selectMenu))
* .addComponents(ActionRow.of(button1, button2))
* .queue();
* }</pre>
*
* @param components
* The layout components to add
*
* @throws IllegalArgumentException
* <ul>
* <li>If {@code null} is provided</li>
* <li>If any of the components is not {@link LayoutComponent#isMessageCompatible() message compatible}</li>
* <li>If the accumulated list of components is longer than {@value Message#MAX_COMPONENT_COUNT}</li>
* </ul>
*
* @return The same instance for chaining
*
* @see ActionRow
*/
@Nonnull
R addComponents(@Nonnull Collection<? extends LayoutComponent> components);
/**
* Appends the provided {@link LayoutComponent LayoutComponents} to the request.
* <br>Use {@link #setComponents(Collection)} instead, to replace the components entirely.
*
* <p><b>Example</b><br>
* Sending a message with multiple action rows:
* <pre>{@code
* channel.sendMessageComponents(ActionRow.of(selectMenu))
* .addComponents(ActionRow.of(button1, button2))
* .queue();
* }</pre>
*
* @param components
* The layout components to add
*
* @throws IllegalArgumentException
* <ul>
* <li>If {@code null} is provided</li>
* <li>If any of the components is not {@link LayoutComponent#isMessageCompatible() message compatible}</li>
* <li>If the accumulated list of components is longer than {@value Message#MAX_COMPONENT_COUNT}</li>
* </ul>
*
* @return The same instance for chaining
*
* @see ActionRow
*/
@Nonnull
default R addComponents(@Nonnull LayoutComponent... components)
{
return addComponents(Arrays.asList(components));
}
/**
* Appends a single {@link ActionRow} to the request.
* <br>Use {@link #setComponents(Collection)} instead, to replace the components entirely.
*
* <p><b>Example</b><br>
* Sending a message with multiple action rows:
* <pre>{@code
* channel.sendMessageComponents(ActionRow.of(selectMenu))
* .addActionRow(button1, button2)
* .queue();
* }</pre>
*
* @param components
* The {@link ItemComponent components} to add to the action row, must not be empty
*
* @throws IllegalArgumentException
* <ul>
* <li>If {@code null} is provided</li>
* <li>If any of the components is not {@link ItemComponent#isMessageCompatible() message compatible}</li>
* <li>If the accumulated list of components is longer than {@value Message#MAX_COMPONENT_COUNT}</li>
* <li>In all the same cases as {@link ActionRow#of(Collection)} throws an exception</li>
* </ul>
*
* @return The same instance for chaining
*
* @see ActionRow#of(Collection)
*/
@Nonnull
default R addActionRow(@Nonnull Collection<? extends ItemComponent> components)
{
return addComponents(ActionRow.of(components));
}
/**
* Appends a single {@link ActionRow} to the request.
* <br>Use {@link #setComponents(Collection)} instead, to replace the components entirely.
*
* <p><b>Example</b><br>
* Sending a message with multiple action rows:
* <pre>{@code
* channel.sendMessageComponents(ActionRow.of(selectMenu))
* .addActionRow(button1, button2)
* .queue();
* }</pre>
*
* @param components
* The {@link ItemComponent components} to add to the action row, must not be empty
*
* @throws IllegalArgumentException
* <ul>
* <li>If {@code null} is provided</li>
* <li>If any of the components is not {@link ItemComponent#isMessageCompatible() message compatible}</li>
* <li>If the accumulated list of components is longer than {@value Message#MAX_COMPONENT_COUNT}</li>
* <li>In all the same cases as {@link ActionRow#of(ItemComponent...)} throws an exception</li>
* </ul>
*
* @return The same instance for chaining
*
* @see ActionRow#of(ItemComponent...)
*/
@Nonnull
default R addActionRow(@Nonnull ItemComponent... components)
{
return addComponents(ActionRow.of(components));
}
/**
* Appends the provided {@link FileUpload FileUploads} to the request.
* <br>Use {@link #setFiles(Collection)} instead, to replace the file attachments entirely.
*
* <p><b>Resource Handling Note:</b> Once the request is handed off to the requester, for example when you call {@link RestAction#queue()},
* the requester will automatically clean up all opened files by itself. You are only responsible to close them yourself if it is never handed off properly.
* For instance, if an exception occurs after using {@link FileUpload#fromData(File)}, before calling {@link RestAction#queue()}.
* You can safely use a try-with-resources to handle this, since {@link FileUpload#close()} becomes ineffective once the request is handed off.
*
* <p><b>Example</b><br>
* Sending a message with multiple files:
* <pre>{@code
* channel.sendFiles(file1).addFiles(file2).queue();
* }</pre>
*
* @param files
* The files to add
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The same instance for chaining
*/
@Nonnull
R addFiles(@Nonnull Collection<? extends FileUpload> files);
/**
* Appends the provided {@link FileUpload FileUploads} to the request.
* <br>Use {@link #setFiles(Collection)} instead, to replace the file attachments entirely.
*
* <p><b>Resource Handling Note:</b> Once the request is handed off to the requester, for example when you call {@link RestAction#queue()},
* the requester will automatically clean up all opened files by itself. You are only responsible to close them yourself if it is never handed off properly.
* For instance, if an exception occurs after using {@link FileUpload#fromData(File)}, before calling {@link RestAction#queue()}.
* You can safely use a try-with-resources to handle this, since {@link FileUpload#close()} becomes ineffective once the request is handed off.
*
* <p><b>Example</b><br>
* Sending a message with multiple files:
* <pre>{@code
* channel.sendFiles(file1).addFiles(file2).queue();
* }</pre>
*
* @param files
* The files to add
*
* @throws IllegalArgumentException
* If null is provided
*
* @return The same instance for chaining
*/
@Nonnull
default R addFiles(@Nonnull FileUpload... files)
{
return addFiles(Arrays.asList(files));
}
@Nonnull
@Override
List<FileUpload> getAttachments();
/**
* Whether the message should use <em>Text-to-Speech</em> (TTS).
*
* <p>Requires {@link net.dv8tion.jda.api.Permission#MESSAGE_TTS Permission.MESSAGE_TTS} to be enabled.
*
* @param tts
* True, if the message should use TTS
*
* @return The same instance for chaining
*/
@Nonnull
R setTTS(boolean tts);
/**
* Set whether this message should trigger push/desktop notifications to other users.
* <br>When a message is suppressed, it will not trigger push/desktop notifications.
*
* @param suppressed
* True, if this message should not trigger push/desktop notifications
*
* @return The same reply action, for chaining convenience
*/
@Nonnull
R setSuppressedNotifications(boolean suppressed);
/**
* Applies the provided {@link MessageCreateData} to this request.
*
* @param data
* The message create data to apply
*
* @throws IllegalArgumentException
* If the data is null
*
* @return The same instance for chaining
*/
@Nonnull
default R applyData(@Nonnull MessageCreateData data)
{
Checks.notNull(data, "MessageCreateData");
final List<LayoutComponent> layoutComponents = data.getComponents().stream()
.map(LayoutComponent::createCopy)
.collect(Collectors.toList());
return setContent(data.getContent())
.setAllowedMentions(data.getAllowedMentions())
.mentionUsers(data.getMentionedUsers())
.mentionRoles(data.getMentionedRoles())
.mentionRepliedUser(data.isMentionRepliedUser())
.setEmbeds(data.getEmbeds())
.setTTS(data.isTTS())
.setSuppressedNotifications(data.isSuppressedNotifications())
.setComponents(layoutComponents)
.setFiles(data.getFiles());
}
@Nonnull
default R applyMessage(@Nonnull Message message)
{
Checks.notNull(message, "Message");
Checks.check(!message.getType().isSystem(), "Cannot copy a system message");
List<MessageEmbed> embeds = message.getEmbeds()
.stream()
.filter(e -> e.getType() == EmbedType.RICH)
.collect(Collectors.toList());
return setContent(message.getContentRaw())
.setEmbeds(embeds)
.setTTS(message.isTTS())
.setSuppressedNotifications(message.isSuppressedNotifications())
.setComponents(message.getActionRows());
}
/**
* Applies the provided {@link MessageEditData} to this request.
* <br>This will only set fields which were explicitly set on the {@link MessageEditBuilder},
* unless it was configured to be {@link MessageEditRequest#setReplace(boolean) replacing}.
*
* <p>This will <b>not</b> copy the message's attachments, only any configured {@link FileUpload FileUploads}.
* To copy attachments, you must download them explicitly instead.
*
* @param data
* The message create data to apply
*
* @throws IllegalArgumentException
* If the data is null
*
* @return The same instance for chaining
*/
@Nonnull
@SuppressWarnings({"unchecked", "ResultOfMethodCallIgnored"})
default R applyEditData(@Nonnull MessageEditData data)
{
Checks.notNull(data, "MessageEditData");
if (data.isSet(MessageEditBuilder.CONTENT))
setContent(data.getContent());
if (data.isSet(MessageEditBuilder.EMBEDS))
setEmbeds(data.getEmbeds());
if (data.isSet(MessageEditBuilder.COMPONENTS))
{
final List<LayoutComponent> layoutComponents = data.getComponents().stream()
.map(LayoutComponent::createCopy)
.collect(Collectors.toList());
setComponents(layoutComponents);
}
if (data.isSet(MessageEditBuilder.ATTACHMENTS))
setFiles(data.getFiles());
if (data.isSet(MessageEditBuilder.MENTIONS))
{
setAllowedMentions(data.getAllowedMentions());
mentionUsers(data.getMentionedUsers());
mentionRoles(data.getMentionedRoles());
mentionRepliedUser(data.isMentionRepliedUser());
}
return (R) this;
}
}