1
1
<?php
2
2
/**
3
3
* Discord Webhook
4
- * v1.0 .0
5
- * Updated: 20.01 .2019
4
+ * v1.1 .0
5
+ * Updated: 18.07 .2019
6
6
* www.magictm.com
7
7
* (c) Marcin Stawowczyk 2019
8
8
* License: MIT
9
9
*/
10
+
11
+
10
12
final class DiscordWebhook
11
13
{
12
- private $ msg ;
14
+ private $ message ;
13
15
private $ url ;
14
- private $ username ;
16
+ private $ username = " www.magictm.com " ;
15
17
private $ avatar ;
18
+ private $ embed = false ;
19
+ private $ embeds ;
16
20
private $ tts ;
21
+
22
+ private $ file ;
23
+
24
+ private $ errors = [];
25
+
17
26
/**
18
- * @param string $msg message to send
19
27
* @param string $url Discord Webhook URL
20
- * @param string|null $username username
21
- * @param string|null $avatar bot avatar
22
- * @param boolean $tts text-to-speech - the message will be readed
23
28
*/
24
29
public function __construct (
25
- string $ msg ,
26
- string $ url = null ,
27
- string $ username = null ,
28
- string $ avatar = null ,
29
- bool $ tts = null
30
+ string $ url = null
30
31
)
31
32
{
32
- $ this ->msg = $ msg ;
33
- $ this ->url = $ url ??
34
- 'https://discordapp.com/api/webhooks/536576089582075936/QkY4y6xDDlrPXXtnSXLiAOnIo961w6BXl1SLTE0bj-t0--A-P3thhos5tskW_lIhiRfG ' ;
35
- $ this ->username = $ username ?? 'www.magictm.com ' ;
36
- $ this ->avatar = $ avatar ??
37
- 'https://i.ytimg.com/vi/JrQkgLLL9XQ/hqdefault.jpg ' ;
38
- $ this ->tts = $ tts ?? false ;
39
- }
40
-
41
- public function tts (
42
- bool $ tts
43
- ): void
44
- {
45
- $ this ->tts = $ tts ;
33
+ $ this ->url = $ url ;
46
34
}
47
35
48
- public function send (): void
36
+ public function send (): ? self
49
37
{
50
- echo $ this ->tts ;
38
+
39
+ if (!$ this ->validate ()) {
40
+ $ errors = "" ;
41
+ foreach ($ this ->errors as $ key => $ value ) {
42
+ $ errors .= "$ value " ;
43
+ }
44
+ throw new Exception ("Zanim wyślesz wiadomość na Discorda popraw błędy: " . $ errors );
45
+ }
46
+
51
47
$ curl = curl_init ();
52
48
//timeouts - 5 seconds
53
49
curl_setopt ($ curl , CURLOPT_TIMEOUT , 5 ); // 5 seconds
@@ -59,21 +55,113 @@ public function send(): void
59
55
curl_setopt ($ curl , CURLOPT_SSL_VERIFYHOST , false );
60
56
curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
61
57
curl_setopt ($ curl , CURLOPT_POSTFIELDS , json_encode ([
62
- 'content ' => $ this ->msg ,
58
+ 'content ' => $ this ->message ,
63
59
'username ' => $ this ->username ,
64
60
'avatar_url ' => $ this ->avatar ,
65
- 'tts ' => $ this ->tts
61
+ 'tts ' => $ this ->tts ,
62
+ 'file ' => $ this ->file ,
63
+ 'embeds ' => $ this ->embeds
66
64
]));
67
65
$ output = json_decode (
68
66
curl_exec ($ curl ),
69
67
true
70
68
);
71
69
if (curl_getinfo ($ curl , CURLINFO_HTTP_CODE ) != 204 ) {
72
70
curl_close ($ curl );
73
- throw new Exception ("Wystąpił błąd podczas przesyłania wiadomości na Discorda: " . $ output ['message ' ]);
71
+ $ message = "" ;
72
+
73
+ if (!isset ($ output ["message " ])) {
74
+ foreach ($ output as $ key => $ value ) {
75
+ $ message .= ucfirst ($ key ) . " - " .$ value [0 ];
76
+ }
77
+ } else {
78
+ $ message = $ output ["message " ];
79
+ }
80
+
81
+ throw new Exception ("Wystąpił błąd podczas przesyłania wiadomości na Discorda: " . $ message );
74
82
}
75
83
curl_close ($ curl );
84
+
85
+ return $ this ;
76
86
}
87
+
88
+
89
+
90
+ function setMessage ($ message ): ?self {
91
+ $ this ->message = $ message ;
92
+ return $ this ;
93
+ }
94
+
95
+ function getMessage () {
96
+ return $ this ->message ;
97
+ }
98
+
99
+ function setURL (?string $ url ): ?self {
100
+ $ this ->url = $ url ;
101
+ return $ this ;
102
+ }
103
+
104
+ function getURL (): ?string {
105
+ return $ this ->url ;
106
+ }
107
+
108
+ function setUsername (?string $ username ): ?self {
109
+ $ this ->username = $ username ;
110
+ return $ this ;
111
+ }
112
+
113
+ function getUsername (): ?string {
114
+ return $ this ->username ;
115
+ }
116
+
117
+ function setAvatar (?string $ avatar ): ?self {
118
+ $ this ->avatar = $ avatar ;
119
+ return $ this ;
120
+ }
121
+
122
+ function getAvatar (): ?string {
123
+ return $ this ->avatar ;
124
+ }
125
+
126
+ function setFile (?object $ file ): ?self {
127
+ $ this ->file = curl_file_create ($ file ->getFile (), null , $ file ->getFileName ());
128
+ return $ this ;
129
+ }
130
+
131
+ function getFile (): ?string {
132
+ return $ this ->file ;
133
+ }
134
+
135
+ function setTts (?bool $ tts ): ?self {
136
+ $ this ->tts = $ tts ;
137
+ return $ this ;
138
+ }
139
+
140
+ function getTts (): ?bool {
141
+ return $ this ->tts ;
142
+ }
143
+
144
+ function reset (): ?self {
145
+ return $ this ;
146
+ }
147
+
148
+ private function validate (): bool {
149
+
150
+ if (!$ this ->url || $ this ->url === "" ) {
151
+ $ this ->errors ["url " ] = "URL is empty. " ;
152
+ }
153
+ if (!$ this ->message || $ this ->message === "" ) {
154
+ $ this ->errors ["message " ] = "Message is empty. " ;
155
+ } else if (strlen ($ this ->message ) >= 2000 ) {
156
+ $ this ->errors ["message " ] = "Message is too long (max. 2000 characters). " ;
157
+ }
158
+ if (empty ($ this ->errors )) {
159
+ return true ;
160
+ }
161
+ return false ;
162
+ }
163
+
164
+
77
165
}
78
166
79
167
?>
0 commit comments