Orlando
Orlando

Reputation: 23

Python Discord Bot, needing the Server ID

I am creating a discord bot in python, and to keep the data on my computer I am using a dictionary in a text file, but if I want multi-server support for something like config I need to know the server's id. The only thing I found that could've worked is discord.Server.id but it returns <member 'id' of 'Server' objects>. I was wondering if there is any other way to do this in python? My exact code is this:

@bot.command(pass_context=True)
async def info(ctx):
    await bot.say("ID: {}".format(discord.Server.id))

which returns

ID: <member 'id' of 'Server' objects>

when I run c*info (c* being my bot's prefix). I don't know the cause of this error or what I'm doing wrong so please help!

Thanks

Upvotes: 2

Views: 3891

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60974

You can get an instance of the Server class from the command invocation context ctx. That object will have a meaningful id attribute.

@bot.command(pass_context=True)
async def info(ctx):
    await bot.say("ID: {}".format(ctx.message.server.id))

For , this would be

@bot.command()
async def info(ctx):
    await ctx.send("ID: {}".format(ctx.guild.id))

Upvotes: 2

Related Questions