Warm tip: This article is reproduced from serverfault.com, please click

discord.py react to get role

发布于 2020-12-03 22:34:44

I have this embed with rules to my discord server and the embed also has a reaction, the reaction is a check mark(✅). If you react to that embed with that check mark you will get a "Verify" role and your reaction will be deleted.

So it always gonna be one reaction and if u react you get "Verify" role and your reaction get deleted. But that is the problem, I don't get my role when I react and my reaction doesn't get deleted either. I don't have any errors don't know how to fix this.

This is my code

@bot.command(name='rules', pass_ctx=True)
async def rules(ctx):

    rules_embed = discord.Embed(title='DISCORD RULES', color=0xf30000)
    rules_embed.add_field(name="**1. No spamming** ", value="Don't send a lot of small/big messages right after "
                                                            "each other. Do not disrupt chat by spamming.",
                          inline=False)
    rules_embed.add_field(name="**2. No NSFW material** ", value="This is a community server and not meant to "
                                                                 "share this kind of material.", inline=False)
    rules_embed.add_field(name="**3. No bullying or threats** ", value="Threats to other users of DDoS(Distributed "
                                                                       "Denial of Service), Death, DoX/Doxx, abuse, "
                                                                       "and other "
                                                                       "malicious threats are absolutely not okay.",
                          inline=False)
    rules_embed.add_field(name="**4. Don't beg for ranks** ", value="Don't beg for staff or other ranks", inline=False)
    rules_embed.add_field(name="**5. No racism** ", value="Racism is absolutely not okay in this discord server",
                          inline=False)
    rules_embed.add_field(name="**6. Have fun** ", value="Just have fun and be nice", inline=False)

    send_rules = await ctx.message.channel.send(embed=rules_embed)
    reactions = ['✅']
    for i in reactions:
        await send_rules.add_reaction(i)


@bot.event
async def on_raw_reaction_add(payload):
    channel = bot.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    guild = bot.get_guild(payload.guild_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)

    # only work if it is the client
    if payload.member.id == bot.user.id:
        return

    if payload.message_id == 784182764009947177 and reaction.emoji == '✅':
        roles = discord.utils.get(guild.roles, name='Verify')
        await payload.member.add_roles(roles)
        await reaction.remove(payload.member)

Thanks for the help!

Questioner
Sally
Viewed
1
Sofi 2020-12-04 22:54:28

You had several mistakes in the code, I have fixed them and commented a few of them. Please check the following CODE.

@bot.event
async def on_raw_reaction_add(payload):
    #You forgot to await the bot.get_channel
    channel = await bot.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    guild = bot.get_guild(payload.guild_id)
    #Put the following Line
    member = guild.get_member(payload.user_id)
    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)

    # only work if it is the client
    if payload.user_id == bot.user.id:
        return

    if payload.message_id == 784182764009947177 and reaction.emoji == '✅':
        roles = discord.utils.get(guild.roles, name='Verify')
        await member.add_roles(roles)
        await reaction.remove(payload.member)