【问题标题】:wait_for reaction_add always times outwait_for reaction_add 总是超时
【发布时间】:2021-06-28 10:53:43
【问题描述】:

我正在开发一个 Discord 机器人,它需要等待将反应添加到消息中,但它看起来总是超时并且无法识别正在添加的反应。我的代码看起来类似于 StackOverflow 上有关同一事物的其他问题的答案,但它仍然无法正常工作。

发送消息 10 秒后,timed out :( 字符串被打印出来。与此同时,我对我发送的消息添加了一个反应,但没有任何反应。它也不会调用 check,因为永远不会打印表情符号。

有谁知道我做错了什么?我还查看了 Discord.py 文档,看起来我做得对。

编辑:我创建了一个由主文件和 1 个 cog 组成的最小可重现示例:https://github.com/put/discord-testing-bot。我就是这样做的:

  1. python main.py
  2. 等待打印“Test Cog loaded”
  3. 确保机器人在线
  4. 在机器人所在的服务器中发送消息
  5. 在 10 秒内添加一个竖起大拇指的表情反应
  6. 等等
  7. 请参阅控制台中打印的“超时 :(”。

机器人拥有我服务器中的所有权限: GIF of permissions

ma​​in.py

import discord
from discord.ext import commands

intents = discord.Intents.none()
intents.guilds = True
intents.members = True
intents.emojis = True
intents.guild_messages = True
intents.guild_reactions = True

class TestBot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix="^", help_command=None, max_messages=None, intents=intents)
        self.load_extension('test_cog')

if __name__ == "__main__":
    bot = TestBot()
    bot.run("[TOKEN HERE]", bot=True)

test_cog.py

import discord
from discord.ext import commands
import asyncio

class TestCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._last_member = None

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author.bot or message.channel.type.name != 'text':
            return

        def check(reaction, user):
            print('a')
            return user == message.author and str(reaction.emoji) == "????"

        try:        
            reaction, user = await self.bot.wait_for('reaction_add', check=check, timeout=10)
            print('success!')

        except asyncio.exceptions.TimeoutError:
            print('timed out :(')

        except Exception as e:
            print(f'other error: {repr(e)}')

def setup(bot):
    bot.add_cog(TestCog(bot))
    print("Test Cog loaded")

【问题讨论】:

  • this 会回答你的问题吗?
  • 其余代码是什么样的?我已将您的 on_message 放在一个 cog 中,它确实看到了正在添加的反应。
  • 您是否启用了反应和成员意图?
  • @Benjin 我已经更新了我的问题以包含整个 cog!我还阻止了其他齿轮加载,只是为了确保这不是问题。它似乎仍然无法识别正在添加的反应。
  • @FalseDev 我已将我的机器人更改为具有管理员意图,并且我还开启了特权“服务器成员”意图。让它重新加入我的测试服务器只是为了确定,它仍然只是超时,并且从未到达check调用:(

标签: python-3.x discord discord.py


【解决方案1】:

来自documentation

ma​​x_messages(可选[int])—— 存储在内部消息缓存中的最大消息数。这默认为1000。传入None 会禁用消息缓存。

您正在使用max_messages=None 禁用内部消息缓存。由于机器人不存储消息,它看不到任何添加到它的反应。

None 更改为某个整数值(默认为1000),或者只是从您的代码中删除max_messages=None

【讨论】:

  • 非常感谢!这确实解决了它!
猜你喜欢
  • 1970-01-01
  • 2021-04-24
  • 2021-07-10
  • 2021-06-20
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
相关资源
最近更新 更多