简体   繁体   中英

Python Discord Bot - Reaction Role

How would i make a reaction role event. This is my code i tried with

@client.event
async def on_raw_reaction_add(payload):
    msgID = 05793080854315018
    if payload != None:
        if payload.message_id == msgID:
            if str(payload.emoji) == "✍️":
                await add_roles("cool")

What i wanna do is have my bot checking for reactions on a specific message and then give the user a role if they reacted with the right emoji.

Assuming that you are using discord.py 1.3.3 you can update your code with the following to add a role based on a specific emoji.

You need to add references to the guild in order to reference the role you wish to add. You will also need to compare the emoji to a string representation of payload.emoji . It might be worth considering using emoji.demojize .

Try:

@client.event
async def on_raw_reaction_add(payload=None):
    msgID = <your message id: int>
    guild = discord.utils.get(client.guilds, name=<your guild name>)
    role = discord.utils.get(guild.roles, name='cool')
    if payload is not None:
        if payload.message_id == msgID:
            if str(payload.emoji) == "✍️":
                await payload.member.add_roles(role)

I refreshed your code, but it still not working, so if someone could help I'm also searching for a solution, first you import discord's library;

import discord
from discord.client import Client
from discord.ext import commands

#I also add a prefix if code would evolve later:

bot = commands.Bot(command_prefix='!')

#Here we got a message who tells bot is online

@bot.event
async def on_ready():
    print("Bot Is entered the building!")

#Then here is the code;

@bot.event
async def on_raw_reaction_add(payload=None):
    msgID = 9454546465646847898,
    guild = 7894651646646464898,
    role = discord.utils.get(guild.roles, name='lvl 0 Test bot')
    if payload is not None:
        if payload.message_id == msgID:
            if str(payload.emoji) == ":yes:":
                await payload.member.add_roles(role)


    bot.run("BOT TOKEN ID")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM