peppewarrior1
peppewarrior1

Reputation: 55

Have two users challenge each other using reactions discord.py

I would like two users to challenge each other to be able to use reactions so if you can only have one command to use. Using the command tfo @user I would like you to send a private message to the user with the possibility of clicking one of the two reactions to accept or reject the challenge having a time limit that in case it is not executed in time cancels the challenge request and will send another private message (to both of you).

I know that to do this I should use await client.wait_for but I don't know how to use it for reactions.

This is the code I used for the version against the bot

@client.command(aliases=["TFO"])
async def tenta_la_fortuna_offline(ctx, amount : int):
    id = str(ctx.message.author.id)
    amounts[id] -= amount
    if amounts[id] < 0:
        await ctx.send(f"Non hai abbastanza soldi per giocare.")
        amounts[id] += amount
        return
    if amount < 99:
        await ctx.send("Puoi puntare da 100 in poi")
        return
    if not amount:
        await ctx.send("Aggiungi quanti soldi vuoi puntare")
        return
    if id not in amounts:
        await ctx.send("Hai bisogno di un account per giocare")
        return
    utente = [
        ('Sei salvo!', 'salvo',),
        ('Sei a rischio eliminazione!', 'rischio',),
        ('Sei __ELIMINATO__!', 'eliminato'),
        ('Hai __~~VINTO~~__!', 'vinto'),
        ('Sei a rischio eliminazione!', 'rischio',),
    ]
    bot = [
        ('Sei salvo!', 'salvo',),
        ('Sei a rischio eliminazione!', 'rischio',),
        ('Sei __ELIMINATO__!', 'eliminato'),
        ('Hai __~~VINTO~~__!', 'vinto'),
        ('Sei a rischio eliminazione!', 'rischio',),
    ]
    embed = discord.Embed(
        title="**Tenta la Fortuna - Offline**",
        description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
        color=0x003399
    )
    embed.set_thumbnail(url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png')
    embed.add_field(
        name="**Utente**",
        value="** **",
        inline=True
    )
    embed.add_field(
        name="**Bot**",
        value="** **",
        inline=True
    )
    message = await ctx.send(embed=embed)
    win = int(amount * 2)
    await asyncio.sleep(1)
    for numero in range(1,101):
        u_uscita, u_condizione = random.choice(utente)
        embed.clear_fields()
        if u_condizione == "eliminato":
            embed = discord.Embed(
                title="**Tenta la Fortuna - Offline**",
                description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                color=0xFF0000
            )
            embed.set_thumbnail(
                url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
            )
            embed.set_footer(
                text=f"Round numero {numero}"
            )
            embed.add_field(
                name="**Utente**",
                value=f"{u_uscita}",
                inline=True
            )
            embed.add_field(
                name="**Bot**",
                value="** **",
                inline=True
            )
            await message.edit(embed=embed)
            return
        if u_condizione == "vinto":
            embed = discord.Embed(
                title="**Tenta la Fortuna - Offline**",
                description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                color=0x00FF00
            )
            embed.set_thumbnail(
                url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
            )
            embed.set_footer(
                text=f"Round numero {numero}"
            )
            embed.add_field(
                name="**Utente**",
                value=f"{u_uscita}",
                inline=True
            )
            embed.add_field(
                name="**Bot**",
                value="** **",
                inline=True
            )
            await message.edit(embed=embed)
            amounts[id] += win
            return
        embed.set_footer(
            text=f"Round numero {numero}"
        )
        embed.add_field(
            name="**Utente**",
            value=f"{u_uscita}",
            inline=True
        )
        embed.add_field(
            name="**Bot**",
            value="** **",
            inline=True
        )
        await message.edit(embed=embed)
        await asyncio.sleep(0.5)
        b_uscita, b_condizione = random.choice(bot)
        embed.clear_fields()
        if b_condizione == "vinto":
            embed = discord.Embed(
                title="**Tenta la Fortuna - Offline**",
                description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                color=0xFF0000
            )
            embed.set_thumbnail(
                url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
            )
            embed.set_footer(
                text=f"Round numero {numero}"
            )
            embed.add_field(
                name="**Utente**",
                value=f"{u_uscita}",
                inline=True
            )
            embed.add_field(
                name="**Bot**",
                value=f"{b_uscita}",
                inline=True
            )
            await message.edit(embed=embed)
            return
        if u_condizione == "rischio":
            if b_condizione == "salvo":
                embed = discord.Embed(
                    title="**Tenta la Fortuna - Offline**",
                    description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                    color=0xFF0000
                )
                embed.set_thumbnail(
                    url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
                )
                embed.set_footer(
                    text=f"Round numero {numero}"
                )
                embed.add_field(
                    name="**Utente**",
                    value=f"{u_uscita}",
                    inline=True
                )
                embed.add_field(
                    name="**Bot**",
                    value=f"{b_uscita}",
                    inline=True
                )
                await message.edit(embed=embed)
                return
        if b_condizione == "eliminato":
            embed = discord.Embed(
                title="**Tenta la Fortuna - Offline**",
                description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                color=0x00FF00
            )
            embed.set_thumbnail(
                url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
            )
            embed.set_footer(
                text=f"Round numero {numero}"
            )
            embed.add_field(
                name="**Utente**",
                value=f"{u_uscita}",
                inline=True
            )
            embed.add_field(
                name="**Bot**",
                value=f"{b_uscita}",
                inline=True
            )
            await message.edit(embed=embed)
            amounts[id] += amount
            return
        if b_condizione == "rischio":
            if u_condizione == "salvo":
                embed = discord.Embed(
                    title="**Tenta la Fortuna - Offline**",
                    description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                    color=0x00FF00
                )
                embed.set_thumbnail(
                    url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
                )
                embed.set_footer(
                    text=f"Round numero {numero}"
                )
                embed.add_field(
                    name="**Utente**",
                    value=f"{u_uscita}",
                    inline=True
                )
                embed.add_field(
                    name="**Bot**",
                    value=f"{b_uscita}",
                    inline=True
                )
                await message.edit(embed=embed)
                amounts[id] += win
                return
        embed.set_footer(
            text=f"Round numero {numero}"
        )
        embed.add_field(
            name="**Utente**",
            value=f"{u_uscita}",
            inline=True
        )
        embed.add_field(
            name="**Bot**",
            value=f"{b_uscita}",
            inline=True
        )
        await message.edit(embed=embed)
        await asyncio.sleep(1)
        if numero == 100:
            embed = discord.Embed(
                title="**Tenta la Fortuna - Offline**",
                description="Affronta il nostro bot per poter vincere il doppio dei soldi scommessi!",
                color=0xDBA901
            )
            embed.set_thumbnail(
                url='https://cdn.discordapp.com/attachments/640563710104043530/731596857868025886/TENTA_LA_FORTUNA.png'
            )
            embed.set_footer(
                text=f"Round numero {numero}"
            )
            embed.add_field(
                name="**Utente**",
                value=f"{u_uscita}",
                inline=True
            )
            embed.add_field(
                name="**Bot**",
                value=f"{b_uscita}",
                inline=True
            )
            await message.edit(embed=embed)

Upvotes: 0

Views: 71

Answers (1)

stijndcl
stijndcl

Reputation: 5650

Your code fragment is huge & in another language so I can't help you directly (as I have no idea what's going on), but this is how to use wait_for with reactions:

@client.event
async def on_message(message):
    if message.content.startswith('$thumb'):
        channel = message.channel
        await channel.send('Send me that 👍 reaction, mate')

        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == '👍'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('👎')
        else:
            await channel.send('👍')

Fragment taken out of the API Docs for wait_for, shows how to wait for a 👍 reaction & do something with it. In case the user doesn't add the reaction within the timeout (here 60 seconds), it will throw a TimeoutError and send 👎. You can modify this to fit your needs & put it in your command where it fits.

Upvotes: 1

Related Questions