How to get the ID of a mentioned User in Python di

2019-08-18 00:29发布

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.

2条回答
仙女界的扛把子
2楼-- · 2019-08-18 01:03

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
查看更多
来,给爷笑一个
3楼-- · 2019-08-18 01:18

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

查看更多
登录 后发表回答