简体   繁体   English

如何获取 Python discord 机器人中提到的用户的 ID?

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

So I was programming a discord bot in python.所以我在 python 中编写了一个 discord 机器人。 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?但是如何获取作者在消息中提到的用户 ID? I couldn't get the answer.我无法得到答案。

You can get the user(s) that were mentioned in a message using message.mentions ( async rewrite ) 您可以使用message.mentions异步 重写 )获取消息中提到的用户。

From there, you can either get the ID from the Member object, or get a formatted mention from those objects 从那里,您可以从Member对象获取ID,也可以从这些对象获取格式化的注释

message.mentions[0].id
message.mentions[0].mention

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 discord.Message对象具有的属性mentions返回列表discord.Member在邮件内容中提到的所有用户对象, discord.Member对象具有discord.User对象作为父母,所以你可以访问discord.User属性,如author.idauthor.name等。只需使用

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. 您确实应该使用discord.ext.commands (异步重写),因为制作仅在on_message事件方法中on_message的命令解析器是一个糟糕的非pythonic想法。

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 要访问作者,您可以简单地通过带有message.authordiscord.Message对象来实现message.author

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM