Dante
Dante

Reputation: 3

Discord.py how to get message author from the message id

I'm new to programming discord bots. I'm currently using on_raw_reaction_add(payload) and on_raw_reaction_remove(payload) in order to keep track of how many times a certain reaction is used. Is there a way that I can get the message author from the payload or get the message author from payload.message_id?

Upvotes: 0

Views: 3014

Answers (2)

Just for fun
Just for fun

Reputation: 4225

The above answer is partially correct, you need TextChannel.fetch_message() not Client.fetch_message()

msg = await bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
author = msg.author

Upvotes: 2

Allister
Allister

Reputation: 911

I misread the docs. Thanks to @Poojan for pointing that out. To fix the error you're getting from their answer, you need to await the command. I assume this is because fetch_message calls some sort of http request instead of copying information from memory. This means there is a delay from the request to the information being received, and you need to use the keyword "await" to wait for it.

@bot.event
async def on_raw_reaction_add(payload):
    msg = await bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
    author = msg.author
    print(author.display_name)

Tested and it outputs my display name. Author is a user object.

Upvotes: 1

Related Questions