【问题标题】:discord.py How do I get a list of all the servers a user is connected to?discord.py 如何获取用户连接的所有服务器的列表?
【发布时间】:2021-06-19 18:13:28
【问题描述】:

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

我试过了,但失败了

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

但是,失败了……

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

【问题讨论】:

    标签: discord.py


    【解决方案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}")
    

    上面的代码如下所示:

    如果上述方法不起作用,您可能需要启用 intentsYou 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安装。

    【讨论】:

    • 值得一提的是,在 discord.py 1.7+ 中有 User.mutual_guilds 属性
    • 这是真的,我会在我的回答中添加如何做到这一点™
    • 编辑:自己不知道怎么做,如果可以请提供这个例子
    • 只需ctx.author.mutual_guilds
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多