Skip to content

Commit

Permalink
fix: 修复本地配置相关错误
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktunes committed Aug 17, 2021
1 parent 317d55a commit 8af8e26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
33 changes: 16 additions & 17 deletions src/Bot/App.ts
Original file line number Diff line number Diff line change
@@ -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 '..'
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/Bot/modules/Plugin.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8af8e26

Please sign in to comment.