好吧,所以我太困惑了。当我执行我的命令"prefix“时,我得到了这个错误return prefixesstr(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)请帮帮我!
发布于 2021-03-09 23:49:45
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)
https://stackoverflow.com/questions/65458585
复制相似问题