DiscordWebhookAPI icon indicating copy to clipboard operation
DiscordWebhookAPI copied to clipboard

feature: Message->getAllowedMentions

Open packbier opened this issue 4 years ago • 13 comments

This function adds the allowed_mentions field from the Execute Webhook request (Message->setAllowedMentions) The function takes 3 parameters - Roles, Users and Everyone and all of them are set by default to false Resources:

  • https://discord.com/developers/docs/resources/webhook
  • https://discord.com/developers/docs/resources/channel#allowed-mentions-object

packbier avatar Aug 05 '21 13:08 packbier

Has this been tested? According to the documentation, this implementation is incorrect

the parse array key contains data types needing to be parsed by discord... This can be roles, users or everyone roles is list of role IDs as strings users is a list of user IDs as strings

The right way of implementing this is to make a new class called AllowedMentions, with the following methods: AllowedMentions->addRole(string $snowflakeID): void AllowedMentions->addUser(string $snowflakeID): void AllowedMentions->mentionEveryone(bool $mention): void

the parse array would be handled by that class, adding users, roles and everyone accordingly and the IDs would be added to specific arrays by that class as well...

make sure to make it JsonSerializable and serialize the data to an array

CortexPE avatar Aug 05 '21 17:08 CortexPE

That's rather weird, last time I've read the docs to the allowed_mentions field it might've been different? Technically could check them by the github repo, but yes I have tested it.

	public function webhook(): void {
		$webhook = new Webhook("<webhook>");
		$messageOne = new Message();
		$messageOne->setContent("Message One: @everyone");
		$webhook->send($messageOne);

		$messageTwo = new Message();
		$messageTwo->setAllowedMentions(false, false, false);
		$messageTwo->setContent("Message Two: @everyone");
		$webhook->send($messageTwo);
	}

and it returned me this image

packbier avatar Aug 05 '21 18:08 packbier

I didn’t check the roles and users yet I will do it after I’m getting home again

packbier avatar Aug 05 '21 18:08 packbier

Okay, back at home I checked it again with users and roles and it works completely fine, my sample code is

	public function webhook(): void {
		$webhook = new Webhook("webhook");
		$messageOne = new Message();
		$messageOne->setContent("Message One: @everyone - User: <@216199376077455360> | Role: <@&719009266559746079>");
		$webhook->send($messageOne);

		$messageTwo = new Message();
		$messageTwo->setAllowedMentions(false, false, false);
		$messageTwo->setContent("Message Two: @everyone - User: <@216199376077455360> | Role: <@&719009266559746079>");
		$webhook->send($messageTwo);
	}

and the result of it was image

packbier avatar Aug 05 '21 22:08 packbier

Works perfectly fine!

xDidntPot avatar Aug 09 '21 10:08 xDidntPot

I still think it's better to implement the actual API's class instead of relying on undocumented behavior 🤔

CortexPE avatar Aug 11 '21 07:08 CortexPE

The following commit adds a few functions Message->getAllowedMentions which will return a new instance of AllowedMentions, if the function wasn't called before, else it will return the same as called before. AllowedMentions has these functions:

  • AllowedMentions->suppressAll To suppress all mentions
  • AllowedMentions->mentionEveryone To control whether @everyone and @here mentions are allowed
  • AllowedMentions->addRole Which takes a variable length array out of snowflakes for role ids
  • AllowedMentions->addUser Which takes a variable length array out of snowflakes for user ids
  • AllowedMentions->jsonSerialize which got adapted from the reference

The class took reference from here

packbier avatar Aug 20 '21 14:08 packbier

Perhaps include tests and some sample code into README.md

Looks good to me, just needs documentation and tests

CortexPE avatar Oct 23 '21 16:10 CortexPE

This should be included into the PM4-branch.

supercrafter333 avatar Dec 15 '21 12:12 supercrafter333

Was being busy with school until now.

packbier avatar Dec 16 '21 21:12 packbier

Sample testing code:

   protected function onEnable(): void
    {
        $webhook = new Webhook(self::URL);

        // test @everyone
        $msg1 = new Message();
        $msg1->setContent("Hello! @everyone");
        $msg1->getAllowedMentions(); // explain the behavior of the readme

        $msg2 = new Message();
        $msg2->setContent("Hello! @everyone");
        $msg2->getAllowedMentions()->suppressAll();

        $webhook->send($msg1);
        usleep(100);
        $webhook->send($msg2);
        sleep(1);

        // test user mentions, expected behavior: only mention martin not japanlover, 2nd mention both
        $msg3 = new Message();
        $msg3->setContent("<@216199376077455360> <@846158172191850516>");
        $msg3->getAllowedMentions()->addUser("216199376077455360");

        $msg4 = new Message();
        $msg4->setContent("<@216199376077455360> <@846158172191850516>");

        $webhook->send($msg3);
        usleep(100);
        $webhook->send($msg4);
        sleep(1);

        // role id: first is called first, second is called second. martin has first, japanlover has second
        // test role mentions, expected behavior: only mention @first, 2nd mention both
        $msg5 = new Message();
        $msg5->setContent("<@&1104850989795790879> <@&1104851010893123694>");
        $msg5->getAllowedMentions()->addRole("1104850989795790879");

        $msg6 = new Message();
        $msg6->setContent("<@&1104850989795790879> <@&1104851010893123694>");

        $webhook->send($msg5);
        usleep(100);
        $webhook->send($msg6);
        sleep(1);
    }

Results of both accounts image

Second picture with higher delay from JapanLover POV because it was set too low and send the other one before the second one. (2s delay) image

The left side is @Martin (216199376077455360), the right side is @JapanLover (846158172191850516) @Martin has the role @first (1104850989795790879), @JapanLover has the role @second (1104851010893123694).

Ignore the cringe name

packbier avatar May 07 '23 19:05 packbier

stop using usleep() or sleep() this may block your main thread.

xqwtxon avatar Aug 24 '23 06:08 xqwtxon

@xqwtxon I am very much aware, it is just way more readable using sleep in a single function then other approaches and this is just meant to showcase it

packbier avatar Aug 24 '23 15:08 packbier