Python Discord Bot,需要服务器 ID [英] Python Discord Bot, needing the Server ID
问题描述
我在 python 中创建了一个不和谐的机器人,为了将数据保存在我的计算机上,我使用文本文件中的字典,但是如果我想要多服务器支持类似配置的东西,我需要知道服务器的 id.我发现唯一可行的是discord.Server.id
,但它返回<member 'id' of 'Server' objects>
.我想知道在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-重写,这将是
@bot.command()
async def info(ctx):
await ctx.send("ID: {}".format(ctx.guild.id))
这篇关于Python Discord Bot,需要服务器 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!