Python Discord Bot,需要服务器ID

时间:2018-08-01 16:42:07

标签: python-3.x discord.py

我正在用python创建一个discord bot,并将数据保存在我的计算机上,我在文本文件中使用字典,但是如果我想为多服务器支持诸如config之类的东西,则需要知道服务器的ID。我发现唯一可行的方法是discord.Server.id,但它返回<member 'id' of 'Server' objects>。我想知道是否有其他方法可以在python中做到这一点?我的确切代码是这样的:

@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 *是我的机器人的前缀)时。我不知道此错误的原因或做错了什么,请提供帮助!

谢谢

1 个答案:

答案 0 :(得分:1)

您可以从command invocation context Server获取ctx类的实例。该对象将具有有意义的id属性。

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