【问题标题】:How to get member object from Id discord.py如何从 ID discord.py 中获取成员对象
【发布时间】:2021-03-27 14:37:23
【问题描述】:

我有一个用户 ID,但是我希望能够将其转换为成员对象。 我的机器人有一个排行榜,并且商店使用用户不和谐 ID 作为键。我希望能够使用这些 id 来获取成员对象。即使该用户不在服务器中,我也希望能够访问它。 这可能吗,如果可以,我该怎么做

【问题讨论】:

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


    【解决方案1】:

    你可以在一行代码中做到这一点:

    member_obj = ctx.guild.get_member(user_id)
    

    例如,您可以在函数中使用它:

    import discord
    from discord.ext import commands
    
    @client.command()
    async def id_to_name(ctx, *, id):
        member_obj = ctx.guild.get_member(user_id)
        await ctx.send(f'ID to name: {member_obj.display_name}')
    

    【讨论】:

    • 当我使用它时,它总是返回 discord.ext.commands.errors.CommandInvokeError: 命令引发异常:AttributeError: 'Context' object has no attribute 'get_member'
    • @noaathholm 尝试改用ctx.guild.get_member()ctx.get_member()
    • ctx.guild.get_members() 工作但它正在返回 none 你知道是什么原因造成的吗?
    • 如果您尝试打印不和谐的项目,它不返回任何内容是正常的。您需要将其用作方法,例如:member_obj.mention 或类似的东西。永远记住,discord 对象不是字符串或整数。
    【解决方案2】:

    你可以做两件事中的任何一件。

    如果你想使用 ctx:

    ctx.author.mention
    

    #如果你想使用会员的东西。

    @Bot.command()
    
    async def example(ctx, member : discord.Member):
    
           await ctx.send(f"name{member.mention or ctx.authour.mention}
    
    

    【讨论】:

    • 这不能回答 OP 的问题。他们问的是如何从 Discord.py 中的 ID 获取 Member 对象,而不是如何提及用户。
    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    相关资源
    最近更新 更多