【问题标题】:How can you check for multiple reactions in a command discord.py如何在命令 discord.py 中检查多个反应
【发布时间】:2020-03-31 06:14:53
【问题描述】:

这是我第一次在这里提问,所以我可能会在某些方面搞砸。

所以我有一个经济机器人,我希望这个命令有 3 个反应,这样人们就可以告诉它该做什么。在这里,这是为了收钱,提升收入或提升他们“公司”的最大产能。

阅读API文档后,我首先有3个检查函数来检查每个反应,比如这个:

(stuff)
message = await ctx.send(embed=embed)
await message.add_reaction("1️⃣")
await message.add_reaction("2️⃣")
await message.add_reaction("3️⃣")

def check1(reaction, user):
    return user == ctx.message.author and str(reaction.emoji) == "1️⃣"

        try:
            reaction, user = await client.wait_for("reaction_add", timeout = 30.0, check = check1)
        except asyncio.TimeoutError:
            print("Timeout")
        else:
            (code to collect money)

但后来我意识到它会一个一个地检查这些,所以你必须等待第一个超时,然后它才会检查第二个。所以经过一番思考,我想出了这个:

(stuff)
message = await ctx.send(embed=embed)
await message.add_reaction("1️⃣")
await message.add_reaction("2️⃣")
await message.add_reaction("3️⃣")

def check(reaction, user):
    return user == ctx.message.author and user != "Robofriend#7565" and str(reaction.emoji) == "1️⃣" or "2️⃣" or "3️⃣"

try:
    reaction, user = await client.wait_for("reaction_add", timeout = 30.0, check = check)
except asyncio.TimeoutError:
    print("Timeout")
else:
    if str(reaction.emoji) == "1️⃣":
        (code to collect money)

我惊讶地发现这已经在堆栈溢出中以与我相同的方式得到了回答,但由于某种原因,我的有问题。当我运行命令时,检查会立即返回 True,因为它会检查自己的反应......我知道这一点是因为我通过使其在检查功能上打印用户来检查。

任何帮助表示赞赏!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    我发现了问题:

    在检查功能中,为了防止机器人检查自己的反应,它会检查 user == ctx.message.author。这里的问题是我已将机器人发送的消息设置为 "message" 以添加反应:

    message = await ctx.send(embed=embed)
    await message.add_reaction("1️⃣")
    await message.add_reaction("2️⃣")
    await message.add_reaction("3️⃣")
    

    我所做的只是将上面代码中的“消息”更改为其他内容,一切正常。

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2021-03-04
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多