forked from discord-php/DiscordPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBuilder.php
696 lines (597 loc) · 16.1 KB
/
MessageBuilder.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
<?php
/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <[email protected]>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace Discord\Builders;
use Discord\Builders\Components\ActionRow;
use Discord\Builders\Components\Component;
use Discord\Builders\Components\SelectMenu;
use Discord\Exceptions\FileNotFoundException;
use Discord\Helpers\Multipart;
use Discord\Http\Exceptions\RequestFailedException;
use Discord\Parts\Channel\Attachment;
use Discord\Parts\Channel\Message;
use Discord\Parts\Embed\Embed;
use Discord\Parts\Guild\Sticker;
use JsonSerializable;
use function Discord\poly_strlen;
/**
* Helper class used to build messages.
*
* @since 7.0.0
*
* @author David Cole <[email protected]>
*/
class MessageBuilder implements JsonSerializable
{
/**
* Content of the message.
*
* @var string|null
*/
private $content;
/**
* A nonce that can be used for message roundtrips with the gateway (up to 25 characters).
*
* @var int|string|null
*/
private $nonce;
/**
* Override the default username of the webhook.
*
* @var string|null
*/
private $username;
/**
* Override the default avatar of the webhook.
*
* @var string|null
*/
private $avatar_url;
/**
* Whether the message is text-to-speech.
*
* @var bool
*/
private $tts = false;
/**
* Array of embeds to send with the message.
*
* @var array[]|null
*/
private $embeds;
/**
* Allowed mentions object for the message.
*
* @var array|null
*/
private $allowed_mentions;
/**
* Message to reply to with this message.
*
* @var Message|null
*/
private $replyTo;
/**
* Components to send with this message.
*
* @var Component[]|null
*/
private $components;
/**
* IDs of up to 3 stickers in the server to send in the message.
*
* @var string[]
*/
private $sticker_ids = [];
/**
* Files to send with this message.
*
* @var array[]|null
*/
private $files;
/**
* Attachments to send with this message.
*
* @var Attachment[]|null
*/
private $attachments;
/**
* Flags to send with this message.
*
* @var int|null
*/
private $flags;
/**
* Creates a new message builder.
*
* @return static
*/
public static function new(): self
{
return new static();
}
/**
* Sets the content of the message.
*
* @param string $content Content of the message. Maximum 2000 characters.
*
* @throws \LengthException
*
* @return $this
*/
public function setContent(string $content): self
{
if (poly_strlen($content) > 2000) {
throw new \LengthException('Message content must be less than or equal to 2000 characters.');
}
$this->content = $content;
return $this;
}
/**
* Sets the nonce of the message. Only used for sending message.
*
* @param int|string|null $nonce Nonce of the message.
*
* @throws \LengthException `$nonce` string exceeds 25 characters.
*
* @return $this
*/
public function setNonce($nonce = null): self
{
if (is_string($nonce) && poly_strlen($nonce) > 25) {
throw new \LengthException('Message nonce must be less than or equal to 25 characters.');
}
$this->nonce = $nonce;
return $this;
}
/**
* Override the default username of the webhook. Only used for executing webhook.
*
* @param string $username New webhook username.
*
* @throws \LengthException `$username` exceeds 80 characters.
*
* @return $this
*/
public function setUsername(string $username): self
{
if (poly_strlen($username) > 80) {
throw new \LengthException('Username can be only up to 80 characters.');
}
$this->username = $username;
return $this;
}
/**
* Override the default avatar URL of the webhook. Only used for executing webhook.
*
* @param string $avatar_url New webhook avatar URL.
*
* @return $this
*/
public function setAvatarUrl(string $avatar_url): self
{
$this->avatar_url = $avatar_url;
return $this;
}
/**
* Sets the TTS status of the message. Only used for sending message or executing webhook.
*
* @param bool $tts
*
* @return $this
*/
public function setTts(bool $tts = false): self
{
$this->tts = $tts;
return $this;
}
/**
* Returns the value of TTS of the builder.
*
* @return bool
*/
public function getTts(): bool
{
return $this->tts ?? false;
}
/**
* Adds an embed to the builder.
*
* @param Embed|array ...$embeds
*
* @throws \OverflowException Builder exceeds 10 embeds.
*
* @return $this
*/
public function addEmbed(...$embeds): self
{
foreach ($embeds as $embed) {
if ($embed instanceof Embed) {
$embed = $embed->getRawAttributes();
}
if (isset($this->embeds) && count($this->embeds) >= 10) {
throw new \OverflowException('You can only have 10 embeds per message.');
}
$this->embeds[] = $embed;
}
return $this;
}
/**
* Sets the embeds for the message. Clears the existing embeds in the process.
*
* @param Embed[]|array ...$embeds
*
* @return $this
*/
public function setEmbeds(array $embeds): self
{
$this->embeds = [];
return $this->addEmbed(...$embeds);
}
/**
* Sets the allowed mentions object of the message.
*
* @link https://discord.com/developers/docs/resources/channel#allowed-mentions-object
*
* @param array $allowed_mentions
*
* @return $this
*/
public function setAllowedMentions(array $allowed_mentions): self
{
$this->allowed_mentions = $allowed_mentions;
return $this;
}
/**
* Sets this message as a reply to another message. Only used for sending message.
*
* @param Message|null $message
*
* @return $this
*/
public function setReplyTo(?Message $message = null): self
{
$this->replyTo = $message;
return $this;
}
/**
* Adds a component to the builder.
*
* @param Component $component Component to add.
*
* @throws \InvalidArgumentException Component is not a type of `ActionRow` or `SelectMenu`
* @throws \OverflowException Builder exceeds 5 components.
*
* @return $this
*/
public function addComponent(Component $component): self
{
if (! ($component instanceof ActionRow || $component instanceof SelectMenu)) {
throw new \InvalidArgumentException('You can only add action rows and select menus as components to messages. Put your other components inside an action row.');
}
if (isset($this->components) && count($this->components) >= 5) {
throw new \OverflowException('You can only add 5 components to a message');
}
$this->components[] = $component;
return $this;
}
/**
* Removes a component from the builder.
*
* @param Component $component Component to remove.
*
* @return $this
*/
public function removeComponent(Component $component): self
{
if (($idx = array_search($component, $this->components)) !== null) {
array_splice($this->components, $idx, 1);
}
return $this;
}
/**
* Sets the components of the message. Removes the existing components in the process.
*
* @param array $components New message components.
*
* @return $this
*/
public function setComponents(array $components): self
{
$this->components = [];
foreach ($components as $component) {
$this->addComponent($component);
}
return $this;
}
/**
* Returns all the components in the builder.
*
* @return Component[]
*/
public function getComponents(): array
{
return $this->components;
}
/**
* Adds a sticker to the builder. Only used for sending message or creating forum thread.
*
* @param string|Sticker $sticker Sticker to add.
*
* @throws \OverflowException Builder exceeds 3 stickers.
*
* @return $this
*/
public function addSticker($sticker): self
{
if (count($this->sticker_ids) >= 3) {
throw new \OverflowException('You can only add 3 stickers to a message');
}
if ($sticker instanceof Sticker) {
$sticker = $sticker->id;
}
$this->sticker_ids[] = $sticker;
return $this;
}
/**
* Removes a sticker from the builder.
*
* @param string|Sticker $sticker Sticker to remove.
*
* @return $this
*/
public function removeSticker($sticker): self
{
if ($sticker instanceof Sticker) {
$sticker = $sticker->id;
}
if (($idx = array_search($sticker, $this->sticker_ids)) !== null) {
array_splice($this->sticker_ids, $idx, 1);
}
return $this;
}
/**
* Sets the stickers of the builder. Removes the existing stickers in the process.
*
* @param array $stickers New sticker ids.
*
* @return $this
*/
public function setStickers(array $stickers): self
{
$this->sticker_ids = [];
foreach ($stickers as $sticker) {
$this->addSticker($sticker);
}
return $this;
}
/**
* Returns all the sticker ids in the builder.
*
* @return string[]
*/
public function getStickers(): array
{
return $this->sticker_ids;
}
/**
* Adds a file attachment to the builder.
*
* Note this is a synchronous function which uses `file_get_contents` and therefore
* should not be used when requesting files from an online resource. Fetch the content
* asynchronously and use the `addFileFromContent` function for tasks like these.
*
* @param string $filepath Path to the file to send.
* @param string|null $filename Name to send the file as. `null` for the base name of `$filepath`.
*
* @return $this
*/
public function addFile(string $filepath, ?string $filename = null): self
{
if (! file_exists($filepath)) {
throw new FileNotFoundException("File does not exist at path {$filepath}.");
}
return $this->addFileFromContent($filename ?? basename($filepath), file_get_contents($filepath));
}
/**
* Adds a file attachment to the builder with a given filename and content.
*
* @param string $filename Name to send the file as.
* @param string $content Content of the file.
*
* @return $this
*/
public function addFileFromContent(string $filename, string $content): self
{
$this->files[] = [$filename, $content];
return $this;
}
/**
* Returns the number of files attached to the builder.
*
* @return int
*/
public function numFiles(): int
{
if (! isset($this->files)) {
return 0;
}
return count($this->files);
}
/**
* Removes all files from the builder.
*
* @return $this
*/
public function clearFiles(): self
{
$this->files = [];
return $this;
}
/**
* Adds attachment(s) to the builder.
*
* @param Attachment|string|int ...$attachments Attachment objects or IDs to add
*
* @return $this
*/
public function addAttachment(...$attachments): self
{
foreach ($attachments as $attachment) {
if ($attachment instanceof Attachment) {
$attachment = $attachment->getRawAttributes();
} else {
$attachment = ['id' => $attachment];
}
$this->attachments[] = $attachment;
}
return $this;
}
/**
* Returns all the attachments in the builder.
*
* @return Attachment[]
*/
public function getAttachments(): array
{
return $this->attachments;
}
/**
* Removes all attachments from the message.
*
* @return $this
*/
public function clearAttachments(): self
{
$this->attachments = [];
return $this;
}
/**
* Sets the flags of the message.
* Only works for some message types and some message flags.
*
* @param int $flags
*
* @since 10.0.0
*
* @return $this
*/
public function setFlags(int $flags): self
{
$this->flags = $flags;
return $this;
}
/**
* @deprecated 10.0.0 Use MessageBuilder::setFlags()
*/
public function _setFlags(int $flags): self
{
return $this->setFlags($flags);
}
/**
* Returns a boolean that determines whether the message needs to
* be sent via multipart request, i.e. contains files.
*
* @return bool
*/
public function requiresMultipart(): bool
{
return isset($this->files);
}
/**
* Converts the request to a multipart request.
*
* @internal
*
* @param bool $payload Whether to include the JSON payload in the response.
*
* @return Multipart
*/
public function toMultipart(bool $payload = true): Multipart
{
$fields = [];
if ($payload) {
$fields = [
[
'name' => 'payload_json',
'content' => json_encode($this),
'headers' => [
'Content-Type' => 'application/json',
],
],
];
}
foreach ($this->files as $idx => [$filename, $content]) {
$fields[] = [
'name' => 'file'.$idx,
'content' => $content,
'filename' => $filename,
];
}
return new Multipart($fields);
}
/**
* {@inheritDoc}
*/
public function jsonSerialize(): array
{
$empty = true;
if (! empty($this->files)) {
$body = [];
$empty = false;
}
if (isset($this->content)) {
$body['content'] = $this->content;
$empty = false;
}
if (isset($this->username)) {
$body['username'] = $this->username;
}
if (isset($this->avatar_url)) {
$body['avatar_url'] = $this->avatar_url;
}
if ($this->nonce !== null) {
$body['nonce'] = $this->nonce;
}
if ($this->tts) {
$body['tts'] = true;
}
if (isset($this->embeds)) {
$body['embeds'] = $this->embeds;
$empty = false;
}
if (isset($this->allowed_mentions)) {
$body['allowed_mentions'] = $this->allowed_mentions;
}
if ($this->replyTo) {
$body['message_reference'] = [
'message_id' => $this->replyTo->id,
'channel_id' => $this->replyTo->channel_id,
];
}
if (isset($this->components)) {
$body['components'] = $this->components;
$empty = false;
}
if ($this->sticker_ids) {
$body['sticker_ids'] = $this->sticker_ids;
$empty = false;
}
if (isset($this->attachments)) {
$body['attachments'] = $this->attachments;
$empty = false;
}
if (isset($this->flags)) {
$body['flags'] = $this->flags;
} elseif ($empty) {
throw new RequestFailedException('You cannot send an empty message. Set the content or add an embed or file.');
}
return $body;
}
}