【发布时间】:2021-06-28 10:53:43
【问题描述】:
我正在开发一个 Discord 机器人,它需要等待将反应添加到消息中,但它看起来总是超时并且无法识别正在添加的反应。我的代码看起来类似于 StackOverflow 上有关同一事物的其他问题的答案,但它仍然无法正常工作。
发送消息 10 秒后,timed out :( 字符串被打印出来。与此同时,我对我发送的消息添加了一个反应,但没有任何反应。它也不会调用 check,因为永远不会打印表情符号。
有谁知道我做错了什么?我还查看了 Discord.py 文档,看起来我做得对。
编辑:我创建了一个由主文件和 1 个 cog 组成的最小可重现示例:https://github.com/put/discord-testing-bot。我就是这样做的:
python main.py- 等待打印“Test Cog loaded”
- 确保机器人在线
- 在机器人所在的服务器中发送消息
- 在 10 秒内添加一个竖起大拇指的表情反应
- 等等
- 请参阅控制台中打印的“超时 :(”。
机器人拥有我服务器中的所有权限: GIF of permissions
main.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