-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathApi.php
1427 lines (1367 loc) · 67.3 KB
/
Api.php
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* DiscordBot, PocketMine-MP Plugin.
*
* Licensed under the Open Software License version 3.0 (OSL-3.0)
* Copyright (C) 2020-present JaxkDev
*
* Discord :: JaxkDev
* Email :: [email protected]
*/
namespace JaxkDev\DiscordBot\Plugin;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestAddReaction;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestAddRole;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestBanMember;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestBroadcastTyping;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestBulkDeleteMessages;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestCreateChannel;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestCreateInvite;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestCreateRole;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestCreateThread;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestCreateThreadFromMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestCreateWebhook;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestDeleteChannel;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestDeleteInvite;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestDeleteMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestDeleteRole;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestDeleteWebhook;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestEditMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchBans;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchChannel;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchChannels;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchGuild;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchGuilds;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchInvites;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchMember;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchMembers;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchPinnedMessages;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchRole;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchRoles;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchUser;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchUsers;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestFetchWebhooks;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestInteractionRespondWithAutocomplete;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestInteractionRespondWithMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestInteractionRespondWithModal;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestKickMember;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestLeaveGuild;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestPinMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestRemoveAllReactions;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestRemoveReaction;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestRemoveRole;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestSendMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUnbanMember;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUnpinMessage;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUpdateBotPresence;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUpdateChannel;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUpdateNickname;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUpdateRole;
use JaxkDev\DiscordBot\Communication\Packets\Plugin\RequestUpdateWebhook;
use JaxkDev\DiscordBot\Libs\React\Promise\PromiseInterface;
use JaxkDev\DiscordBot\Models\Channels\Channel;
use JaxkDev\DiscordBot\Models\Channels\ChannelType;
use JaxkDev\DiscordBot\Models\Channels\ForumTag;
use JaxkDev\DiscordBot\Models\Channels\Overwrite;
use JaxkDev\DiscordBot\Models\Channels\VideoQualityMode;
use JaxkDev\DiscordBot\Models\Emoji;
use JaxkDev\DiscordBot\Models\Interactions\Commands\CommandOptionChoice;
use JaxkDev\DiscordBot\Models\Interactions\Interaction;
use JaxkDev\DiscordBot\Models\Interactions\InteractionType;
use JaxkDev\DiscordBot\Models\Messages\Component\ActionRow;
use JaxkDev\DiscordBot\Models\Messages\Component\ComponentType;
use JaxkDev\DiscordBot\Models\Messages\Embed\Embed;
use JaxkDev\DiscordBot\Models\Messages\Message;
use JaxkDev\DiscordBot\Models\Permissions\RolePermissions;
use JaxkDev\DiscordBot\Models\Presence\Activity\Activity;
use JaxkDev\DiscordBot\Models\Presence\Presence;
use JaxkDev\DiscordBot\Models\Presence\Status;
use JaxkDev\DiscordBot\Models\Role;
use JaxkDev\DiscordBot\Models\User;
use JaxkDev\DiscordBot\Models\Webhook;
use JaxkDev\DiscordBot\Models\WebhookType;
use JaxkDev\DiscordBot\Plugin\Events\BotUserUpdated;
use JaxkDev\DiscordBot\Plugin\Events\DiscordClosed;
use JaxkDev\DiscordBot\Plugin\Events\DiscordReady;
use pocketmine\event\EventPriority;
use function count;
use function in_array;
use function JaxkDev\DiscordBot\Libs\React\Promise\reject as rejectPromise;
use function sizeof;
use function strlen;
/**
* For internal and developers use for interacting with the discord bot.
*
* @see Main::getApi() To get instance.
*/
final class Api{
private Main $plugin;
/** @var bool If the API is ready to be used. */
private bool $ready = false;
/** @var User The connected bot user */
private User $bot_user;
public function __construct(Main $plugin){
$this->plugin = $plugin;
try{
$this->plugin->getServer()->getPluginManager()->registerEvent(DiscordReady::class, function(DiscordReady $event){
$this->bot_user = $event->getBotUser();
$this->ready = true;
$this->plugin->getLogger()->notice("DiscordBot Connected, API is ready.");
}, EventPriority::LOWEST, $this->plugin, true);
$this->plugin->getServer()->getPluginManager()->registerEvent(DiscordClosed::class, function(DiscordClosed $event){
$this->ready = false;
$this->plugin->getLogger()->notice("DiscordBot Disconnected, API no longer ready.");
}, EventPriority::LOWEST, $this->plugin, true);
$this->plugin->getServer()->getPluginManager()->registerEvent(BotUserUpdated::class, function(BotUserUpdated $event){
$this->bot_user = $event->getBot();
$this->plugin->getLogger()->debug("Updated API Bot user.");
}, EventPriority::LOWEST, $this->plugin, true);
}catch(\Throwable $e){
$this->plugin->getLogger()->logException($e);
}
}
/**
* @return bool Whether the API is ready to be used.
*/
public function isReady(): bool{
return $this->ready;
}
public function getBotUser(): User{
return $this->bot_user;
}
/**
* Fetch all Bans in the specified guild.
*
* @return PromiseInterface Resolves with an array of Ban models.
*/
public function fetchBans(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
$pk = new RequestFetchBans($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch a Channel by ID.
*
* @return PromiseInterface Resolves with an array of Channel models.
*/
public function fetchChannel(?string $guild_id, string $channel_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
$pk = new RequestFetchChannel($guild_id, $channel_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Channels in the specified Guild.
*
* @return PromiseInterface Resolves with an array of Channel models.
*/
public function fetchChannels(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
$pk = new RequestFetchChannels($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch a Guild by ID.
*
* @return PromiseInterface Resolves with a Guild model.
*/
public function fetchGuild(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
$pk = new RequestFetchGuild($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Guilds the bot is in.
*
* @return PromiseInterface Resolves with an array of Guild models.
*/
public function fetchGuilds(): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
$pk = new RequestFetchGuilds();
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Invites in the specified Guild.
*
* @return PromiseInterface Resolves with an array of Invite models.
*/
public function fetchInvites(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
$pk = new RequestFetchInvites($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch a Member from the specified Guild by User ID.
*
* @return PromiseInterface Resolves with an array of Member models.
*/
public function fetchMember(string $guild_id, string $user_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'"));
}
$pk = new RequestFetchMember($guild_id, $user_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Members in the specified Guild.
*
* @return PromiseInterface Resolves with an array of Member models.
*/
public function fetchMembers(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
$pk = new RequestFetchMembers($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch a Message by ID.
*
* @return PromiseInterface Resolves with a Message model.
*/
public function fetchMessage(?string $guild_id, string $channel_id, string $message_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'"));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'"));
}
$pk = new RequestFetchMessage($guild_id, $channel_id, $message_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Pinned Messages in the specified Channel.
*
* @return PromiseInterface Resolves with an array of Message models.
*/
public function fetchPinnedMessages(?string $guild_id, string $channel_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'"));
}
$pk = new RequestFetchPinnedMessages($guild_id, $channel_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch a Role by ID.
*
* @return PromiseInterface Resolves with a Role model.
*/
public function fetchRole(string $guild_id, string $role_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
if(!Utils::validDiscordSnowflake($role_id)){
return rejectPromise(new ApiRejection("Invalid role ID '$role_id'"));
}
$pk = new RequestFetchRole($guild_id, $role_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Roles in the specified Guild.
*
* @return PromiseInterface Resolves with an array of Role models.
*/
public function fetchRoles(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
$pk = new RequestFetchRoles($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch a User by ID.
*
* @return PromiseInterface Resolves with a User model.
*/
public function fetchUser(string $user_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'"));
}
$pk = new RequestFetchUser($user_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Users (may not be a complete list).
*
* @return PromiseInterface Resolves with an array of User models.
*/
public function fetchUsers(): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
$pk = new RequestFetchUsers();
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Fetch all Webhooks in the specified Guild (optionally channel specific webhooks).
*
* @return PromiseInterface Resolves with an array of Webhook models.
*/
public function fetchWebhooks(string $guild_id, ?string $channel_id = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'"));
}
if($channel_id !== null && !Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'"));
}
$pk = new RequestFetchWebhooks($guild_id, $channel_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Creates a normal webhook inside a channel.
*
* @param string $name max 80chars, 'clyde' and 'discord' not allowed in name.
* @param ?string $avatar_data If null, the default webhook avatar will be used. (see Utils::imageToDiscordData())
* @see Utils::imageToDiscordData()
*
* @return PromiseInterface Resolves with a Webhook model.
*/
public function createWebhook(string $guild_id, string $channel_id, string $name, ?string $avatar_data = null,
?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Webhook guild ID is invalid."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Webhook channel ID is invalid."));
}
if($avatar_data !== null && !Utils::validImageData($avatar_data)){
return rejectPromise(new ApiRejection("Webhook avatar data is invalid."));
}
$pk = new RequestCreateWebhook($guild_id, $channel_id, $name, $avatar_data, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Update a webhooks name or avatar hash.
*
* To change webhook avatar, set avatar to null in Webhook model and provide new VALID IMAGE DATA (Utils::imageToDiscordData()) as $new_avatar_data.
* To remove webhook avatar, set avatar to null in Webhook model.
*
* @return PromiseInterface Resolves with a Webhook model.
*@link Utils::imageToDiscordData()
*/
public function updateWebhook(Webhook $webhook, ?string $new_avatar_data = null, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($webhook->getType() !== WebhookType::INCOMING){
return rejectPromise(new ApiRejection("Only Incoming webhooks can be edited."));
}
if($webhook->getToken() === null){
return rejectPromise(new ApiRejection("Webhook does not have a token, it cannot be edited before being created."));
}
if(!Utils::validDiscordSnowflake($webhook->getId())){
return rejectPromise(new ApiRejection("Invalid webhook ID '{$webhook->getId()}'."));
}
if($new_avatar_data !== null && !Utils::validImageData($new_avatar_data)){
return rejectPromise(new ApiRejection("Webhook new avatar data is invalid."));
}
$pk = new RequestUpdateWebhook($webhook, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Delete a webhook
*
* @return PromiseInterface Resolves with no data.
*/
public function deleteWebhook(string $guild_id, string $channel_id, string $webhook_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Webhook guild ID is invalid."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($webhook_id)){
return rejectPromise(new ApiRejection("Invalid webhook ID '$webhook_id'."));
}
$pk = new RequestDeleteWebhook($guild_id, $channel_id, $webhook_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
//createGuild will not be added due to security issues,
//If you find a genuine use for createGuild please open an issue.
/**
* Leave a discord guild.
*
* @return PromiseInterface Resolves with no data.
*/
public function leaveGuild(string $guild_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
$pk = new RequestLeaveGuild($guild_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Pin a message to the channel.
*
* @return PromiseInterface Resolves with no data.
*/
public function pinMessage(?string $guild_id, string $channel_id, string $message_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'."));
}
$pk = new RequestPinMessage($guild_id, $channel_id, $message_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Un-pin a message to the channel.
*
* @return PromiseInterface Resolves with no data.
*/
public function unpinMessage(?string $guild_id, string $channel_id, string $message_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'."));
}
$pk = new RequestUnpinMessage($guild_id, $channel_id, $message_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Create a role.
*
* (Note, icon_data and unicode_emoji only work with guilds with the ROLE_ICONS feature)
*
* @return PromiseInterface Resolves with Role model.
*/
public function createRole(string $guild_id, string $name = "new role", RolePermissions $permissions = null,
int $colour = 0, bool $hoist = false, ?string $icon_data = null,
?string $unicode_emoji = null, bool $mentionable = false, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($icon_data !== null && !Utils::validImageData($icon_data)){
return rejectPromise(new ApiRejection("Invalid icon data '$icon_data'."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
$pk = new RequestCreateRole($guild_id, $name, $permissions ?? new RolePermissions(), $colour, $hoist, $icon_data,
$unicode_emoji, $mentionable, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Update an already created role, ID must be present.
*
* Note you cannot change the hoisted position of the 'everyone' role, or move any role higher than the bots highest role.
*
* If hoisted position changed, all roles that move to account for the change will emit an updated event.
*
* To change role icon, set icon to null in Role model and provide new VALID IMAGE DATA (Utils::imageToDiscordData()) as $new_icon_data.
* To remove role icon, set icon to null in Role model.
*
* @link Utils::imageToDiscordData()
* @return PromiseInterface Resolves with a Role model.
*/
public function updateRole(Role $role, ?string $new_icon_data = null, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($new_icon_data !== null && !Utils::validImageData($new_icon_data)){
return rejectPromise(new ApiRejection("Invalid icon data '$new_icon_data'."));
}
$pk = new RequestUpdateRole($role, $new_icon_data, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Delete a role.
*
* @return PromiseInterface Resolves with no data.
*/
public function deleteRole(string $guild_id, string $role_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($role_id)){
return rejectPromise(new ApiRejection("Invalid role ID '$role_id'."));
}
$pk = new RequestDeleteRole($guild_id, $role_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Remove a role from a member.
*
* @return PromiseInterface Resolves with no data.
*/
public function removeRole(string $guild_id, string $user_id, string $role_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'."));
}
if(!Utils::validDiscordSnowflake($role_id)){
return rejectPromise(new ApiRejection("Invalid role ID '$role_id'."));
}
$pk = new RequestRemoveRole($guild_id, $user_id, $role_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Give the member a role.
*
* @return PromiseInterface Resolves with no data.
*/
public function addRole(string $guild_id, string $user_id, string $role_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'."));
}
if(!Utils::validDiscordSnowflake($role_id)){
return rejectPromise(new ApiRejection("Invalid role ID '$role_id'."));
}
$pk = new RequestAddRole($guild_id, $user_id, $role_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Remove a single reaction.
*
* @param Emoji $emoji e.g. Emoji::fromUnicode('👍') or Emoji::fromPrivate() for custom/private/animated.
* See Emoji::class for more info.
* @return PromiseInterface Resolves with no data.
* @see Emoji::fromUnicode()
* @see Emoji::fromPrivate()
*/
public function removeReaction(?string $guild_id, string $channel_id, string $message_id, string $user_id,
Emoji $emoji): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'."));
}
$pk = new RequestRemoveReaction($guild_id, $channel_id, $message_id, $user_id, $emoji);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Remove all reactions on a message.
*
* @param Emoji|null $emoji If no emoji specified ALL reactions by EVERYONE will be deleted,
* if specified everyone's reaction with that emoji will be removed.
*
* e.g. Emoji::fromUnicode('👍') or Emoji::fromPrivate() for custom/private/animated.
* See Emoji::class for more info.
* @return PromiseInterface Resolves with no data.
* @see Emoji::fromUnicode()
* @see Emoji::fromPrivate()
*/
public function removeAllReactions(?string $guild_id, string $channel_id, string $message_id,
?Emoji $emoji = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'."));
}
$pk = new RequestRemoveAllReactions($guild_id, $channel_id, $message_id, $emoji);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Add a reaction to a message.
*
* Note, If you have already reacted with the emoji provided it will still respond with a successful promise resolution.
*
* @param Emoji $emoji e.g. Emoji::fromUnicode('👍') or Emoji::fromPrivate() for custom/private/animated.
* See Emoji::class for more info.
* @return PromiseInterface Resolves with no data.
* @see Emoji::fromPrivate()
* @see Emoji::fromUnicode()
*/
public function addReaction(?string $guild_id, string $channel_id, string $message_id, Emoji $emoji): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'."));
}
$pk = new RequestAddReaction($guild_id, $channel_id, $message_id, $emoji);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* "Generally bots should not implement this. However, if a bot is responding to a command and expects the computation
* to take a few seconds, this endpoint may be called to let the user know that the bot is processing their message."
* The 'typing' effect will last for 5s
*
* DO NOT ABUSE THIS.
*
* @return PromiseInterface Resolves with no data.
*/
public function broadcastTyping(?string $guild_id, string $channel_id): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
$pk = new RequestBroadcastTyping($guild_id, $channel_id);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Sends a new presence to replace the current one the bot has.
*
* @return PromiseInterface Resolves with no data.
*/
public function updateBotPresence(Status $status = Status::ONLINE, Activity $activity = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
$pk = new RequestUpdateBotPresence(new Presence($status, $activity === null ? [] : [$activity], null));
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Attempt to ban a member.
*
* @param int $delete_message_seconds number of seconds to delete messages for, between 0 and 604800 (7 days)
*
* @return PromiseInterface Resolves with no data.
*/
public function banMember(string $guild_id, string $user_id, int $delete_message_seconds = 0, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'."));
}
if($delete_message_seconds < 0 || $delete_message_seconds > 604800){
return rejectPromise(new ApiRejection("Delete message seconds must be between 0 and 604800 (7days)."));
}
$pk = new RequestBanMember($guild_id, $user_id, $delete_message_seconds, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Attempt to unban a member.
*
* @return PromiseInterface Resolves with no data.
*/
public function unbanMember(string $guild_id, string $user_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'."));
}
$pk = new RequestUnbanMember($guild_id, $user_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Attempt to kick a member.
*
* @return PromiseInterface Resolves with no data.
*/
public function kickMember(string $guild_id, string $user_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(!Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($user_id)){
return rejectPromise(new ApiRejection("Invalid user ID '$user_id'."));
}
$pk = new RequestKickMember($guild_id, $user_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Sends a Message to discord.
*
* At least one of "content, embeds, sticker_ids, components, or files" is required.
*
* @param string|null $content Max 2000 characters. Read note above.
* @param string|null $reply_message_id Message ID to reply to, null if not a reply message.
* @param Embed[]|null $embeds Array of embeds, max 10. Read note above.
* @param bool|null $tts Text to speech message?
* @param ActionRow[]|null $components Array of ActionRow components, max 5. Read note above. (cannot contain TEXT_INPUT components)
* @param string[]|null $sticker_ids Array of sticker IDs, max 3. Read note above.
* @param array<string, string>|null $files Array of file data to send, max 8MB total. Read note above.
* Key is the file name, value is the file data.
* e.g. ['file.png' => 'raw_file_data']
*
* @return PromiseInterface Resolves with a Message model.
*/
public function sendMessage(?string $guild_id, string $channel_id, ?string $content = null, ?string $reply_message_id = null,
?array $embeds = null, ?bool $tts = null, ?array $components = null, ?array $sticker_ids = null,
?array $files = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(strlen($content ?? "") > 2000){
return rejectPromise(new ApiRejection("Message content cannot be larger than 2000 characters for bots."));
}
if($reply_message_id !== null && !Utils::validDiscordSnowflake($reply_message_id)){
return rejectPromise(new ApiRejection("Invalid reply message ID '$reply_message_id'."));
}
if(count($embeds ?? []) > 10){
return rejectPromise(new ApiRejection("Embed array cannot contain more than 10 embeds."));
}
foreach(($embeds ?? []) as $embed){
if(!$embed instanceof Embed){
return rejectPromise(new ApiRejection("Embed array must all be of type '" . Embed::class . "'."));
}
}
if(count($components ?? []) > 5){
return rejectPromise(new ApiRejection("Components array cannot contain more than 5 ActionRow components."));
}
foreach(($components ?? []) as $comp){
if(!$comp instanceof ActionRow){
return rejectPromise(new ApiRejection("Components array must all be of type '" . ActionRow::class . "'."));
}
foreach($comp->getComponents() as $c){
if($c->getType() === ComponentType::TEXT_INPUT){
//Text inputs are MODAL FORM only, cannot be sent via message only via interaction response.
return rejectPromise(new ApiRejection("Components array cannot contain TEXT_INPUT type."));
}
}
}
if(count($sticker_ids ?? []) > 3){
return rejectPromise(new ApiRejection("Sticker array cannot contain more than 3 stickers."));
}
foreach(($sticker_ids ?? []) as $id){
if(!Utils::validDiscordSnowflake($id)){
return rejectPromise(new ApiRejection("Invalid sticker ID '$id'."));
}
}
foreach($files ?? [] as $name => $data){
if(strlen($name) > 256){
return rejectPromise(new ApiRejection("File name cannot be larger than 256 characters."));
}
if(strlen($data) > 8388608){
return rejectPromise(new ApiRejection("File data cannot be larger than 8388608 bytes."));
}
}
$pk = new RequestSendMessage($guild_id, $channel_id, $content, $reply_message_id, $embeds, $tts, $components,
$sticker_ids, $files);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Edit a sent message.
*
* TODO-Next-Minor, add support for editing message files & stickers.
*
* Note you can't convert a 'REPLY' message to a normal 'MESSAGE'.
* Note at the moment we don't support editing/removing/adding stickers/files :(
*
* @return PromiseInterface Resolves with a Message model.
*/
public function editMessage(Message $message): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if(strlen($message->getContent() ?? "") > 2000){
return rejectPromise(new ApiRejection("Message content cannot be larger than 2000 characters for bots."));
}
$pk = new RequestEditMessage($message);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* Delete a sent message.
*
* @return PromiseInterface Resolves with no data.
*/
public function deleteMessage(?string $guild_id, string $channel_id, string $message_id, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));
}
if($guild_id !== null && !Utils::validDiscordSnowflake($guild_id)){
return rejectPromise(new ApiRejection("Invalid guild ID '$guild_id'."));
}
if(!Utils::validDiscordSnowflake($channel_id)){
return rejectPromise(new ApiRejection("Invalid channel ID '$channel_id'."));
}
if(!Utils::validDiscordSnowflake($message_id)){
return rejectPromise(new ApiRejection("Invalid message ID '$message_id'."));
}
$pk = new RequestDeleteMessage($guild_id, $channel_id, $message_id, $reason);
$this->plugin->writeOutboundData($pk);
return ApiResolver::create($pk->getUID());
}
/**
* @param string|null $guild_id Null for DMs
* @param string[] $message_ids Unique array of message IDs (limit 100) to delete (messages cannot be older than 2 weeks)
*/
public function bulkDeleteMessages(?string $guild_id, string $channel_id, array $message_ids, ?string $reason = null): PromiseInterface{
if(!$this->ready){
return rejectPromise(new ApiRejection("API is not ready for requests."));