Martin Wenzl
Martin Wenzl

Reputation: 19

Discord bot python embed message reaction

My target is to make bot react to this embed message and also continue when someone reacts

@client.event 
async def on_message(ctx):
    if ctx.author == client.user:
      return
    if ctx.content.startswith('.summon'):
        embed = discord.Embed(title=":crossed_swords:I have been summoned:crossed_swords:", description="to proceed select lvl of your enemy", color=0x00ff00)
        embed.add_field(name="Difficulty level :one:", value="suitable for quick xp, or single take down", inline=False)
        embed.add_field(name="Difficulty level :two:", value="sigher level enemy, with lots of hp and sweet reward", inline=False)
        embed.add_field(name="Difficulty level :three:", value="almost if not impossible enemy, strongest of them all, suitable only for raids", inline=False)
        await ctx.channel.send(embed=embed)

Upvotes: 1

Views: 471

Answers (1)

Impaex
Impaex

Reputation: 106

ctx.channel.send() returns the message sent. If you save that in a variable, you will be able to use the add_reaction method to add reactions. It takes a unicode string or emoji as parameter.

To check whether a user reacts, the wait_for method can be used to wait for a user to add a reaction, and make the bot reply accordingly.

I have included a small example below, though the documentation I linked gives you all the information you need.

The Discord.py library has it's own Discord server, where you can always ask questions, here's the link: https://discord.gg/dpy

@client.event 
async def on_message(ctx):
    if ctx.author == client.user:
      return
    if ctx.content.startswith('.summon'):
        embed = discord.Embed(title=":crossed_swords:I have been summoned:crossed_swords:", description="to proceed select lvl of your enemy", color=0x00ff00)
        embed.add_field(name="Difficulty level :one:", value="suitable for quick xp, or single take down", inline=False)
        embed.add_field(name="Difficulty level :two:", value="sigher level enemy, with lots of hp and sweet reward", inline=False)
        embed.add_field(name="Difficulty level :three:", value="almost if not impossible enemy, strongest of them all, suitable only for raids", inline=False)
        
        # Saving the sent message in a variable here.
        message = await ctx.channel.send(embed=embed)
        await message.add_reaction('\U0001f534')
     
        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == '\U0001f534'

       try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            # Do whatever if the user did not react within 60 seconds.
            await ctx.channel.send('👎')
        else:
            # If users reacted in time, you can check which reaction they
            # pressed by checking it against str(reaction.emoji)
            await ctx.channel.send('👍')

Hope this helps!

Upvotes: 1

Related Questions