繁体   English   中英

使用 discord.py 进行反应检查

[英]Reaction Check with discord.py

我正在研究一个命令,如果你对机器人做出同意的反应,它将发送一个 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")```

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

  • 等待用户反应'✔️'
  • 发送 gif 3Tt5
  • 等待用户反应'❌'
  • 发送“取消”

你会想要写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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM