問題描述
我在使用 message.author.id 時遇到問題.當我要求機器人返回用戶 ID 時,它會返回...用戶 ID.但這不是我想要的.我想讓它說出用戶的不和諧名稱(例如,而不是像 13284701972315 那樣,它會說 MyName #0000).我正在制作一個機器人,它會根據(jù)命令給出布朗尼點數(shù).這是它可能會說的內(nèi)容和示例.
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!
我正在尋找一種讓機器人顯示用戶名而不是用戶 ID 的方法.
I'm looking for a way for the bot to display the user's name instead of the user's ID.
推薦答案
如果您已經(jīng)有一個 Member
對象,則可以立即訪問該名稱.
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
如果您沒有 Member
對象,但有 Guild
(discord.py 0.x 中的 Server
),您可以使用 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
方法.請注意,這將返回 User
而不是 Member
,因此您將無法訪問昵稱、角色等.
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
這篇關(guān)于對于 discord.py,我如何將作者的 id 變成不和諧用戶的真實姓名?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!