python - discord.py 按内容获取消息
问题描述
我正在尝试为反应角色构建一个不和谐的机器人。为此,我尝试将 on_reaction_add 与 fetch_message 结合使用来检查添加的反应是否是机器人发送的消息,但我不断收到 fetch_message 的各种错误。这是代码:
@bot.command()
async def createteams(ctx):
msg = await ctx.send("React to get into your teams")
await msg.add_reaction("1️⃣")
await msg.add_reaction("2️⃣")
await msg.add_reaction("3️⃣")
await msg.add_reaction("4️⃣")
await msg.add_reaction("5️⃣")
await msg.add_reaction("6️⃣")
await msg.add_reaction("7️⃣")
await msg.add_reaction("8️⃣")
@bot.event
async def on_reaction_add(reaction, user):
id = reaction.message.id
if await reaction.message.author.fetch_message(reaction.message.id) == "React to get into your teams":
print("Success")
这给了我错误
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "hypixel.py", line 60, in on_reaction_add
fetch = await reaction.message.author.fetch_message(id)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/abc.py", line 955, in fetch_message
channel = await self._get_channel()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/member.py", line 243, in _get_channel
ch = await self.create_dm()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/member.py", line 109, in general
return getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'
但是当我去复制我反应的消息的 ID 时,它与打印的变量相同,id
但它仍然显示 Message Not Found
谢谢
解决方案
我发现了问题,您不能将 fetch_message 与用户一起使用,它需要是频道,所以我将代码更改为
await reaction.message.channel.fetch_message(id)
它奏效了:)
推荐阅读
- javascript - NVD3 图表显示错误数据的工具提示
- c++ - C++:将 std::set_union() 输出存储在 std::multiset 中
- vb.net - TCP 客户端程序无法连接到我的服务器
- php - 如何比较输入到数组php
- python - 如何对我的机器人进行编码以从一个命令生成随机图像
- mongodb - 在 MongoDB 中迁移数据
- python - Imagemagick 魔杖使用深度不像命令行那样工作
- javascript - 处理来自控制台的nodeJs输入?
- javascript - 仅将简单的 php web 应用程序转换为 LAN
- angularjs - 如果进度条为 100%,AngularJS 删除“活动”类