-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
WebhookCluster.java
813 lines (775 loc) · 32.1 KB
/
WebhookCluster.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*
* Copyright 2015-2017 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.webhook;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.entities.Webhook;
import net.dv8tion.jda.core.utils.Checks;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import org.json.JSONObject;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.function.Predicate;
/**
* A central collection of {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* which allows to broadcast ({@link #broadcast(WebhookMessage)}) or multicast ({@link #multicast(Predicate, WebhookMessage)})
* to all registered clients (receivers).
*
* <h3>Registering existing WebhookClients</h3>
* To register an existing WebhookClient instance (that is not currently shutdown)
* you either use {@link #addWebhooks(WebhookClient...)} or {@link #addWebhooks(java.util.Collection) addWebhooks(Collection<WebhookClient>)}.
* <br>These methods allow to register multiple clients in one call
* and will <b>fail if the client has been closed via {@link WebhookClient#close() WebhookClient.close()}</b>.
*
* <p><i><b>Note that you should always remove a WebhookClient from this cluster when you close it directly!</b></i>
*
* <h3>Removing already registered WebhookClients</h3>
* The cluster allows to remove existing clients in batch via {@link #removeWebhooks(WebhookClient...)}, {@link #removeWebhooks(Collection) removeWebhooks(Collection<WebhookClient>)}
* and {@link #removeIf(java.util.function.Predicate) removeIf(Predicate<WebhookClient>)}.
* It does not matter if the specified clients are actually registered to this cluster when using these methods.
*
* <p><i><b>Note that removing a WebhookClient from the cluster does not close it!</b></i>
*
* <h3>Building WebhookClients from the Cluster</h3>
* This class allows to set default values that will be provided to {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilder}
* when building via {@link #buildWebhooks(Webhook...)} and {@link #buildWebhooks(Collection) buildWebhooks(Collection<Webhook>)}.
* <br>The following settings can be used:
* <ul>
* <li>{@link #setDefaultExecutorService(ScheduledExecutorService)}</li>
* <li>{@link #setDefaultHttpClientBuilder(OkHttpClient.Builder)}</li>
* <li>{@link #setDefaultHttpClient(OkHttpClient)}</li>
* </ul>
*
* <p>Note that when you provide your own {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService} you are able to shut it down
* outside of the clients which will cause them to fail.
* <br><i><b>Do not shutdown the pool before closing all clients!</b></i>
*
* <h3>Sending to multiple Webhooks at once</h3>
* This cluster allows to both broadcast and multicast to registered clients.
* <br>When broadcasting a message is created before iterating each client to save performance which makes the broadcast
* method superior to direct for-loops.
*
* <p>Multicasting will send a message to all clients which meet a set filter.
* The filter is specified using a {@link java.util.function.Predicate Predicate} which is provided to {@link #multicast(Predicate, WebhookMessage)}.
* <br>The predicate decides whether the client should receive the message (returning true) or should be ignored (returning false).
*
* <h3>Closing all connections</h3>
* Each {@link net.dv8tion.jda.webhook.WebhookClient WebhookClient} is a {@link java.io.Closeable Closable} resource which means
* it must be closed to free resources and enhance performance of the JVM.
* <br>The WebhookCluster allows to close all registered clients using {@link #close()}.
* Calling close on the cluster means it will <i><b>remove and close</b></i> all currently registered webhooks.
* <br>The cluster may still be used after closing.
*/
public class WebhookCluster implements AutoCloseable
{
protected final List<WebhookClient> webhooks;
protected OkHttpClient.Builder defaultHttpClientBuilder;
protected OkHttpClient defaultHttpClient;
protected ScheduledExecutorService defaultPool;
protected ThreadFactory threadFactory;
protected boolean isDaemon;
/**
* Creates a new WebhookCluster with the provided
* {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} as initial client cache.
*
* @param initialClients
* Collection of WebhookClients that should be added
*
* @throws java.lang.IllegalArgumentException
* If any of the provided clients is {@code null} or closed
*/
public WebhookCluster(@Nonnull Collection<? extends WebhookClient> initialClients)
{
webhooks = new ArrayList<>(initialClients.size());
for (WebhookClient client : initialClients)
addWebhooks(client);
}
/**
* Creates a new WebhookCluster with the specified initial capacity.
* <br>For more information about this see {@link java.util.ArrayList ArrayList}.
*
* @param initialCapacity
* The initial capacity for this cluster
*
* @throws java.lang.IllegalArgumentException
* If the provided capacity is negative
*/
public WebhookCluster(int initialCapacity)
{
webhooks = new ArrayList<>(initialCapacity);
}
/**
* Creates a new WebhookCluster with default initial capacity
* and no registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
*/
public WebhookCluster()
{
webhooks = new ArrayList<>();
}
// Default builder values
/**
* Sets the default {@link okhttp3.OkHttpClient.Builder OkHttpClient.Builder} that should be
* used when building {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} via
* {@link #buildWebhooks(Webhook...)} or {@link #buildWebhooks(Collection)}.
*
* @param builder
* The default builder, {@code null} to reset
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster setDefaultHttpClientBuilder(@Nullable OkHttpClient.Builder builder)
{
this.defaultHttpClientBuilder = builder;
return this;
}
/**
* Sets the default {@link okhttp3.OkHttpClient OkHttpClient} that should be
* used when building {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} via
* {@link #buildWebhooks(Webhook...)} or {@link #buildWebhooks(Collection)}.
*
* @param defaultHttpClient
* The default client, {@code null} to reset
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster setDefaultHttpClient(@Nullable OkHttpClient defaultHttpClient)
{
this.defaultHttpClient = defaultHttpClient;
return this;
}
/**
* Sets the default {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService} that should be
* used when building {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} via
* {@link #buildWebhooks(Webhook...)} or {@link #buildWebhooks(Collection)}.
*
* @param executorService
* The default executor service, {@code null} to reset
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster setDefaultExecutorService(@Nullable ScheduledExecutorService executorService)
{
this.defaultPool = executorService;
return this;
}
/**
* Factory that should be used by the default {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService}
* to create Threads for rate limitation handling of the created {@link net.dv8tion.jda.webhook.WebhookClient WebhookClient}!
* <br>This allows changing thread information such as name without having to create your own executor.
*
* @param factory
* The {@link java.util.concurrent.ThreadFactory ThreadFactory} that will
* be used when no {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService}
* has been set via {@link #setDefaultExecutorService(ScheduledExecutorService)}
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster setDefaultThreadFactory(@Nullable ThreadFactory factory)
{
this.threadFactory = factory;
return this;
}
/**
* Whether rate limit threads of created {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* should be treated as {@link Thread#isDaemon()} or not.
* <br><b>Default: false</b>
*
* <p>This will not be used when the default thread pool has been set via {@link #setDefaultExecutorService(ScheduledExecutorService)}!
*
* @param isDaemon
* True, if the threads should be daemon
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster setDefaultDaemon(boolean isDaemon)
{
this.isDaemon = isDaemon;
return this;
}
// Webhook creation/add/remove
/**
* Creates new {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} and adds them
* to this cluster.
* <br>The {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilders}
* will be supplied with the default settings of this cluster.
*
* @param webhooks
* Webhooks to target (duplicates will not be filtered)
*
* @throws java.lang.IllegalArgumentException
* If the provided array or any of the contained
* webhooks is {@code null}
*
* @return The current WebhookCluster for chaining convenience
*
* @see #buildWebhooks(Webhook...)
* @see #newBuilder(Webhook)
*/
public WebhookCluster buildWebhooks(Webhook... webhooks)
{
Checks.notNull(webhooks, "Webhooks");
for (Webhook webhook : webhooks)
{
Checks.notNull(webhook, "Webhook");
buildWebhook(webhook.getIdLong(), webhook.getToken());
}
return this;
}
/**
* Creates new {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} and adds them
* to this cluster.
* <br>The {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilders}
* will be supplied with the default settings of this cluster.
*
* @param webhooks
* Webhooks to target (duplicates will not be filtered)
*
* @throws java.lang.IllegalArgumentException
* If the provided collection or any of the contained
* webhooks is {@code null}
*
* @return The current WebhookCluster for chaining convenience
*
* @see #buildWebhooks(Webhook...)
* @see #newBuilder(Webhook)
*/
public WebhookCluster buildWebhooks(Collection<Webhook> webhooks)
{
Checks.notNull(webhooks, "Webhooks");
for (Webhook webhook : webhooks)
{
Checks.notNull(webhook, "Webhook");
buildWebhook(webhook.getIdLong(), webhook.getToken());
}
return this;
}
/**
* Creates new {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} and adds them
* to this cluster.
* <br>The {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilders}
* will be supplied with the default settings of this cluster.
*
* @param id
* The id for the webhook
* @param token
* The token for the webhook
*
* @throws java.lang.IllegalArgumentException
* If the provided webhooks token is {@code null} or contains whitespace
*
* @return The current WebhookCluster for chaining convenience
*
* @see #newBuilder(long, String)
*/
public WebhookCluster buildWebhook(long id, String token)
{
this.webhooks.add(newBuilder(id, token).build());
return this;
}
/**
* Creates a new {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilder}
* with the defined default settings of this cluster.
*
* @param id
* The webhook id
* @param token
* The webhook token
*
* @throws java.lang.IllegalArgumentException
* If the token is {@code null}, empty or contains blanks
*
* @return The WebhookClientBuilder with default settings
*
* @see net.dv8tion.jda.webhook.WebhookClientBuilder#WebhookClientBuilder(long, String) new WebhookClientBuilder(long, String)
*/
public WebhookClientBuilder newBuilder(long id, String token)
{
WebhookClientBuilder builder = new WebhookClientBuilder(id, token);
builder.setExecutorService(defaultPool)
.setHttpClient(defaultHttpClient)
.setThreadFactory(threadFactory)
.setDaemon(isDaemon);
if (defaultHttpClientBuilder != null)
builder.setHttpClientBuilder(defaultHttpClientBuilder);
return builder;
}
/**
* Creates a new {@link net.dv8tion.jda.webhook.WebhookClientBuilder WebhookClientBuilder}
* with the defined default settings of this cluster.
*
* @param webhook
* The target webhook
*
* @throws java.lang.IllegalArgumentException
* If the webhook is {@code null}
*
* @return The WebhookClientBuilder with default settings
*
* @see net.dv8tion.jda.webhook.WebhookClientBuilder#WebhookClientBuilder(Webhook) new WebhookClientBuilder(Webhook)
*/
public WebhookClientBuilder newBuilder(Webhook webhook)
{
Checks.notNull(webhook, "Webhook");
return newBuilder(webhook.getIdLong(), webhook.getToken());
}
/**
* Adds the specified {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* to this cluster's list of receivers.
* <br>Duplicate clients are supported and will not be filtered automatically.
*
* @param clients
* WebhookClients to add
*
* @throws java.lang.IllegalArgumentException
* If the provided array or any of the contained
* clients is {@code null} or closed
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster addWebhooks(WebhookClient... clients)
{
Checks.notNull(clients, "Clients");
for (WebhookClient client : clients)
{
Checks.notNull(client, "Client");
Checks.check(!client.isShutdown,
"One of the provided WebhookClients has been closed already!");
webhooks.add(client);
}
return this;
}
/**
* Adds the specified {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* to this cluster's list of receivers.
* <br>Duplicate clients are supported and will not be filtered automatically.
*
* @param clients
* WebhookClients to add
*
* @throws java.lang.IllegalArgumentException
* If the provided collection or any of the contained
* clients is {@code null} or closed
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster addWebhooks(Collection<WebhookClient> clients)
{
Checks.notNull(clients, "Clients");
for (WebhookClient client : clients)
{
Checks.notNull(client, "Client");
Checks.check(!client.isShutdown,
"One of the provided WebhookClients has been closed already!");
webhooks.add(client);
}
return this;
}
/**
* Removes the specified {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* from this cluster's list of receivers.
* <br>It does not matter whether any of the provided clients is actually in the list of receivers.
*
* <p><b>Note that the removed clients are not closed by this operation!</b>
*
* @param clients
* WebhookClients to remove
*
* @throws java.lang.IllegalArgumentException
* If the provided array is {@code null}
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster removeWebhooks(WebhookClient... clients)
{
Checks.notNull(clients, "Clients");
webhooks.removeAll(Arrays.asList(clients));
return this;
}
/**
* Removes the specified {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* from this cluster's list of receivers.
* <br>It does not matter whether any of the provided clients is actually in the list of receivers.
*
* <p><b>Note that the removed clients are not closed by this operation!</b>
*
* @param clients
* WebhookClients to remove
*
* @throws java.lang.IllegalArgumentException
* If the provided collection is {@code null}
*
* @return The current WebhookCluster for chaining convenience
*/
public WebhookCluster removeWebhooks(Collection<WebhookClient> clients)
{
Checks.notNull(clients, "Clients");
webhooks.removeAll(clients);
return this;
}
/**
* Removes the specified {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* from this cluster's list of receivers under the conditions of the provided filter.
* <br>The filter should return {@code true} to remove provided clients and {@code false} to retain them.
*
* <p><b>Note that the removed clients are not closed by this operation!</b>
*
* @param predicate
* The filter
*
* @throws java.lang.IllegalArgumentException
* If the provided filter is {@code null}
*
* @return List of removed clients
*/
public List<WebhookClient> removeIf(Predicate<WebhookClient> predicate)
{
Checks.notNull(predicate, "Predicate");
List<WebhookClient> clients = new ArrayList<>();
for (WebhookClient client : webhooks)
{
if (predicate.test(client))
clients.add(client);
}
removeWebhooks(clients);
return clients;
}
/**
* Closes all {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} that meet
* the specified filter.
* <br>The filter may return {@code true} for all clients that should be <b>removed and closed</b>.
*
* @param predicate
* The filter to decide which clients to remove
*
* @throws java.lang.IllegalArgumentException
* If the provided filter is {@code null}
*
* @return List of removed and closed clients
*/
public List<WebhookClient> closeIf(Predicate<WebhookClient> predicate)
{
Checks.notNull(predicate, "Filter");
List<WebhookClient> clients = new ArrayList<>();
for (WebhookClient client : webhooks)
{
if (predicate.test(client))
clients.add(client);
}
removeWebhooks(clients);
clients.forEach(WebhookClient::close);
return clients;
}
/**
* The current list of receivers for this WebhookCluster instance.
* <br>The provided list is an immutable copy of the actual stored list of {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* @return Immutable list of registered receivers
*/
public List<WebhookClient> getWebhooks()
{
return Collections.unmodifiableList(new ArrayList<>(webhooks));
}
// Broadcasting / Multicasting
/**
* Sends the provided {@link net.dv8tion.jda.webhook.WebhookMessage WebhookMessage}
* to all {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients} that meet the specified
* filter.
* <br>The filter should return {@code true} for all clients that should receive the message.
*
* <p>Hint: Use {@link net.dv8tion.jda.webhook.WebhookMessageBuilder WebhookMessageBuilder} to
* create a {@link net.dv8tion.jda.webhook.WebhookMessage WebhookMessage} instance!
*
* @param filter
* The filter that decides what clients receive the message
* @param message
* The message that should be sent to the filtered clients
*
* @throws java.lang.IllegalArgumentException
* If any of the provided arguments is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> multicast(Predicate<WebhookClient> filter, WebhookMessage message)
{
Checks.notNull(filter, "Filter");
Checks.notNull(message, "Message");
final RequestBody body = message.getBody();
final List<Future<?>> callbacks = new ArrayList<>();
for (WebhookClient client : webhooks)
{
if (filter.test(client))
callbacks.add(client.execute(body));
}
return callbacks;
}
/**
* Sends the provided {@link net.dv8tion.jda.webhook.WebhookMessage WebhookMessage}
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p>Hint: Use {@link net.dv8tion.jda.webhook.WebhookMessageBuilder WebhookMessageBuilder} to
* create a {@link net.dv8tion.jda.webhook.WebhookMessage WebhookMessage} instance!
*
* @param message
* The message that should be sent to the clients
*
* @throws java.lang.IllegalArgumentException
* If any of the provided arguments is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(WebhookMessage message)
{
Checks.notNull(message, "Message");
final RequestBody body = message.getBody();
final List<Future<?>> callbacks = new ArrayList<>(webhooks.size());
for (WebhookClient webhook : webhooks)
callbacks.add(webhook.execute(body));
return callbacks;
}
/**
* Sends the provided {@link net.dv8tion.jda.core.entities.Message Message}
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p>Hint: Use {@link net.dv8tion.jda.core.MessageBuilder MessageBuilder} to
* create a {@link net.dv8tion.jda.core.entities.Message Message} instance!
*
* @param message
* The message that should be sent to the clients
*
* @throws java.lang.IllegalArgumentException
* If the provided message is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(Message message)
{
return broadcast(WebhookMessage.from(message));
}
/**
* Sends the provided {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbeds}
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p><b>You can send up to 10 embeds per message! If more are sent they will not be displayed.</b>
*
* <p>Hint: Use {@link net.dv8tion.jda.core.EmbedBuilder EmbedBuilder} to
* create a {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbeds} instance!
*
* @param embeds
* The embeds that should be sent to the clients
*
* @throws java.lang.IllegalArgumentException
* If any of the provided arguments is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(MessageEmbed... embeds)
{
return broadcast(WebhookMessage.of(embeds));
}
/**
* Sends the provided {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbeds}
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p><b>You can send up to 10 embeds per message! If more are sent they will not be displayed.</b>
*
* <p>Hint: Use {@link net.dv8tion.jda.core.EmbedBuilder EmbedBuilder} to
* create a {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbeds} instance!
*
* @param embeds
* The embeds that should be sent to the clients
*
* @throws java.lang.IllegalArgumentException
* If any of the provided arguments is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(Collection<MessageEmbed> embeds)
{
return broadcast(WebhookMessage.of(embeds));
}
/**
* Sends the provided text message
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* @param content
* The text that should be sent to the clients
*
* @throws java.lang.IllegalArgumentException
* If the provided content is {@code null} or blank
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(String content)
{
Checks.notBlank(content, "Content");
Checks.check(content.length() <= 2000, "Content may not exceed 2000 characters!");
final RequestBody body = WebhookClient.newBody(new JSONObject().put("content", content).toString());
final List<Future<?>> callbacks = new ArrayList<>(webhooks.size());
for (WebhookClient webhook : webhooks)
callbacks.add(webhook.execute(body));
return callbacks;
}
/**
* Sends the provided {@link java.io.File File}
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p><b>The provided data should not exceed 8MB in size!</b>
*
* @param file
* The file that should be sent to the clients
*
* @throws java.lang.IllegalArgumentException
* If the provided file is {@code null}, does not exist or ist not readable
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(File file)
{
Checks.notNull(file, "File");
return broadcast(file, file.getName());
}
/**
* Sends the provided {@link java.io.File File}
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p><b>The provided data should not exceed 8MB in size!</b>
*
* @param file
* The file that should be sent to the clients
* @param fileName
* The name that should be given to the file
*
* @throws java.lang.IllegalArgumentException
* If the provided file is {@code null}, does not exist or ist not readable
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(File file, String fileName)
{
Checks.notNull(file, "File");
Checks.check(file.length() <= Message.MAX_FILE_SIZE, "Provided File exceeds the maximum size of 8MB!");
return broadcast(new WebhookMessageBuilder().setFile(file, fileName).build());
}
/**
* Sends the provided {@link java.io.InputStream InputStream} as an attachment
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p><b>The provided data should not exceed 8MB in size!</b>
*
* @param data
* The data that should be sent to the clients
* @param fileName
* The name that should be given to the attachment
*
* @throws java.lang.IllegalArgumentException
* If the provided data is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(InputStream data, String fileName)
{
return broadcast(new WebhookMessageBuilder().setFile(data, fileName).build());
}
/**
* Sends the provided {@code byte[]} data as an attachment
* to all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}.
*
* <p><b>The provided data should not exceed 8MB in size!</b>
*
* @param data
* The data that should be sent to the clients
* @param fileName
* The name that should be given to the attachment
*
* @throws java.lang.IllegalArgumentException
* If the provided data is {@code null}
* @throws java.util.concurrent.RejectedExecutionException
* If any of the receivers has been shutdown
* @throws net.dv8tion.jda.core.exceptions.HttpException
* If the HTTP request fails
*
* @return A list of {@link java.util.concurrent.Future Future} instances
* representing all message tasks.
*/
public List<Future<?>> broadcast(byte[] data, String fileName)
{
Checks.notNull(data, "Data");
Checks.check(data.length < Message.MAX_FILE_SIZE, "Provided data exceeds the maximum size of 8MB!");
return broadcast(new WebhookMessageBuilder().setFile(data, fileName).build());
}
/**
* Closes all registered {@link net.dv8tion.jda.webhook.WebhookClient WebhookClients}
* and removes the from this cluster!
*/
@Override
public void close()
{
webhooks.forEach(WebhookClient::close);
webhooks.clear();
}
}