在Discord.py中从公会获取所有成员

时间:2020-07-21 23:51:51

标签: python discord discord.py

嘿,这是我下面的代码,它可以正常工作,但是我只获得了一次与我聊天(在私人聊天中)的用户。


@client.command()
async def hi(ctx):
    with open('users.txt','w') as f:
        for member in ctx.guild.members:
            print("{},{}".format(member,member.id), file=f,)
        print("done")

2 个答案:

答案 0 :(得分:2)

您可能需要调用fetch_members来从Discord API更新客户端内部缓存:

@client.command()
async def hi(ctx):
    with open('users.txt','w') as f:
        async for member in ctx.guild.fetch_members(limit=None):
            print("{},{}".format(member,member.id), file=f,)
    print("done")

答案 1 :(得分:0)

您是否启用了 Intent,如果没有,请启用它们并尝试。