Exodus
Exodus

Reputation: 5

How to get a list of servers my bot is in discord.py

I want to use a command that prints the list of servers my bot is in. This is what I have. When I use the command it sends "property object at 0x000001988E164270" instead of a list of server names

import discord
from discord.ext import commands

client = discord.Client
activeservers = client.guilds

class OwnerCommands(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print("OwnerCommands Is Ready")

    @commands.command()
    async def servers(self, ctx):
        await ctx.send(activeservers)
        print(activeservers)

def setup(client):
    client.add_cog(OwnerCommands(client))

Upvotes: 0

Views: 8432

Answers (1)

Benjin
Benjin

Reputation: 3495

client.guilds is a list of all guilds that the bot is connected to. You need to iterate over this.

Additionally, activeservers = client.guilds is called before the bot has connected, meaning the list will be empty. Move this to inside your command to have the most up to date list at the time the command is called.

import discord
from discord.ext import commands

client = discord.Client

class OwnerCommands(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print("OwnerCommands Is Ready")

    @commands.command()
    async def servers(self, ctx):
        activeservers = client.guilds
        for guild in activeservers:
            await ctx.send(guild.name)
            print(guild.name)

def setup(client):
    client.add_cog(OwnerCommands(client))

Upvotes: 2

Related Questions