Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Message->getAllowedMentions #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mzztin
Copy link

@mzztin mzztin commented Aug 5, 2021

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:

@CortexPE
Copy link
Owner

CortexPE commented Aug 5, 2021

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

@mzztin
Copy link
Author

mzztin commented Aug 5, 2021

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

@mzztin
Copy link
Author

mzztin commented Aug 5, 2021

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

@mzztin
Copy link
Author

mzztin commented Aug 5, 2021

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

@xDidntPot
Copy link

Works perfectly fine!

@CortexPE
Copy link
Owner

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

@mzztin
Copy link
Author

mzztin commented Aug 20, 2021

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

@mzztin mzztin changed the title feature: Message->setAllowedMentions feature: Message->setAllowedMentions (2) Aug 20, 2021
@mzztin mzztin changed the title feature: Message->setAllowedMentions (2) feature: Message->setAllowedMentions Aug 20, 2021
@mzztin mzztin changed the title feature: Message->setAllowedMentions feature: Message->getAllowedMentions Aug 20, 2021
@CortexPE
Copy link
Owner

CortexPE commented Oct 23, 2021

Perhaps include tests and some sample code into README.md

Looks good to me, just needs documentation and tests

@supercrafter333
Copy link

This should be included into the PM4-branch.

@mzztin
Copy link
Author

mzztin commented Dec 16, 2021

Was being busy with school until now.

@mzztin
Copy link
Author

mzztin commented May 7, 2023

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

@xqwtxon
Copy link

xqwtxon commented Aug 24, 2023

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

@mzztin
Copy link
Author

mzztin commented Aug 24, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants