【问题标题】:Python Discord Bot, needing the Server IDPython Discord Bot,需要服务器 ID
【发布时间】:2018-08-01 16:42:06
【问题描述】:

我在 python 中创建了一个不和谐的机器人,为了将数据保存在我的计算机上,我在文本文件中使用了一个字典,但是如果我想要多服务器支持类似 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* 是我的机器人的前缀)。我不知道这个错误的原因或我做错了什么,所以请帮忙!

谢谢

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

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

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

    对于,这将是

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

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 2021-07-28
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2018-02-15
      相关资源
      最近更新 更多