vanilla
vanilla

Reputation: 79

Raw reaction event doesn't work in discord py

I am currently trying to make a bot that detects if a white checkmark has been added to a certain message id, then it removes the reaction the user just added (back to the number 1).

Right now this block of code isn't working, and I also discovered up until the first if statement at least it was functional. I do have a cog file, but I was hoping I could make it in my bot py. But it isn't too hard to make it a cog. If I were to make it a cog, I might as well make a command that sends the message and reacts to it with a checkmark, then uses the listener.

Anyways, here's the code.

@bot.event()
async def on_raw_reaction_add(payload):
    if payload.message_id == 733759031138582538:
        if payload.emoji == "white_check_mark":
            member = discord.utils.find(payload.user_id)
            await member.add_roles("Guild Member")
            await member.remove_roles("Rules Pending")

Again, the first if statement isn't even working as I have tested with a print() before. Possibly the on_raw_reaction_add may not be functioning for some reason?

Let me know your thoughts!

EDIT: Heres the error i got in the console, also found this post

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "bot.py", line 105, in on_raw_reaction_add
    await member.add_roles(guild_member)
AttributeError: 'NoneType' object has no attribute 'add_roles'

Upvotes: 0

Views: 4100

Answers (3)

Jonas
Jonas

Reputation: 41

Import emoji
@commands.Cog.listener()
    async def on_raw_reaction_add(self,payload=None):
        msgID = 783797861190991893 #message id
        guild = discord.utils.get(self.bot.guilds, name="asywetwe") #guild name here
        role = discord.utils.get(guild.roles, name='wetwet') #group role name
        if payload is not None:
            if payload.message_id == msgID:
                if str(payload.emoji) == "✅": #add raw emoji
                    await payload.member.add_roles(role)

Upvotes: 0

Muhammad Shafeeq
Muhammad Shafeeq

Reputation: 1

to get user don't do all that

@client.event
async def on_raw_reaction_add(payload):
    if payload.message_id == 779742701804847135:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        emoji = payload.emoji.name
        role = discord.utils.get(guild.roles, name=emoji)
        member = payload.member
        if member is not None:
            await member.add_roles(role)

This is my code and it works well

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 60974

There are a few problems:

  1. payload.emoji will be a PartialEmoji object. For unicode emojis, payload.emoji.name will be be the actual codepoint of the emoji. We can represent this with '\N{WHITE HEAVY CHECK MARK}'
  2. To get the member we must first get the guild that the message was in, then get the member from the guild by id.
  3. To add and remove roles, we need to get the Role objects from the guild.

@bot.event
async def on_raw_reaction_add(payload):
    if payload.message_id == 733759031138582538:
        if payload.emoji.name == '\N{WHITE HEAVY CHECK MARK}':
            guild = await bot.fetch_guild(payload.guild_id)
            if guild is not None:
                member = guild.get_member(payload.user_id)
                guild_member = discord.utils.get(guild.roles, name="Guild Member")
                pending = discord.utils.get(guild.roles, name="Rules Pending")
                await member.add_roles(guild_member)
                await member.remove_roles(pending )

Upvotes: 1

Related Questions