How to get the ID of a mentioned User in Python discord bot?

谁说胖子不能爱 提交于 2019-12-11 14:43:53

问题


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



来源:https://stackoverflow.com/questions/48354901/how-to-get-the-id-of-a-mentioned-user-in-python-discord-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!