C. Pendergrass
C. Pendergrass

Reputation: 13

How to grab a message object from it's id discord.py

So i'm trying to build a discord bot and one of the functions I'd like to have is to reference previous messages and pull data from it. I've stored the message ID's that I would like to reference but I can't seem to find how to pull a message object from it's ID.

if split[0] == 'ref':
      if (len(split) < 2):
        await message.channel.send("!ref,poll or sg,id number")
      if (len(split) == 3):
        if split[1] == "poll":
          for x in db["poll"]:
            if x[1] == message.guild.id and x[0] == split[2]:
              original = await discord.utils.get(message.channel, message_id=int(split[2]))
              await message.channel.send(original.content)

I keep using the wrong syntax for the original variable but I can't figure it out

I tried using

original = await message.channel.fetch_message(int(split[2]))

and got

Bot is online as *******
Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 277, in on_message
    original = await message.channel.fetch_message(int(split[2]))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 1132, in fetch_message
    data = await self._state.http.get_message(channel.id, id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message

I have the ID for the channel I would like to pull from too

Upvotes: 1

Views: 3549

Answers (1)

Razzer
Razzer

Reputation: 884

You can fetch a message directly, you just need the message-id and the channel, where the message is.

you can fetch the "original" message in the message.channel with this way:

original = await message.channel.fetch_message(int(split[2]))

If you don't have the channel object and only the channel id, than you just need bot.get_channel. That would look like this:

channel = bot.get_channel(mychannelid)

Sources

Upvotes: 2

Related Questions