Skip to content

Commit 43218f2

Browse files
authored
Merge pull request #3 from m7rlin/dev
Dev
2 parents 7b89437 + 79460ca commit 43218f2

File tree

1 file changed

+120
-32
lines changed

1 file changed

+120
-32
lines changed

DiscordWebhook.php

+120-32
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
11
<?php
22
/**
33
* Discord Webhook
4-
* v1.0.0
5-
* Updated: 20.01.2019
4+
* v1.1.0
5+
* Updated: 18.07.2019
66
* www.magictm.com
77
* (c) Marcin Stawowczyk 2019
88
* License: MIT
99
*/
10+
11+
1012
final class DiscordWebhook
1113
{
12-
private $msg;
14+
private $message;
1315
private $url;
14-
private $username;
16+
private $username = "www.magictm.com";
1517
private $avatar;
18+
private $embed = false;
19+
private $embeds;
1620
private $tts;
21+
22+
private $file;
23+
24+
private $errors = [];
25+
1726
/**
18-
* @param string $msg message to send
1927
* @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
2328
*/
2429
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
3031
)
3132
{
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;
4634
}
4735

48-
public function send(): void
36+
public function send(): ?self
4937
{
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+
5147
$curl = curl_init();
5248
//timeouts - 5 seconds
5349
curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 5 seconds
@@ -59,21 +55,113 @@ public function send(): void
5955
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
6056
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
6157
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
62-
'content' => $this->msg,
58+
'content' => $this->message,
6359
'username' => $this->username,
6460
'avatar_url' => $this->avatar,
65-
'tts' => $this->tts
61+
'tts' => $this->tts,
62+
'file' => $this->file,
63+
'embeds' => $this->embeds
6664
]));
6765
$output = json_decode(
6866
curl_exec($curl),
6967
true
7068
);
7169
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 204) {
7270
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);
7482
}
7583
curl_close($curl);
84+
85+
return $this;
7686
}
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+
77165
}
78166

79167
?>

0 commit comments

Comments
 (0)