diff --git a/README.md b/README.md index 420cc45..b2ccb05 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,18 @@ Now that the embed has been constructed and has a valid content, we will have to ```php $msg->addEmbed($embed); ``` + +You can even enable specific mentions, using the following statement: `Message->getAllowedMentions`. The only things you need are discord snowflakes/ids. +```php +$msg->getAllowedMentions()->addUser($userId1, $userId2); // Only the two users corresponding with these two ids will be mentionend +$msg->getAllowedMentions()->addRole($roleId1, $roleId2); // Now also all the people with $roleId1 and $roleId2 will be mentioned +``` + +But if you want to supress every mention out of that message you can use following method. +```php +$msg->getAllowedMentions()->suppressAll(); +``` + **That's all for the Basic Usage of the API. To learn more, You can explore it by reading the API's source code yourself (the code is simple and explanatory) or by using your favorite IDE to index it yourself. :3** # Sample Code used to test this API earlier: ```php @@ -73,6 +85,7 @@ $msg = new Message(); $msg->setUsername("USERNAME"); $msg->setAvatarURL("https://cortexpe.xyz/utils/kitsu.png"); $msg->setContent("INSERT TEXT HERE"); +$msg->suppressAll(); // Create an embed object with #FF0000 (RED) as the embed's color and "EMBED 1" as the title $embed = new Embed(); diff --git a/src/supercrafter333/DiscordWebhooksX/Embed.php b/src/supercrafter333/DiscordWebhooksX/Embed.php index f90b61d..b4117f7 100644 --- a/src/supercrafter333/DiscordWebhooksX/Embed.php +++ b/src/supercrafter333/DiscordWebhooksX/Embed.php @@ -31,8 +31,8 @@ class Embed { - /** @var array */ - protected $data = []; + + protected array $data = []; public function asArray(): array { diff --git a/src/supercrafter333/DiscordWebhooksX/Message.php b/src/supercrafter333/DiscordWebhooksX/Message.php index 8bc6792..8a63a89 100644 --- a/src/supercrafter333/DiscordWebhooksX/Message.php +++ b/src/supercrafter333/DiscordWebhooksX/Message.php @@ -29,10 +29,12 @@ namespace supercrafter333\DiscordWebhooksX; +use CortexPE\DiscordWebhookAPI\AllowedMentions; + class Message implements \JsonSerializable { - /** @var array */ - protected $data = []; + + protected array $data = []; public function __construct(array $embeds = null) { @@ -91,7 +93,16 @@ public function setTextToSpeech(bool $ttsEnabled): self return $this; } - public function jsonSerialize() + public function getAllowedMentions(): AllowedMentions + { + if (array_key_exists("allowed_mentions", $this->data)) { + return $this->data["allowed_mentions"]; + } + + return $this->data["allowed_mentions"] = new AllowedMentions(); + } + + public function jsonSerialize(): array { return $this->data; } diff --git a/src/supercrafter333/DiscordWebhooksX/Webhook.php b/src/supercrafter333/DiscordWebhooksX/Webhook.php index ef7f1b8..c55b3cc 100644 --- a/src/supercrafter333/DiscordWebhooksX/Webhook.php +++ b/src/supercrafter333/DiscordWebhooksX/Webhook.php @@ -32,23 +32,23 @@ use supercrafter333\DiscordWebhooksX\task\DiscordWebhookSendTask; use pocketmine\Server; -class Webhook { - /** @var string */ - protected $url; +class Webhook +{ - public function __construct(string $url){ - $this->url = $url; - } + public function __construct(protected readonly string $url) {} - public function getURL(): string{ - return $this->url; - } + public function getURL(): string + { + return $this->url; + } - public function isValid(): bool{ - return filter_var($this->url, FILTER_VALIDATE_URL) !== false; - } + public function isValid(): bool + { + return filter_var($this->url, FILTER_VALIDATE_URL) !== false; + } - public function send(Message $message): void{ - Server::getInstance()->getAsyncPool()->submitTask(new DiscordWebhookSendTask($this, $message)); - } + public function send(Message $message): void + { + Server::getInstance()->getAsyncPool()->submitTask(new DiscordWebhookSendTask($this, $message)); + } } diff --git a/src/supercrafter333/DiscordWebhooksX/task/DiscordWebhookSendTask.php b/src/supercrafter333/DiscordWebhooksX/task/DiscordWebhookSendTask.php index 2c15ffc..cb3b132 100644 --- a/src/supercrafter333/DiscordWebhooksX/task/DiscordWebhookSendTask.php +++ b/src/supercrafter333/DiscordWebhooksX/task/DiscordWebhookSendTask.php @@ -34,33 +34,29 @@ use pocketmine\scheduler\AsyncTask; use pocketmine\Server; -class DiscordWebhookSendTask extends AsyncTask { - /** @var Webhook */ - protected $webhook; - /** @var Message */ - protected $message; - - public function __construct(Webhook $webhook, Message $message){ - $this->webhook = $webhook; - $this->message = $message; - } - - public function onRun():void{ - $ch = curl_init($this->webhook->getURL()); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->message)); - curl_setopt($ch, CURLOPT_POST,true); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]); - $this->setResult([curl_exec($ch), curl_getinfo($ch, CURLINFO_RESPONSE_CODE)]); - curl_close($ch); - } - - public function onCompletion():void{ - $response = $this->getResult(); - if(!in_array($response[1], [200, 204])){ - Server::getInstance()->getLogger()->error("[DiscordWebhooksX] Got error ({$response[1]}): " . $response[0]); - } - } +class DiscordWebhookSendTask extends AsyncTask +{ + + public function __construct(protected readonly Webhook $webhook, protected readonly Message $message) {} + + public function onRun():void + { + $ch = curl_init($this->webhook->getURL()); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->message)); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]); + $this->setResult([curl_exec($ch), curl_getinfo($ch, CURLINFO_RESPONSE_CODE)]); + curl_close($ch); + } + + public function onCompletion():void + { + $response = $this->getResult(); + if (!in_array($response[1], [200, 204])) { + Server::getInstance()->getLogger()->error("[DiscordWebhooksX] Got error ({$response[1]}): " . $response[0]); + } + } } diff --git a/virion.yml b/virion.yml index 36fd8e1..e2f2812 100644 --- a/virion.yml +++ b/virion.yml @@ -1,5 +1,5 @@ name: DiscordWebhooksX antigen: supercrafter333\DiscordWebhooksX -api: 4.0.0 -version: 1.0.0 +api: [4.0.0, 5.0.0] +version: 1.2.0 authors: ["CortexPE", "supercrafter333"]