对于discord.py,我如何将作者的ID转换为Discord用户的真实姓名? [英] For discord.py, how would I turn the author's id into the discord user's actual name?

查看:221
本文介绍了对于discord.py,我如何将作者的ID转换为Discord用户的真实姓名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理message.author.id时遇到问题。当我要求漫游器返回用户ID时,它会返回井号...用户ID。但这不是我想要的。我希望它说出用户的不和名称(例如,与其说是13284701972315,不如说是MyName#0000)。我正在制作一个机器人,该机器人将在命令中给出布朗尼分数。这是它可能会说的例子。

I'm having trouble with message.author.id. When I ask a bot to return the user's ID, it returns the well... user ID. But that's not what I want. I want it to say the user's discord name (for an example, instead of like 13284701972315 it would say MyName #0000). I'm making a bot which will give out brownie points on command. Here's and example of what it might say.


311661286213550091给@AceFTW提供了22个布朗尼积分。他们目前有22个
布朗尼积分!

311661286213550091 has given 22 brownie points to @AceFTW. They currently have 22 brownie points!

我想说这样的话:


@ tristan360给@AceFTW 22布朗尼积分。他们目前有22个
布朗尼积分!

@tristan360 has given 22 brownie points to @AceFTW. They currently have 22 brownie points!

我正在寻找一种让机器人显示用户名的方法

I'm looking for a way for the bot to display the user's name instead of the user's ID.

推荐答案

如果您已经有成员对象,您可以立即访问该名称。

If you have a Member object already, you can access the name straight away.

# member is <Member id=... name=MyName#0000>
member.name  # MyName
member.nick  # MyNick
member.display_name  # MyNick if nick is not None, else MyName
member.mention  # @MyName
str(member)  # MyName#0000

如果您没有成员对象,但确实有 Guild (discord.py 0.x中的服务器),您可以使用 Guild.get_member 方法。

If you do not have a Member object, but do have a Guild (Server in discord.py 0.x,) you can use the Guild.get_member method.

member = guild.get_member(userID)
member.name  # MyName

如果您根本没有任何信息,可以使用 Client.get_user_info 方法。请注意,这将返回用户而不是成员,因此您将无权访问昵称,角色等。

If you don't have any information at all starting, you can use the Client.get_user_info method. Note, this returns a User not a Member, so you will not have access to nicknames, roles, etc.

user = bot.get_user_info(userID)
user.name  # MyName
user.nick  # Error

这篇关于对于discord.py,我如何将作者的ID转换为Discord用户的真实姓名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆