【问题标题】:Have two users challenge each other using reactions discord.py让两个用户使用反应 discord.py 互相挑战
【发布时间】:2020-11-02 09:35:23
【问题描述】:

我希望两个用户互相挑战以便能够使用反应,所以如果你只能使用一个命令。 使用命令tfo @user 我希望您向用户发送一条私人消息,并可以单击两个反应之一来接受或拒绝具有时间限制的挑战,以防未及时执行取消挑战请求并将发送另一条私人消息(给你们俩)。

我知道要做到这一点,我应该使用await client.wait_for,但我不知道如何将其用于反应。

这是我用于针对机器人的版本的代码

@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)

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您的代码片段很大并且使用另一种语言,所以我无法直接帮助您(因为我不知道发生了什么),但这是如何使用 wait_for 进行反应:

    @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('?')
    

    API Docs 中取出wait_for 的片段,展示了如何等待? 反应并对其进行处理。如果用户没有在timeout(这里是 60 秒)内添加反应,它会抛出一个TimeoutError 并发送?。您可以对其进行修改以满足您的需求并将其放入您的命令中。

    【讨论】:

    • 你好。我如何在命令中应用这些字符串?我了解该系统,但我不知道如何应用它以及如何指定,只有当消息的作者做出反应继续进行时。
    • 嗨,我仍在尝试使用这种方法,在它必须工作的那一刻,但我希望在添加反应时也删除用户的反应。我尝试了ctx.author,但失败了,还有其他方法吗?
    • I don't know how to specify that only if the author of the message reacts to go ahead" 在上面的例子中,check 函数检查添加反应的人 (user) 是否与作者相同 (message.author)
    • How could I apply these strings in a command? 取第一个 if 语句下面的所有内容(忽略if message.content.startswith(),这就是您的命令所做的),并将message 替换为ctx
    • “我尝试过但失败了”是什么意思?您能否更新您的帖子以显示您尝试过的内容并说出出了什么问题?只是说“它失败了”对我帮助你没有多大帮助。另外,请只发布代码的相关部分,而不是像原始帖子中那样大的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2021-12-05
    • 2023-03-08
    • 2020-01-04
    相关资源
    最近更新 更多