discord.py 如何获取用户连接的所有服务器的列表?

时间:2021-03-23 04:25:58

标签: discord.py

不和谐.py 如何获取用户加入的所有服务器的列表?

我试过了,但失败了

await ctx.send(str(ctx.author.guilds))

但是,它失败了...

我如何获得用户加入的所有服务器的列表?

1 个答案:

答案 0 :(得分:1)

可能不是最快的方法,但一种方法是检查您的机器人所在的每个公会,然后检查 ctx.author 是否在该公会中。如果作者在该公会中,则该公会会附加到您的列表中,在本例中为 shared_guilds

@client.command()
async def test(ctx):
    shared_guilds = []
    for guild in client.guilds:
        if ctx.author in guild.members:
            shared_guilds.append(guild)

    # This is just a clean way to send the message
    message = ""
    for guild in shared_guilds:
        message += f"`{guild.name}` \n"

    await ctx.send(f"Guilds I share with {ctx.author}: \n{message}")

上面的代码如下所示:

Working Code

如果上述方法不起作用,您可能需要启用意图You can view how to do this here

编辑

在 discord.py 版本 1.7+ 中有 User.mutual_guilds 属性:

@client.command()
async def test(ctx):
    shared_guilds = ctx.author.mutual_guilds

    # This is just a clean way to send the message
    message = ""
    for guild in shared_guilds:
        message += f"`{guild.name}` \n"

    await ctx.send(f"Guilds I share with {ctx.author}: \n{message}")

版本1.7还没有发布,你可以等到它发布,或者直接从github安装。