CraftSpider / dpytest

A package that assists in writing tests for discord.py
MIT License
103 stars 24 forks source link

Get member mentions by using the user ID in mention #51

Closed Shmuppel closed 3 years ago

Shmuppel commented 3 years ago

Bot clients are not being added as a member to the guilds, because of this when you mention the bot user discord.util.get returns null

Reproduction:

@pytest.mark.asyncio
async def test_hello_mention(bot):
    message = await dpytest.message(f"hello <@{bot.user.id}>")
    dpytest.verify_message(f"<@{message.author.id}>, get to work!")

Will cascade into:

return [discord.utils.get(guild.members, mention=match) for match in matches]

Which will return [None].

This PR contains a simple fix that adds the bot client to any guilds that are initialised with runner.configure

Sergeileduc commented 3 years ago

I'll look into it ASAP !

Shmuppel commented 3 years ago

To clarify on what's happening, here is the problem:

# Member / Client with username
mention = '<@!...>'
discord.util.get(guild.members, mention=mention) # returns the user

# Member / Client without username
mention = '<@!...>'
discord.util.get(guild.members, mention=mention) # does not return the user, expects <@...>

This goes visa versa for clients with usernames being mentioned by <@..> the get comparison expects <@!..> I've now changed it so it retrieves the user id after the member_mention pattern is matched, after which I use discord.util.get(guild.members, id=...) instead.

CraftSpider commented 3 years ago

Makes sense. It may also be possible to make the member_mention pattern better, if that's the one we're defining

Shmuppel commented 3 years ago

Yeah the mention pattern allows <@! and <@ which makes sense.

e.g. using <@! the discord client and discord.py mention the user when sending a message whether they have a nickname or not, its just that discord.util.get specifically compares the mention pattern

Not sure how to improve this pattern though! The current pattern r"<@!?[0-9]{17,21}>" seems fine to me when you're considering how people will be using <@! and <@ interchangeably

Sergeileduc commented 3 years ago

@CraftSpider is it okay to merge ?

CraftSpider commented 3 years ago

Yeah, this looks okay to me