Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get message by id discord.py

I'm wondering how to get a message by its message id. I have tried discord.fetch_message(id) and discord.get_message(id), but both raise:

Command raised an exception: AttributeError: module 'discord' has no attribute 'fetch_message'/'get_message'
like image 533
HoneyPoop Avatar asked May 17 '20 11:05

HoneyPoop


People also ask

How do I find a discord message using ID?

Add a search option in the search bar with the name: "ID". This feature would allow user's who cannot code bots, or don't have bots that can fetch messages to find specific messages with IDs. If no message is found an error can be returned, etc.

Is discord.py discontinued?

They've decided to step down from the role of maintainer and it marks a sad end to the entire community. The API allowed us, the python-developers to quickly create bots with modern features like Asynchronous Python.

How to get objects in discord Py?

person.speak () person.walk () person.left_hand.grab (a_thing) In discord py, decorators curate special objects for you. If you use on_message, you get a message object, if you use commands you get a context object. What you want to go read in the docs is what are the attributes and methods of the objects you get in the different functions.

How to check who sent the message in discord?

To check who send the message, you would do message.author to get a Member object which has attributes such as display_name which is a string that prints out what their nickname is or mention which is a string that has what you need to mention that user Where can i find discord.py2..

Why can't I find the sent message ID?

If the message has not been sent it doesn't exist yet so there will not be an ID. If you want to edit the message after it has been sent the send function returns a Message object that is the sent message. I can't give you a running example right now but the pseudo-code would look like this.


Video Answer


1 Answers

When getting a message, you're going to need an abc.Messageable object - essentially an object where you can send a message in, for example a text channel, a DM etc.

Example:

@bot.command()
async def getmsg(ctx, msgID: int): # yes, you can do msg: discord.Message
                                   # but for the purposes of this, i'm using an int

    msg = await ctx.fetch_message(msgID) # you now have the message object from the id
                                         # ctx.fetch_message gets it from the channel
                                         # the command was executed in


###################################################

@bot.command()
async def getmsg(ctx, channel: discord.TextChannel, member: discord.Member):
    msg = discord.utils.get(await channel.history(limit=100).flatten(), author=member)
    # this gets the most recent message from a specified member in the past 100 messages
    # in a certain text channel - just an idea of how to use its versatility

References:

  • abc.Messageable
  • TextChannel.history()
  • TextChannel.fetch_message()
  • Context.fetch_message()
like image 89
Diggy. Avatar answered Oct 24 '22 07:10

Diggy.