-
Notifications
You must be signed in to change notification settings - Fork 7
/
DiscordWebhook.php
563 lines (492 loc) · 15.4 KB
/
DiscordWebhook.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
<?php
class DiscordWebhook {
# DiscordWebhook-PHP
# github.com/renzbobz
# 2/9/23
public $webhook = null;
public $parseJSON = true;
public $curlOpts = [];
//? Query string params
public $wait = false;
public $threadId = null;
//? Message props
public $content = null;
public $username = null;
public $avatarUrl = null;
public $tts = false;
public $embeds = [];
public $allowedMentions = [];
public $attachments = [];
public $flags = null;
public $threadName = null;
public $files = [];
public $embed = [];
public $messageId = null;
private $_offsetIndex = 0;
public function __construct($webhook, $opts=[]) {
$opts = $webhook && !$opts ? $webhook : [ "webhook" => $webhook, ...$opts ];
$this->_setOpts($opts);
}
public function toJSON() { return $this->_getData(); }
//! Helpers
private function _setOpts($opts) {
if (is_string($opts)) {
$this->webhook = $opts;
} else if (is_array($opts)) {
foreach ($opts as $key => $val) {
$this->{$key} = $val;
}
}
}
private function _resolveColor($clr) {
if (is_string($clr)) {
if ($clr == 'random') return rand(0x000000, 0xFFFFFF);
if ($clr[0] == '#') $clr = substr($clr, 1);
if (preg_match('/,/', $clr)) $clr = sprintf('%02x%02x%02x', ...explode(',', $clr));
$clr = hexdec($clr);
}
return $clr;
}
private function _setPropArrayValue($prop, $firstArg, $structuredVal) {
if (is_array($firstArg)) {
$this->{$prop} = $firstArg;
} else {
$this->{$prop} = $structuredVal;
}
}
private function _silentJSONParse($str) {
$json = json_decode($str, true);
if (json_last_error() != JSON_ERROR_NONE) {
return $str;
}
return $json;
}
private function _arrayAdderHandler($name, $values) {
foreach ($values as $value) {
if (!$value) continue;
// indexed array
if (isset($value[0])) {
$this->{$name}(...$value);
// associative array
} else {
$this->{$name}($value);
}
}
}
private function _getData() {
$embed = $this->embed;
$embeds = $this->embeds;
if ($embed || $embeds) {
// sort embeds by key first before adding the main embed so it doesn't change the order
ksort($embeds);
if ($embed) array_splice($embeds, $this->_offsetIndex, 0, [$embed]);
}
$data = [
"content" => $this->content,
"username" => $this->username,
"avatar_url" => $this->avatarUrl,
"tts" => $this->tts,
"flags" => $this->flags,
"embeds" => $embeds,
"attachments" => $this->attachments,
"thread_name" => $this->threadName,
];
if ($this->allowedMentions) $data["allowed_mentions"] = $this->allowedMentions;
return json_encode($data);
}
private function _getFormData() {
$files = [];
foreach ($this->files as $id => $file) {
$snowflake = $this->_getFileSnowflakeById($id);
$files["files[$snowflake]"] = curl_file_create($file["path"], $file["type"], $file["name"]);
}
$data = [
"payload_json" => $this->_getData(),
] + $files;
return $data;
}
private function _setEmbed($key, $value, $append=false) {
return $append ? $this->embed[$key][] = $value : $this->embed[$key] = $value;
}
private function _getEmbed($key) {
return $key ? $this->embed[$key] : $this->embed;
}
private function _setEmbedArrayValue($key, $firstArg, $structuredVal) {
return $this->_setEmbed($key, is_array($firstArg) ? $firstArg : $structuredVal);
}
private function _getFileSnowflakeById($id) {
if (isset($this->files[$id])) {
return $this->files[$id]["snowflake"];
} else {
throw new Exception("No file found with the id of ($id). Make sure you add the file first with the id of ($id).");
}
}
private function _getWebhookMsgUrl($messageId=null) {
$messageId = $messageId ?? $this->_getMessageId();
$webhook = $this->_buildWebhookQuery($this->_getWebhook().'\/messages\/'.$messageId);
return $webhook;
}
private function _getMessageId() {
$messageId = $this->messageId;
if (!$messageId) throw new Exception("Message Id is Required.");
return $messageId;
}
private function _getPostCurlOpts() {
if ($this->files) {
$contentType = 'multipart/form-data';
$data = $this->_getFormData();
} else {
$contentType = "application/json";
$data = $this->_getData();
}
return [
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => [
'Content-type: '.$contentType
]
];
}
private function _getWebhook() {
$webhook = $this->webhook;
if (!$webhook) throw new Exception("Webhook is Required.");
return $webhook;
}
private function _buildWebhookQuery($webhook) {
$query = [];
if ($this->wait) $query['wait'] = true;
if ($this->threadId) $query['thread_id'] = $this->threadId;
if ($query) $webhook .= '?' . http_build_query($query);
return $webhook;
}
private function _buildCurlOpts($opts=[]) {
$defaultOpts = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
];
return $defaultOpts + $opts + $this->curlOpts;
}
private function _curlResHandler($ch, $body) {
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$success = $code >= 200 && $code < 299;
$curlErr = curl_error($ch);
$res = [
'success' => $success,
'code' => $code,
'curl_error' => $curlErr,
];
$parsedBody = $this->_silentJSONParse($body);
$res['body'] = $this->parseJSON ? $parsedBody : $body;
if ($success && isset($parsedBody['id'])) $this->messageId = $parsedBody['id'];
return (object) $res;
}
private function _execCurl($url, $opts=[]) {
$ch = curl_init($url);
curl_setopt_array($ch, $opts);
$body = curl_exec($ch);
curl_close($ch);
return $this->_curlResHandler($ch, $body);
}
public function setWebhook($webhook) {
$this->webhook = $webhook;
return $this;
}
# Bot profile
public function setUsername($username) {
$this->username = $username;
return $this;
}
public function setAvatar($avatarUrl) {
$this->avatarUrl = $avatarUrl;
return $this;
}
# Query string params
public function setThreadId($id) {
$this->threadId = $id;
return $this;
}
public function waitMessage($wait) {
$this->wait = $wait;
return $this;
}
# Clone
public function newMessage($message=null) {
$cloned = clone $this;
if ($message) $cloned->setContent($message);
return $cloned;
}
public function newThread($name=null) {
$cloned = $this->newMessage();
$cloned->threadName = $name;
return $cloned;
}
# Msg content
public function setContent($content) {
$this->content = $content;
return $this;
}
public function prependContent($content) {
$this->content = $content . $this->content;
return $this;
}
public function appendContent($content) {
$this->content .= $content;
return $this;
}
# Msg content text to speech
public function setTts($tts=true) {
$this->tts = $tts;
return $this;
}
# Embed title
public function setTitle($title, $url=null) {
$this->_setEmbed("title", $title);
if ($url) $this->setUrl($url);
return $this;
}
public function prependTitle($title) {
$this->_setEmbed("title", $title . $this->_getEmbed("title"));
return $this;
}
public function appendTitle($title) {
$this->_setEmbed("title", $this->_getEmbed("title") . $title);
return $this;
}
# Embed url
public function setUrl($url) {
$this->_setEmbed("url", $url);
return $this;
}
# Embed description
public function setDescription($desc) {
$this->_setEmbed("description", $desc);
return $this;
}
public function prependDescription($desc) {
$this->_setEmbed("description", $desc . $this->_getEmbed("description"));
return $this;
}
public function appendDescription($desc) {
$this->_setEmbed("description", $this->_getEmbed("description") . $desc);
return $this;
}
# Embed color
public function setColor($clr) {
$this->_setEmbed("color", $this->_resolveColor($clr));
return $this;
}
public function setRandomColor() {
$this->_setEmbed("color", $this->_resolveColor("random"));
return $this;
}
# Embed timestamp
public function setTimestamp($ts=null) {
if (!$ts) $ts = date('c');
$this->_setEmbed("timestamp", $ts);
return $this;
}
# Embed author
public function setAuthor($name, $url=null, $iconUrl=null, $proxyIconUrl=null) {
$this->_setEmbedArrayValue("author", $name, [
"name" => $name,
"url" => $url,
"icon_url" => $iconUrl,
"proxy_icon_url" => $proxyIconUrl,
]);
return $this;
}
# Embed thumbnail
public function setThumbnail($url, $proxyUrl=null, $height=null, $width=null) {
$this->_setEmbedArrayValue("thumbnail", $url, [
"url" => $url,
"proxy_url" => $proxyUrl,
"height" => $height,
"width" => $width,
]);
return $this;
}
# Embed image
public function setImage($url, $proxyUrl=null, $height=null, $width=null) {
$this->_setEmbedArrayValue("image", $url, [
"url" => $url,
"proxy_url" => $proxyUrl,
"height" => $height,
"width" => $width,
]);
return $this;
}
# Embed footer
public function setFooter($text, $iconUrl=null, $proxyIconUrl=null) {
$this->_setEmbedArrayValue("footer", $text, [
"text" => $text,
"icon_url" => $iconUrl,
"proxy_icon_url" => $proxyIconUrl
]);
return $this;
}
# Embed fields
public function addField($name, $value=null, $inline=false) {
$this->embed["fields"][] = is_array($name) ? $name : [
"name" => $name,
"value" => $value,
"inline" => $inline
];
return $this;
}
public function addFields(...$fields) {
$this->_arrayAdderHandler("addField", $fields);
return $this;
}
# Message files
public function addFile($id, $path=null, $name=null, $type=null) {
$snowflake = count($this->files);
if (is_array($id)) {
$this->files[$id["id"]] = [
...$id,
"snowflake" => $snowflake,
];
} else {
$this->files[$id] = [
"path" => $path,
"name" => $name,
"type" => $type,
"snowflake" => $snowflake,
];
}
return $this;
}
public function addFiles(...$files) {
$this->_arrayAdderHandler("addFile", $files);
return $this;
}
# Message attachments
public function addAttachment($id, $filename=null) {
if (is_array($id)) {
$attachment = $id;
// if not is set size, then it's not direct arg data
if (!isset($attachment["size"])) $attachment["id"] = $this->_getFileSnowflakeById($attachment["id"]);
} else {
$snowflake = $this->_getFileSnowflakeById($id);
$attachment = [
"id" => $snowflake,
"filename" => $filename,
];
}
$this->attachments[] = $attachment;
return $this;
}
public function addAttachments(...$attachments) {
$this->_arrayAdderHandler("addAttachment", $attachments);
return $this;
}
# Message flag
public function setFlag($flag) {
$this->flags = $flag;
return $this;
}
# Message allowed mentions
public function setAllowedMentions($data) {
$this->allowedMentions = $data;
return $this;
}
# Add Embed. Chain multiple embed
// $embed = DiscordWebhook | DiscordEmbed
public function addEmbed($embedClass, $indx=null, $replace=false) {
// DiscordWebhook->embed | DiscordEmbed->toArray()
$embed = property_exists($embedClass, "embed") ? $embedClass->embed : $embedClass->toArray();
if (is_null($indx)) {
// if embeds are still empty then assign this first embed to index 1, in respect to main embed,
if (!$this->embeds && $this->embed) return $this->addEmbed($embedClass, 1);
// otherwise append embed
$this->embeds[] = $embed;
} else {
// if embed wants to be the first, change main embed offset to 1 to move it
if ($indx == 0) $this->_offsetIndex = 1;
// if indx is already taken and not replace, insert and move
if (isset($this->embeds[$indx])) {
array_splice($this->embeds, $indx-1, $replace ? 1 : 0, [$embed]);
// insert embed based on index
} else {
$this->embeds[$indx] = $embed;
}
}
return $this;
}
public function setMessageId($messageId) {
$this->messageId = $messageId;
return $this;
}
public function getMessage($messageId=null, $copyMsg=true) {
$webhook = $this->_getWebhookMsgUrl($messageId);
$curlOpts = $this->_buildCurlOpts();
$res = $this->_execCurl($webhook, $curlOpts);
if ($res->success) {
[
'id' => $id,
'content' => $content,
'author' => $bot,
'embeds' => $embeds,
'content' => $content,
'flags' => $flag,
'tts' => $tts,
'attachments' => $attachments,
] = $res->body;
$clone = $this->newMessage();
$clone->setMessageId($id);
if ($copyMsg) {
$clone
->setContent($content)
->setUsername($bot['username'])
->setTts($tts)
->setFlag($flag);
$avatar = $bot['avatar'] ?? '';
$avatarUrl = 'https://cdn.discordapp.com/avatars/'.$bot["id"].'/'.$avatar.'.png';
if ($avatar) $clone->setAvatar($avatarUrl);
if ($attachments) $clone->addAttachments(...$attachments);
if ($embeds) {
$mainEmbed = array_splice($embeds, 0, 1);
// $i+1? index 0 is for main embed
foreach ($embeds as $i => $embed) $clone->embeds[$i+1] = $embed;
if ($mainEmbed) {
$e = $mainEmbed[0];
if (isset($e['url'])) $clone->setUrl($e['url']);
if (isset($e['title'])) $clone->setTitle($e['title']);
if (isset($e['description'])) $clone->setDescription($e['description']);
if (isset($e['color'])) $clone->setColor($e['color']);
if (isset($e['timestamp'])) $clone->setTimestamp($e['timestamp']);
if (isset($e['author'])) $clone->setAuthor($e['author']);
if (isset($e['image'])) $clone->setImage($e['image']);
if (isset($e['thumbnail'])) $clone->setThumbnail($e['thumbnail']);
if (isset($e['footer'])) $clone->setFooter($e['footer']);
if (isset($e['fields'])) $clone->addFields(...$e['fields']);
}
}
}
}
return $clone ?? null;
}
public function get($messageId=null) {
$webhook = $this->_getWebhookMsgUrl($messageId);
$curlOpts = $this->_buildCurlOpts();
return $this->_execCurl($webhook, $curlOpts);
}
public function delete($messageId=null) {
$webhook = $this->_getWebhookMsgUrl($messageId);
$curlOpts = $this->_buildCurlOpts([
CURLOPT_CUSTOMREQUEST => 'DELETE',
]);
return $this->_execCurl($webhook, $curlOpts);
}
public function update($messageId=null) {
$webhook = $this->_getWebhookMsgUrl($messageId);
$curlOpts = $this->_buildCurlOpts([ CURLOPT_CUSTOMREQUEST => 'PATCH' ] + $this->_getPostCurlOpts());
return $this->_execCurl($webhook, $curlOpts);
}
public function send($opts=null) {
$this->_setOpts($opts);
$webhook = $this->_buildWebhookQuery($this->_getWebhook());
if (!$this->content && !$this->embed && !$this->embeds && !$this->files) throw new Exception("You must provide a value for at least one of content, embeds, or files.");
$curlOpts = $this->_buildCurlOpts([ CURLOPT_POST => true ] + $this->_getPostCurlOpts());
return $this->_execCurl($webhook, $curlOpts);
}
}
?>