【问题标题】:Discord Python - How to get the username from the user ID?Discord Python - 如何从用户 ID 中获取用户名?
【发布时间】:2019-05-03 21:53:13
【问题描述】:

有谁知道如何获取给定 ID 的人的不和谐用户名(和鉴别器)? 此代码不返回用户名:

userid = '123'
username = bot.get_user_info(userid)

谢谢! :)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    bot 是什么?我猜这是你的discord.Client 对象?然后,您对get_user_info 的调用将返回类discord.User 的对象。此外,get_user_infocoroutine

    你必须这样写:

    user = await bot.get_user_info(userid)
    username = user.name
    

    正如@squaswin 在 cmets 中指出的那样:请记住,await 语句必须在 async 函数内。否则将引发SyntaxError

    【讨论】:

    • 您是否尝试在未标记为 async def 的函数中使用 await 语句
    • 使用 discord.py 的良好经验法则,如果显示“生成器”,那么您可能错过了等待,如果开始抛出语法错误,您可能错过了异步。
    猜你喜欢
    • 2019-12-30
    • 2023-04-11
    • 1970-01-01
    • 2021-02-06
    • 2022-06-12
    • 2020-07-05
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多