Jennifer
Jennifer

Reputation: 11

Discord.py - Count how many servers the bot is in

I know it's something like len.client.servers or len(client.servers) but I can't get it to work. I am trying to get it to say "I'm in x servers!" when I say ^botservers. All of the other commands work.

Here's what I have so far:

if message.content.startswith('^botservers'):
  await client.send_message(message.channel, "I'm in " + len(client.servers) + " servers!")

Upvotes: 1

Views: 13876

Answers (4)

Eduardo CF
Eduardo CF

Reputation: 1

Count Guilds!

import discord   

class MyClient(discord.Client):
    async def on_ready(self):
        count = len(self.guilds)
        print(f'Logged on as {count}, your bot {self.user} !')
        
client = MyClient()
client.run(Token)

Upvotes: 0

Just an Developer
Just an Developer

Reputation: 71

Actually you have it!

Just do this

if message.content.startswith('^botservers'):

    await message.channel.send("I'm in " + str(len(client.guilds)) + " servers!")

That makes sure you have an str not an int.

Upvotes: 7

Jam Coder
Jam Coder

Reputation: 31

It's Not client.servers its client.guilds Here is my example

if message.content.startswith('^botservers'):
  await message.channel.send( f"I'm in {len(client.guilds)} servers!")

Upvotes: 1

Mary
Mary

Reputation: 89

len()returns an integer, and you are trying to concatenate it with a string. You should typecast it with str()

if message.content.startswith('^botservers'):
  await client.send_message(message.channel, "I'm in " + str(len(client.servers)) + " servers!")

Also, preferably for better design, you should be using the built-in command handler rather the on_message event to make commands.

Try the following code:

from discord.ext import commands

client = commands.Bot(command_prefix='^')

@client.command(pass_context=True)
async def botservers(ctx):
    await client.say("I'm in " + str(len(client.servers)) + " servers")

client.run("token")

Upvotes: -1

Related Questions