0

discord.py How do I get a list of all the servers a user is join to?

i tried it, but failed

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

but, it failed...

how i get a list of all the servers a user is join to?

peanut
  • 41
  • 9

1 Answers1

1

Probably not the fastest way to do this, but one way would be to check every guild your bot is in, then check if the ctx.author is in that guild. If the author is in that guild, said guild is appended to your list, in this case 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}")

The above code works as seen below:

Working Code

If the above does not work, you may need to enable intents. You can view how to do this here.

EDIT

In discord.py version 1.7+ there's the User.mutual_guilds attribute:

@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}")

Version 1.7 it's not yet released, you can either wait till it is, or install it directly from github.

Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39
Bagle
  • 2,326
  • 3
  • 12
  • 36