From c01392d59d03514cb4f890f6dd7cc239d69a26f0 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 5 Aug 2021 15:53:03 +0200 Subject: [PATCH 1/6] feature: Message->setAllowedMentions --- src/CortexPE/DiscordWebhookAPI/Message.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/CortexPE/DiscordWebhookAPI/Message.php b/src/CortexPE/DiscordWebhookAPI/Message.php index f2c66f1..3182c55 100644 --- a/src/CortexPE/DiscordWebhookAPI/Message.php +++ b/src/CortexPE/DiscordWebhookAPI/Message.php @@ -66,6 +66,10 @@ public function addEmbed(Embed $embed):void{ public function setTextToSpeech(bool $ttsEnabled):void{ $this->data["tts"] = $ttsEnabled; } + + public function setAllowedMentions(bool $roles = true, bool $users = true, bool $everyone = true): void { + $this->data["allowed_mentions"] = ["roles" => $roles, "users" => $users, "everyone" => $everyone]; + } public function jsonSerialize(){ return $this->data; From b8f1160d3320a4933bd0600806e3e64d9b67ccc8 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 20 Aug 2021 16:40:04 +0200 Subject: [PATCH 2/6] feature: AllowedMentions --- .../DiscordWebhookAPI/AllowedMentions.php | 118 ++++++++++++++++++ src/CortexPE/DiscordWebhookAPI/Message.php | 8 +- 2 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/CortexPE/DiscordWebhookAPI/AllowedMentions.php diff --git a/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php b/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php new file mode 100644 index 0000000..d9675ae --- /dev/null +++ b/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php @@ -0,0 +1,118 @@ +. + * + * 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; + } +} \ No newline at end of file diff --git a/src/CortexPE/DiscordWebhookAPI/Message.php b/src/CortexPE/DiscordWebhookAPI/Message.php index 3182c55..4c55d4f 100644 --- a/src/CortexPE/DiscordWebhookAPI/Message.php +++ b/src/CortexPE/DiscordWebhookAPI/Message.php @@ -67,8 +67,12 @@ public function setTextToSpeech(bool $ttsEnabled):void{ $this->data["tts"] = $ttsEnabled; } - public function setAllowedMentions(bool $roles = true, bool $users = true, bool $everyone = true): void { - $this->data["allowed_mentions"] = ["roles" => $roles, "users" => $users, "everyone" => $everyone]; + 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(){ From 683fc02b8beaa177429a724350e597adc48ebc7b Mon Sep 17 00:00:00 2001 From: marshall Date: Sun, 24 Oct 2021 00:36:31 +0800 Subject: [PATCH 3/6] code style --- src/CortexPE/DiscordWebhookAPI/AllowedMentions.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php b/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php index d9675ae..9d995c3 100644 --- a/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php +++ b/src/CortexPE/DiscordWebhookAPI/AllowedMentions.php @@ -31,8 +31,7 @@ use function count; use function in_array; -class AllowedMentions implements \JsonSerializable -{ +class AllowedMentions implements \JsonSerializable { /** @var bool */ private $parseUsers = true, $parseRoles = true, $mentionEveryone = true, $suppressAll = false; @@ -88,8 +87,7 @@ public function suppressAll(): void { $this->suppressAll = true; } - public function jsonSerialize(): array - { + public function jsonSerialize(): array { if ($this->suppressAll) { return [ "parse" => [] @@ -115,4 +113,4 @@ public function jsonSerialize(): array return $data; } -} \ No newline at end of file +} From b05f29cb0669876b5f39ae639418b04089fb0625 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 16 Dec 2021 22:25:02 +0100 Subject: [PATCH 4/6] Update(AllowedMentions): Adding simple sample code sample code --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index d305677..8dff22c 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,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()->supressAll(); +``` + **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 From e6c4bbb74305ef8444d8644bb0be06e9276e04aa Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 16 Dec 2021 22:26:59 +0100 Subject: [PATCH 5/6] Fix(AllowedMentions): Wrong method name + whole sample code --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8dff22c..b908522 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ $msg->getAllowedMentions()->addRole($roleId1, $roleId2); // Now also all the peo But if you want to supress every mention out of that message you can use following method. ```php -$msg->getAllowedMentions()->supressAll(); +$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** @@ -83,6 +83,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(); From a75a1d92852d066db04be6cfd85813ffbcf0a7ac Mon Sep 17 00:00:00 2001 From: martin Date: Sun, 7 May 2023 21:35:08 +0200 Subject: [PATCH 6/6] Fixing typos --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b908522..a9eea2c 100644 --- a/README.md +++ b/README.md @@ -60,13 +60,14 @@ Now that the embed has been constructed and has a valid content, we will have to $msg->addEmbed($embed); ``` -You can even enable specific mentions, using the following statement: `Message->getAllowedMentions`. The only things you need are discord snowflakes/ids. +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 mentionend +$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 supress every mention out of that message you can use following method. +But if you want to suppress every mention out of that message you can use following method. ```php $msg->getAllowedMentions()->suppressAll(); ```