Python Discord Bot,需要服务器ID [英] Python Discord Bot, needing the Server ID
问题描述
我正在用python创建一个discord机器人,并将数据保存在我的计算机上,我在文本文件中使用了字典,但是如果我想对config之类的东西进行多服务器支持,则需要知道服务器的ID。我发现唯一可行的方法是 discord.Server.id
,但它返回<服务器对象的成员 id>。
。我想知道是否还有其他方法可以在python中做到这一点?我的确切代码是这样的:
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))
返回
ID: <member 'id' of 'Server' objects>
当我运行c * info(c *是我的机器人的前缀)时。我不知道此错误的原因或我在做什么错,所以请帮忙!
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!
谢谢
推荐答案
您可以从 ctx
。该对象将具有有意义的 id
属性。
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))
对于 discord.py-rewrite 的问题,这将是
@bot.command()
async def info(ctx):
await ctx.send("ID: {}".format(ctx.guild.id))
这篇关于Python Discord Bot,需要服务器ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!