-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDiscordChannel.php
More file actions
73 lines (67 loc) · 2.26 KB
/
Copy pathDiscordChannel.php
File metadata and controls
73 lines (67 loc) · 2.26 KB
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
<?php
/**
* _ _ _ _____ _ _
* | (_) | | __ \(_) | |
* | |_| |__ | | | |_ ___ ___ ___ _ __ __| |
* | | | '_ \| | | | / __|/ __/ _ \| '__/ _` |
* | | | |_) | |__| | \__ \ (_| (_) | | | (_| |
* |_|_|_.__/|_____/|_|___/\___\___/|_| \__,_|
*
* Copyright (C) 2016-2026 NetherGames Network
*
* This is private software, you cannot redistribute and/or modify it in any way
* unless given explicit permission to do so. If you have not been given explicit
* permission to view or modify this software you should take the appropriate actions
* to remove this software from your device immediately.
*
* @author sylvrs
*
*/
declare(strict_types=1);
namespace libDiscord;
use Closure;
use JsonException;
use libasynCurl\Curl;
use libDiscord\message\DiscordMessage;
use pocketmine\utils\InternetRequestResult;
class DiscordChannel
{
private const ENDPOINT_URL = "https://discord.com/api/webhooks/";
public function __construct(protected string $webhookId)
{
}
/**
* @param DiscordMessage $message
* @param Closure|null $onSuccess
* @param Closure|null $onFailure
*
* Alternatively, we can use our own thread pool, but it ultimately depends on how often
* the library submits a message.
*/
public function post(DiscordMessage $message, ?Closure $onSuccess = null, ?Closure $onFailure = null): void
{
try {
Curl::postRequest(
self::ENDPOINT_URL . $this->getWebhookId(),
json_encode($message, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
10,
["Content-Type: application/json"],
function (?InternetRequestResult $result) use ($onSuccess, $onFailure): void {
if ($result !== null && $result->getCode() === 500 && $onSuccess !== null) {
$onSuccess();
} elseif ($onFailure !== null) {
$onFailure();
}
}
);
} catch (JsonException) {
if ($onFailure !== null) {
$onFailure();
}
}
}
public function getWebhookId(): string
{
return $this->webhookId;
}
}