0

I want to find out the username by the message_id from a responsed message. So there is a response and I get the message that is responsed by that message, and from that message I already know the MESSAGE ID, but I want to know the user that has written it.

So, is there any way to get the USER from the MESSAGE ID?

Sorry for my maybe bad English, I am GERMAN.

I am also new at coding Discord Bots.

I tried:

msg = message.reference.message_id
author = msg.author
print(author)

And as a result I get:

AttributeError: 'int' object has no attribute 'author'

Help?

Fastnlight
  • 922
  • 2
  • 5
  • 16

1 Answers1

0

You want to use the cached_message attribute to access the Message and then get the user from that. However, it might be None if the reference message isn't in the cache.

I don't know what context you're doing this in (as you've not provided any in your question) so I'm using everything on the basis you have a discord.Message and we're within an async context.

reference_message = message.reference.cached_message
if not reference_message:
    guild = message.guild
    channel_id = message.reference.channel_id
    # we need to get the message directly from the channel
    channel = guild.get_channel(channel_id)
    if not channel:
        # channel could theoretically be None so could fetch it - likely of this happening is low though but including just in case
        channel = await guild.fetch_channel(channel_id)
    reference_message = await channel.fetch_message(message.reference.message_id)

original_author = reference_message.author

# do what you want with the author
# reference_message is a discord.Message object
ESloman
  • 1,961
  • 1
  • 10
  • 14