Search code examples
pythondiscorddiscord.py

Send a message on react to bot's message


@bot.command()
async def tournament(ctx, arg):
    async def tournament_host(ctx):  
        message = await ctx.channel.send("React to this message to host either an FFA or 2V2 tournament")
        await message.add_reaction("<:ffa:1197727051898163331>")
        await message.add_reaction("<:2v2:1197727046080679996>")
        
        ctx.channel.send(message)
    
    if arg.lower() == "host":
        await tournament_host(ctx)

This command reacts to message twice. I want the bot to detect when the user reacts to one of these emojis, and send a specific message. For example, if the user reacts to the :ffa: emoji, the bot will say "You are hosting an FFA tournament", and if the user reacts to :2v2:, "You are hosting a 2v2 tournament" is said.

I haven't tried anything yet because I haven't found anything for this specific issue.


Solution

  • Check out this resource.

    Particularly these lines:

    @bot.command(name="reactiontest")
    async def reaction_test(ctx): # waiting for reactions (✅, ❌) here
        await ctx.send(f"**{ctx.author}**, please react with :white_check_mark: or :x: on this message in 60 seconds")
        
        def check(r: discord.Reaction, u: Union[discord.Member, discord.User]):  # r = discord.Reaction, u = discord.Member or discord.User.
            return u.id == ctx.author.id and r.message.channel.id == ctx.channel.id and \
                   str(r.emoji) in ["\U00002705", "\U0000274c"]
    
        reaction, user = await bot.wait_for('reaction_add', check = check)
    

    Then, check which reaction by using reaction.emoji.