diff --git a/src/Bot/App.ts b/src/Bot/App.ts index 55416d7..5d8bbae 100644 --- a/src/Bot/App.ts +++ b/src/Bot/App.ts @@ -1,5 +1,5 @@ import { magenta, white, yellow } from 'colors' -import { existsSync, mkdirSync, readJSONSync } from 'fs-extra' +import { existsSync, readJSONSync } from 'fs-extra' import { cpu, mem } from 'node-os-utils' import { join } from 'path' import { secondsFormat } from '..' @@ -17,14 +17,13 @@ export class App { * @param dirname 插件设置保存位置 * @param saveConfig 是否保存插件设置到本地 */ - constructor(name: string = 'Bot', dirname?: string, saveConfig: boolean = true) { - let dir: string - if (saveConfig) { + constructor(name: string = 'Bot', dirname?: string | false) { + let dir: string = null + if (dirname === false) { if (!dirname) { dir = join(require.main.path, '../config/') - } - if (!existsSync(dir)) { - mkdirSync(dir) + } else { + dir = dirname } } this.Bot = new Bot(name, dir) @@ -262,17 +261,17 @@ export class App { } }) - let config: any = {} - const configPath = join(this.Bot.Plugin.dirname, `./${this.Bot.Data.name}-config.json`) - if (existsSync(configPath)) { - try { - config = readJSONSync(configPath) - for (let i in config) { - this.Bot.Plugin.config[i] = config[i] + if (this.Bot.Plugin.dirname) { + let config: any = {} + const configPath = join(this.Bot.Plugin.dirname, `./${this.Bot.Data.name}-config.json`) + if (existsSync(configPath)) { + try { + config = readJSONSync(configPath) + this.Bot.Plugin.config = {...this.Bot.Plugin.config, ...config} + this.Bot.Log.logNotice('本地配置加载成功', this.Bot.Data.name) + } catch { + this.Bot.Log.logError('本地配置加载失败', this.Bot.Data.name) } - this.Bot.Log.logNotice('本地配置加载成功', this.Bot.Data.name) - } catch { - this.Bot.Log.logError('本地配置加载失败', this.Bot.Data.name) } } diff --git a/src/Bot/modules/Plugin.ts b/src/Bot/modules/Plugin.ts index 3479307..4a14609 100644 --- a/src/Bot/modules/Plugin.ts +++ b/src/Bot/modules/Plugin.ts @@ -1,6 +1,6 @@ -import { BotPlugin as ClassPlugin, PluginConfig, AnonymousPlugin } from '../..' +import { existsSync, mkdirSync, writeJSONSync } from 'fs-extra' +import { AnonymousPlugin, BotPlugin as ClassPlugin, PluginConfig } from '../..' import { Bot } from '../Bot' -import fs = require('fs-extra') import path = require('path') export class Plugin { @@ -32,8 +32,11 @@ export class Plugin { saveConfig(): void { if (this.dirname) { + if (!existsSync(this.dirname)) { + mkdirSync(this.dirname) + } try { - fs.writeJSONSync(path.join(this.dirname, `./${this.Bot.Data.name}-config.json`), this.config, { + writeJSONSync(path.join(this.dirname, `./${this.Bot.Data.name}-config.json`), this.config, { spaces: 2 }) } catch (err) {