【发布时间】:2020-08-24 15:50:24
【问题描述】:
我试图弄清楚如何在不和谐重写中获取服务器 ID,以便我可以在单个服务器级别保存特定设置。有没有办法做到这一点?
【问题讨论】:
-
通过进入用户设置>外观>开发者模式,你可以将你的Discord客户端设置为开发者模式,这样你就可以在服务器上右击并选择“复制ID”
标签: python python-3.x discord discord.py discord.py-rewrite
我试图弄清楚如何在不和谐重写中获取服务器 ID,以便我可以在单个服务器级别保存特定设置。有没有办法做到这一点?
【问题讨论】:
标签: python python-3.x discord discord.py discord.py-rewrite
如果您从原始消息/命令中收集上下文,则可以使用 ctx.guild.name 返回名称或使用 ctx.guild.id 返回发布命令的公会 ID。
例子:
bot = commands.Bot(command_prefix='!')
@bot.command(name='whereami', help='print the current server name/id')
async def whereami(ctx):
await ctx.send(f'{ctx.author.name}, you are currently in {ctx.guild.name} ({ctx.guild.id}).')
【讨论】:
如果您想存储我推荐给您的任何数据 JSON 文件 简单地(获取服务器(重写中的公会)ID: 如果命令:
@bot.command()
async def test(ctx):
ID = ctx.guild.id
如果事件(例如 on_member_join()):
@bot.event()
async def on_member_join(member):
ID = member.guild.id
如果您想将其保存到 JSON 文件中,您可以:
@bot.command()
async def test(ctx):
ID[str(ctx.guild.id)] = [content to save with specific ID]
with open("data.json", "w") as f:
json.dump(ID, f, indent=4)
它将dump 一个数据转换为 JSON 文件。在此文件中,它将如下所示:
{
"[guild id]": "[content to save]",
}
使用这种方法,您可以节省尽可能多的钱
【讨论】: