【发布时间】:2021-04-04 02:26:31
【问题描述】:
好吧,所以我很困惑。当我执行命令“前缀”时,出现此错误 返回前缀[str(message.guild.id)] KeyError:'server.id' 这是我得到的所有代码:
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = ['s!', 'S!']
with open('prefixes.json','w') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
prefixes[str(guild.id)] = ['s!', 'S!']
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
命令:
@client.command()
async def prefix(ctx, *, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix set to: **"{prefix}"**')
还有这段代码:
def get_prefix(client, message):
with open('prefixes.json','r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(command_prefix=get_prefix)
请帮帮我!
【问题讨论】:
-
另外,我花了好几个小时才发布这个
-
哦,是的,我也跟着 Lucas 的教程。