好吧,所以我很困惑。当我执行命令“前缀”时,出现此错误 返回前缀[str(message.guild.id)] 密钥错误:'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)
请帮帮我!
答案 0 :(得分:0)
nvm 得到答案:
def get_prefix(client, message):
with open(....) as ...:
prefixes = json.load(...)
try:
# this will error if the guildid is not in the json
return prefixes[str(message.guild.id)]
except:
# this code will run if the stuff in the if errors
# here you need to add the guildid with your default prefix to the json
# the code is pretty similar to what you have in your on_guild_join event, youll just need to change some stuff, like how to get the current guild
# when you did that then do what you had in the try like this:
# it shouldnt error now cause the guildid is now in the json
return prefixes[str(message.guild.id)]
感谢您的帮助 :D (lol)