user11521670
user11521670

Reputation:

How do I get a server's ID with discord.py?

I'm not using discord.ext, just discord. I need to find the server ID that the bot is currently connected to. I've done some googling, but could only find 1 stack overflow question and the answer wasn't helpful. I could really use this, thanks!

Upvotes: 3

Views: 28746

Answers (4)

CatCraft3512
CatCraft3512

Reputation: 1

Without discord.ext it would be:

@client.event
async def on_message(message):
    if message.author == client.user:
        print(f"Guild id: {message.guild.id}")

Upvotes: 0

Justin
Justin

Reputation: 23

I believe you will have to use the ctx.message.guild.id or ctx.guild.id

Here is some example code

@client.command(pass_context = true)
async def getserverid(ctx):
   serverId = ctx.message.guild.id
   await ctx.send(serverId)

Upvotes: 1

Ilan Kleiman
Ilan Kleiman

Reputation: 1209

Here's a way to get the guild (server) ID without using discord.ext, assuming you're using:

async def on_message(self, message: discord.Message) -> None:
    ...

to capture messages/commands in your bot.

message.guild.name and message.guild.id

Would hold the relevant information.

Guild Name: 'My Server Name'
Guild ID: '299299999999999991'

Upvotes: 3

Clonexy700
Clonexy700

Reputation: 111

You didn't specify how you want to get the server ID, so here is an example using the command, you get ID of server, where this command was used.

@client.command(pass_context=True)
async def getguild(ctx):
    id = ctx.message.guild.id

You can check it and output with print for example print(id) will give output like this: enter image description here Also you can just add ctx.send(id) and Discord Bot will send this ID to server chat. Discord guild id have integer type. Now you got ID and can manipulate with it. Read more about it here

Upvotes: 9

Related Questions