简体   繁体   中英

discord.py Fetching messages by content

I'm trying to build a discord bot for reaction roles. To do this, I'm trying to use the on_reaction_add combined with fetch_message to check if the reaction added was to the message that the bot sent but I keep getting various error with fetch_message. Here is the code:

@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")

This gives me the error

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'

But when I go and copy the ID of the message I reacted to, its the same as the printed variable id but it still says Message Not Found

Thanks

I figured out the issue, you can't use fetch_message with user it needs to be channel so I changed my code to

await reaction.message.channel.fetch_message(id) and it worked :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM