My Discord bot has a command that needs to check 2 discord user ID's: ctx.message.author.id and the author ID of the "reply" message.
Getting the id of the first user is easy, because this is just the id of the one who used the command:
def check_two_ids(ctx):
id1 = ctx.message.author.id
# come other code
Getting the second id is way more difficult as the command only works if it is used as a reply. So I tried to use the MessageReference object and get the second id out of that somehow. Here is what I tried:
def check_two_ids(ctx):
id1 = ctx.message.author.id
message_reference = ctx.message.reference
id2 = message.reference.author.id
# do some more stuff with these two ids
Running this code returns an error: AttributeError: 'MessageReference' object has no attribute 'author'. I thought this would be similar to a normal Message object which has these parameters.
Can I get the second ID with this approach somehow?
CodePudding user response:
The reason why MessageReference doesn't directly have an author attribute is because it simply isn't provided by Discord API. However under certain circumstances (when the reference is pointing to a reply), Discord API will provide the resolved message object for the reference which you can access with message.reference.resolved.
def check_two_ids(ctx):
id1 = ctx.message.author.id
message_reference = ctx.message.reference
if not (message_reference and message_reference.resolved and
isinstance(message_reference.resolved, discord.Message)):
return
id2 = message_reference.resolved.author.id
There's a few caveats with the resolved attribute though:
- It could be
None, Discord API fails to fetch it. - It count be
DeletedReferencedMessage, the reference was deleted.
