【问题标题】:Reaction Check with discord.py使用 discord.py 进行反应检查
【发布时间】:2021-03-13 09:13:35
【问题描述】:

我正在研究一个命令,如果你对机器人做出同意反应,它将发送一个 nuke gif。这很好用,但是如果您选择其他选项或超时,我无法让机器人回答不同。我当前的代码如下。感谢您的帮助

async def nuke(ctx):
    
    yas = '✔️'
    nay = '❌'
    
    message = await ctx.send("Are you sure that you want to use your nukes?")
    
    await message.add_reaction(yas)
    await message.add_reaction(nay)
    
    
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) == '✔️'
    await bot.wait_for('reaction_add', timeout=60.0, check=check)
        
    embed = discord.Embed(title="Code: 759245 Activated. Destruction of Channel started")
    embed.set_image(url="https://i.gifer.com/3Tt5.gif")
    await ctx.send(embed=embed)
    
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) == '❌'
    await bot.wait_for('reaction_add', timeout=60.0, check=check)

    await ctx.send("Cancelled")```

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案1】:

    代码从上到下,这意味着您的代码现在要执行的操作如下:

    • 等待用户反应'✔️'
    • 发送gif3Tt5
    • 等待用户反应'❌'
    • 发送“已取消”

    你会想写one检查是否使用了任何可能的反应,然后以不同的方式处理它们。

    yas = '✔️'
    nay = '❌'
    
    valid_reactions = ['✔️', '❌']
    
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in valid_reactions
    reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
    
    if str(reaction.emoji) == yas:
        embed = # code to create the embed
        return await ctx.send(embed=embed)
    
    # there's only two reactions, so if the above function didn't return, it means the second reaction (nay) was used instead
    await ctx.send("Cancelled")
    

    【讨论】:

    • 非常感谢!对我帮助很大
    猜你喜欢
    • 2021-03-04
    • 2021-03-12
    • 1970-01-01
    • 2020-10-06
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多