如何使用Discord.py将用户ID转换为成员对象

时间:2020-11-06 10:22:21

标签: python discord discord.py discord.py-rewrite

我正在尝试将存储在sqlite3数据库中的用户ID转换为成员对象,以便获得该用户的.display_name属性。

    channel_id = ctx.channel.id
    c.execute(f"SELECT * FROM deleted_messages WHERE channel_id={channel_id}")
    array = c.fetchone()
    message_text = array[1]
    author_id = array[2]
    guild = bot.get_guild(GUILD_ID) #Here I usually have the ID of the guild that the bot is in.
    author = guild.get_member(author_id)
    
    author.display_name #When I try to get the diplay_name of that member, I get the error.

这是我得到的错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'display_name'

1 个答案:

答案 0 :(得分:0)

guild.get_member(author_id)返回None

您必须首先检查收到的作者是否是成员对象。

进一步,您可能未启用会员意图。 Enable that或使用coroutine fetch_member