Demotry
Demotry

Reputation: 907

Get Complete Servers List

So how to get all the servers list my bot has connected or installed. I used below code but its now working no idea why.

servers = list(bot.servers)
print("Connected on " + str(len(bot.servers)) + "servers:")
for x in range(len(servers)):
    print('  ' + servers[x-1].name)

My bot.py full code

token = "This is my Token" # This is what the bot uses to log into Discord.
prefix = "?" # This will be used at the start of commands.

import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

@bot.command(pass_context=True)
async def ping(ctx):
    msg = 'Pong {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

@bot.command(pass_context=True)
async def hello(ctx):
    msg = 'hello... {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)

Upvotes: 1

Views: 11144

Answers (2)

Benjin
Benjin

Reputation: 3495

For the async branch, bot.servers returns an iterable of all the servers the bot can see.

For the rewrite branch, server has been renamed to guild, so use bot.guilds instead.

You can modify your on_ready event to print out all the server names.

async

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

    print('Servers connected to:')
    for server in bot.servers:
        print(server.name)

rewrite

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

    print('Servers connected to:')
    for guild in bot.guilds:
        print(guild.name)

Upvotes: 3

cxnnxrmj
cxnnxrmj

Reputation: 21

I've had a little bit of fun with the code. Here is a currently working version.

@bot.event
async def on_ready():
print(f'Currently at {len(bot.guilds)} servers!')
print('Servers connected to:')
print('')
for server in bot.guilds:
    print(server.name)

This should work just fine for you.

Upvotes: 2

Related Questions