How do I get the server ID of the server that my Discord bot is connected to?
I’m using the Discord API with discord, not discord.ext. I need to find the server ID of the server the bot is currently connected to.
I’ve already looked for an answer online, but wasn’t able to find anything useful. Is there a way to get the server ID of the server the bot is connected to?
# My code
import discord
client = discord.Client()
# ...
Yes, you can get the server ID of the server your Discord bot is connected to using the guild attribute of the Client object. Here’s an example:
# My code
import discord
client = discord.Client()
@client.event
async def on_ready():
# This will print the server ID of the first server the bot is connected to.
print(client.guilds[0].id)
# ...
You can access the guilds attribute of the Client object to get a list of all the servers the bot is connected to.