【问题标题】:bot.wait_for() in Discord.py reading reaction from any bot-sent messageDiscord.py 中的 bot.wait_for() 从任何机器人发送的消息中读取反应
【发布时间】:2021-01-12 20:59:56
【问题描述】:

所以下面的代码有一个我没能解决的问题。

机器人会发送第二个“testMessasge”,然后对该消息和主消息做出反应。这里的问题是,当我只想对第一条消息做出反应时,我可以对任一消息做出反应以触发代码。

不胜感激。

async def foo(ctx, booleanValue, randomClass, stringValue='', otherData=None):
    files, embed = createEmbed(ctx, randomClass)
    message = await ctx.send(files=files, embed=embed)
    await message.add_reaction(data.getEmoji('1'))

    testMessage = await ctx.send('TEST')
    await testMessage.add_reaction(data.getEmoji('1'))

    def check(reaction, user):
        return (user == ctx.message.author and str(reaction.emoji) == data.getEmoji('1'))
    
    async def waitForEmoji(ctx, isBattle):
        print(ctx.message.author)
        try:
            reaction, user = await bot.wait_for('reaction_add', timeout=300.0, check=check)
        except asyncio.TimeoutError:
            await ctx.send(ctx.message.author.display_name + 's connection closed.')
        else:
            print(user)
            print(ctx.message.author)

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    on_reaction_add 的文档中写的注释中,他们提到您可以使用reaction.message 来获取正在响应的消息。

    如果我们在检查中实现这一点,我们会得到:

        def check(reaction, user):
            return (
                user == ctx.message.author
                and str(reaction.emoji) == data.getEmoji('1')
                and reaction.message == message # What you called your first message
                )
    

    这使得机器人只有在反应被添加到第一条消息时才会触发

    【讨论】:

      猜你喜欢
      • 2021-04-11
      • 2020-09-08
      • 1970-01-01
      • 2020-11-17
      • 2020-10-10
      • 2020-02-09
      • 1970-01-01
      • 2022-01-03
      • 2021-01-26
      相关资源
      最近更新 更多