So I was programming a discord bot in python. But there is a problem. I know how to mention the user who sent the message. But how do I get the ID of a user that is mentioned by the author in the message? I couldn't get the answer.
问题:
回答1:
You can get the user(s) that were mentioned in a message using message.mentions
(async rewrite)
From there, you can either get the ID from the Member object, or get a formatted mention from those objects
message.mentions[0].id
message.mentions[0].mention
回答2:
discord.Message
objects have an attribute of mentions
that returns a list of discord.Member
objects of all users mentioned in the message content, discord.Member
objects have discord.User
objects as parents so you can access discord.User
attributes like author.id
and author.name
etc. simply iterate over the mentions with
for member in message.mentions:
# do stuff with member
You should really be using discord.ext.commands
(async rewrite)
because making a command parser that purely works in on_message
event method is a bad non pythonic idea.
What you probably want
seems like all you just want is to be able to access the author object of the message and the users mentioned in the message as parameters, for that please consult my answer here. To access the author you can simply do it through the discord.Message
object with message.author