簡體   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