如何获取 Python discord bot 中提到的用户的 ID?

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

所以我在 python 中编写了一个不和谐的机器人程序。但有一个问题。我知道如何提及发送消息的用户。但是如何获取作者在消息中提到的用户的 ID?我无法得到答案。

您可以使用 message.mentions (async rewrite)

获取消息中提及的用户

从那里,您可以从成员对象中获取 ID,或者从这些对象中获取格式化的提及

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

discord.Message个对象有一个mentions的属性,即returns消息内容中提到的所有用户的discord.Member个对象的列表,discord.Member个对象将 discord.User 对象作为父对象,因此您可以访问 discord.User 属性,例如 author.idauthor.name 等。只需使用

迭代提及
for member in message.mentions:
    # do stuff with member

你真的应该使用 discord.ext.commands(异步重写) 因为制作一个纯粹在 on_message 事件方法中工作的命令解析器是一个糟糕的非 pythonic 想法。

你可能想要什么

似乎您只是想要能够访问消息的作者对象和消息中提到的用户作为参数,为此请参考我的回答 。要访问作者,您只需使用 message.author

通过 discord.Message 对象即可