diff --git a/README.md b/README.md index d305677..a9eea2c 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,19 @@ 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. +Be aware, if you call `Message->getAllowedMentions()` you will get a new instance of the `AllowedMentions` class, which will allowing all mentions passing through. +```php +$msg->getAllowedMentions()->addUser($userId1, $userId2); // Only the two users corresponding with these two ids will be mentioned +$msg->getAllowedMentions()->addRole($roleId1, $roleId2); // Now also all the people with $roleId1 and $roleId2 will be mentioned +``` + +But if you want to suppress 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 @@ -71,6 +84,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/CortexPE/DiscordWebhookAPI/AllowedMentions.php b/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php new file mode 100644 index 0000000..9d995c3 --- /dev/null +++ b/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php @@ -0,0 +1,116 @@ +. + * + * Written by @CortexPE + * Intended for use on SynicadeNetwork + */ + +declare(strict_types = 1); + +namespace CortexPE\DiscordWebhookAPI; + +use function count; +use function in_array; + +class AllowedMentions implements \JsonSerializable { + /** @var bool */ + private $parseUsers = true, $parseRoles = true, $mentionEveryone = true, $suppressAll = false; + + /** @var array */ + private $roles = []; + + /** @var array */ + private $users = []; + + /** + * If following role is given into the messages content, every user of it will be mentioned + * @param string ...$roleID + */ + public function addRole(string ...$roleID): void { + foreach ($roleID as $item) { + if (in_array($item, $this->roles, true)) { + continue; + } + + $this->roles[] = $item; + } + $this->parseRoles = false; + } + + /** + * If following user is given into the messages content, the user will be mentioned + * @param string ...$userID + */ + public function addUser(string ...$userID): void { + foreach ($userID as $item) { + if (in_array($item, $this->users, true)) { + continue; + } + + $this->users[] = $item; + } + + $this->parseUsers = false; + } + + /** + * If the message content has whether everyone or here and $mention is set to false, the users won't be mentioned + * @param bool $mention + */ + public function mentionEveryone(bool $mention): void { + $this->mentionEveryone = $mention; + } + + /** + * If this function is called no mention will be getting showed for anyone + */ + public function suppressAll(): void { + $this->suppressAll = true; + } + + public function jsonSerialize(): array { + if ($this->suppressAll) { + return [ + "parse" => [] + ]; + } + + $data = ["parse" => []]; + if ($this->mentionEveryone) { + $data["parse"][] = "everyone"; + } + + if (count($this->users) !== 0) { + $data["users"] = $this->users; + } else if ($this->parseUsers) { + $data["parse"][] = "users"; + } + + if (count($this->roles) !== 0) { + $data["roles"] = $this->roles; + } else if ($this->parseRoles) { + $data["parse"][] = "roles"; + } + + return $data; + } +} diff --git a/src/CortexPE/DiscordWebhookAPI/Message.php b/src/CortexPE/DiscordWebhookAPI/Message.php index f2c66f1..4c55d4f 100644 --- a/src/CortexPE/DiscordWebhookAPI/Message.php +++ b/src/CortexPE/DiscordWebhookAPI/Message.php @@ -66,6 +66,14 @@ public function addEmbed(Embed $embed):void{ public function setTextToSpeech(bool $ttsEnabled):void{ $this->data["tts"] = $ttsEnabled; } + + 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(){ return $this->data;