您好,当它出错时,我希望它为发生错误的服务器自动创建文件夹
def get_prefix(client, message):
with open(f'Data/{message.guild.id}/settings.json', 'r') as f:
prefixes = json.load(f)
return prefixes['prefix']
client = commands.Bot(command_prefix=get_prefix, case_insensitive=True)
答案 0 :(得分:0)
使用try
,except
,如果出现任何错误,我们就可以运行一段代码[....]
当公会已经有了settings.json
时,它将返回前缀!否则,它将为该行会创建一个新的settings.json。
请注意:为data
字典设置一些值!将一些值存储到创建的settings.json
def get_prefix(client, message):
try:
with open(f'Data/{message.guild.id}/settings.json', 'r') as f:
prefixes = json.load(f)
return prefixes['prefix']
except:
data = {'prefix':'!'}#data is dict to save something on it
with open(f'Data/{message.guild.id}/settings.json', 'w') as f:
json.dump(data, f)
return data['prefix']
client = commands.Bot(command_prefix=get_prefix, case_insensitive=True)