在 discord.py 重写中获取服务器 ID
Get server ID in discord.py rewrite
我试图弄清楚如何在 discord 重写中获取服务器 ID,以便我可以在单个服务器级别的基础上保存特定设置。有没有办法做到这一点?
如果你想存储任何我推荐给你的数据 JSON 文件
简单地(获取服务器(重写公会)ID:
如果命令:
@bot.command()
async def test(ctx):
ID = ctx.guild.id
if 事件(例如 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]",
}
用这个方法你想存多少就存多少
如果您从原始 message/command 收集上下文,那么您可以使用 ctx.guild.name
到 return 名称或 ctx.guild.id
到 return发布命令的公会 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}).')
我试图弄清楚如何在 discord 重写中获取服务器 ID,以便我可以在单个服务器级别的基础上保存特定设置。有没有办法做到这一点?
如果你想存储任何我推荐给你的数据 JSON 文件 简单地(获取服务器(重写公会)ID: 如果命令:
@bot.command()
async def test(ctx):
ID = ctx.guild.id
if 事件(例如 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]",
}
用这个方法你想存多少就存多少
如果您从原始 message/command 收集上下文,那么您可以使用 ctx.guild.name
到 return 名称或 ctx.guild.id
到 return发布命令的公会 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}).')