Python 在discord.py重写中获取服务器ID
我试图弄清楚如何在discord rewrite中获取服务器ID,以便在单个服务器级别上保存特定设置。有什么办法可以这样做吗 如果您想存储任何数据,我建议您使用JSON文件 只需(获取服务器(重写中的公会)ID: 如果命令:Python 在discord.py重写中获取服务器ID,python,python-3.x,discord,discord.py,discord.py-rewrite,Python,Python 3.x,Discord,Discord.py,Discord.py Rewrite,我试图弄清楚如何在discord rewrite中获取服务器ID,以便在单个服务器级别上保存特定设置。有什么办法可以这样做吗 如果您想存储任何数据,我建议您使用JSON文件 只需(获取服务器(重写中的公会)ID: 如果命令: @bot.command() async def test(ctx): ID = ctx.guild.id 如果事件(例如,在成员上加入()): 如果要将其保存到JSON文件中,可以: @bot.command() async def test(ctx):
@bot.command()
async def test(ctx):
ID = ctx.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)
它将将一个数据转储到JSON文件中。在这些文件中,它将如下所示:
{
"[guild id]": "[content to save]",
}
使用此方法,如果您正在从原始消息/命令中收集上下文,则可以使用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}).')
可能的dup-通过进入用户设置>外观>开发人员模式,您可以将Discord客户端置于开发人员模式,这允许您右键单击服务器并选择“复制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}).')