首页 > 解决方案 > 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

谢谢

标签: pythonasynchronousdiscorddiscord.pypython-asyncio

解决方案


我发现了问题,您不能将 fetch_message 与用户一起使用,它需要是频道,所以我将代码更改为

await reaction.message.channel.fetch_message(id) 它奏效了:)


推荐阅读