Skip to content

Commit

Permalink
feat: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktunes committed Jan 16, 2022
1 parent 577c0af commit 13b4bde
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 121 deletions.
37 changes: 23 additions & 14 deletions src/Bot/App.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { magenta, white, yellow } from 'colors'
import { existsSync, readJSONSync, removeSync } from 'fs-extra'
import { merge } from 'lodash'
import { cpu, mem } from 'node-os-utils'
import { readSync, writeSync } from 'node-yaml'
import { join } from 'path'
import { secondsFormat } from '..'
import { Connect } from '../Connect/Connect'
import { BotPlugin } from '../Tools'
import { Plugin, PluginFunction, WebSocketConfig } from '../Type'
import { BotPlugin } from '../Plugin'
import { AnonymousPlugin, Plugin, PluginFunction, WebSocketConfig } from '../Type'
import { Bot } from './Bot'
import { Api } from './modules/Api'
import { Event } from './modules/Event'
Expand Down Expand Up @@ -272,19 +273,27 @@ export class App {
const jsonPath = join(this.Bot.Plugin.dirname, `./${this.Bot.Plugin.filename}.json`)
const ymlPath = join(this.Bot.Plugin.dirname, `./${this.Bot.Plugin.filename}.yml`)
try {
let config: { config: any } = { config: {} }
let config: { config?: any, plugin?: any, bot?: any } = { plugin: {}, bot: {} }
if (existsSync(jsonPath)) {
config = readJSONSync(jsonPath)
removeSync(jsonPath)
writeSync(ymlPath, config)
} else if (existsSync(ymlPath)) {
}
if (existsSync(ymlPath)) {
config = readSync(ymlPath)
}
for (const name in config.config) {
this.Bot.Plugin.setConfig(name, config.config[name])
if (config.config && Object.keys(config.config).length > 0) {
config.plugin = merge(config.plugin, config.config)
delete config.config
writeSync(ymlPath, config)
}
for (const name in config.plugin) {
this.Bot.Plugin.setConfig(name, config.plugin[name])
}
merge(this.Bot.Data.config, config.bot)
this.Bot.Log.logNotice('本地配置加载成功', this.Bot.Data.name)
} catch {
} catch (err) {
console.error(err)
this.Bot.Log.logError('本地配置加载失败', this.Bot.Data.name)
}
}
Expand All @@ -299,25 +308,25 @@ export class App {
if (this.Bot.Plugin.list.length > 0) {
this.Bot.Log.logInfo('开始初始化插件', this.Bot.Data.name)
for (let i in this.Bot.Plugin.list) {
if (this.Bot.Plugin.list[i].name === '匿名插件') {
await this.Bot.Plugin.list[i].init(this.Bot)
this.Bot.Log.logNotice(`${yellow(this.Bot.Plugin.list[i].name)} 已加载`, '插件')
const plugin = this.Bot.Plugin.list[i] as BotPlugin
if (plugin.name === '匿名插件') {
await (plugin as AnonymousPlugin).init(this.Bot)
this.Bot.Log.logNotice(`${yellow(plugin.name)} 已加载`, '插件')
} else {
const plugin = this.Bot.Plugin.list[i] as BotPlugin
if (plugin.config.enable === undefined || plugin.config.enable) {
await plugin.init()
if (plugin.config.auto_save === undefined || plugin.config.auto_save) {
plugin.autoSave()
}
this.Bot.Log.logNotice(`${yellow(this.Bot.Plugin.list[i].name)} 已加载`, '插件')
this.Bot.Log.logNotice(`${yellow(plugin.name)} 已加载`, '插件')
} else {
this.Bot.Log.logWarning(`${white(this.Bot.Plugin.list[i].name)} 已被禁用`, '插件')
this.Bot.Log.logWarning(`${white(plugin.name)} 已被禁用`, '插件')
}
plugin.autoSave = (): void => {
this.Bot.Log.logWarning('请勿重复执行autoSave')
}
}
this.Bot.Plugin.list[i].init = (): void => {
plugin.init = (): void => {
this.Bot.Log.logWarning('请勿重复执行init')
}
}
Expand Down
40 changes: 10 additions & 30 deletions src/Bot/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,24 @@ export class Bot {
this.Command = new Command(this)
this.Debug = new Debug(this)
}
/**
* Bot的部分属性
*/
/** Bot的部分属性 */
Data: Data
/**
* 日志对象
*/
/** 日志对象 */
Log: Log
/**
* CQ码
*/
/** CQ码 */
CQCode: CQCODE = CQCode
/**
* 插件对象
*/
/** 插件对象 */
Plugin: Plugin
/**
* 命令对象
*/
/** 命令对象 */
Command: Command
/**
* 管理员命令
*/
/** 管理员命令 */
Admin: Admin
/**
* WS链接
*/
/** WS链接 */
Conn: Connect
/**
* HTTP API
*/
/** HTTP API */
Api: Api
/**
* 消息事件
*/
/** 消息事件 */
Event: Event
/**
* 测试类
*/
/** 测试类 */
Debug: Debug
}
41 changes: 11 additions & 30 deletions src/Bot/modules/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ export class Command {
this.Bot = Bot
}
private Bot: Omit<Bot, 'Command'>
/**
* 命令列表
*/
/** 命令列表 */
list: Comm[] = []

/**
* 增加命令
*/
/** 增加命令 */
command(name: string) {
const repeat = this.list.some(item => {
return item.comm.includes(name)
Expand Down Expand Up @@ -66,9 +62,8 @@ export class SetComm {
private comm: Comm
private repeat: boolean
private plugin: boolean
/**
* 增加指令别名
*/

/** 增加指令别名 */
alias(name: string) {
const repeat = this.Bot.Command.list.some(item => {
return item.comm.includes(name)
Expand All @@ -79,39 +74,29 @@ export class SetComm {
this.comm.comm.push(name)
}
}
/**
* 增加命令描述,重复调用会被覆盖
*/
/** 增加命令描述,重复调用会被覆盖 */
desc(text: string) {
this.comm.desc = text
return this
}
/**
* 增加正则规则,调用后命令名将会失效
*/
/** 增加正则规则,调用后命令名将会失效 */
reg(reg: RegExp | NamedRegExp) {
if (this.repeat) return this
this.comm.reg = reg
this.comm.desc += '(动态指令)'
return this
}
/**
* 是否为管理员指令
*/
/** 是否为管理员指令 */
admin(flag = true) {
this.comm.admin = flag
return this
}
/**
* 设置命令分类
*/
/** 设置命令分类 */
group(name: string) {
this.comm.group = name
return this
}
/**
* 增加命令处理方法,可添加多个
*/
/** 增加命令处理方法,可添加多个 */
action(type: 'private', fn: (e: PrivateMsg) => Prevent): this
action(type: 'group', fn: (e: GroupMsg) => Prevent): this
action(type: 'group' | 'private', fn: (e: any) => Prevent) {
Expand All @@ -123,9 +108,7 @@ export class SetComm {
}
return this
}
/**
* 增加白名单列表,请勿和黑名单同时使用
*/
/** 增加白名单列表,请勿和黑名单同时使用 */
white(list: number[]) {
if (this.repeat || list.length < 1) return this
if (this.comm.blacklist.size > 0) {
Expand All @@ -135,9 +118,7 @@ export class SetComm {
this.comm.whitelist = new Set(([...this.comm.whitelist, ...list]))
return this
}
/**
* 增加黑名单列表,请勿和白名单同时使用
*/
/** 增加黑名单列表,请勿和白名单同时使用 */
black(list: number[]) {
if (this.repeat || list.length < 1) return this
if (this.comm.whitelist.size > 0) {
Expand Down
12 changes: 3 additions & 9 deletions src/Bot/modules/Debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ export class Debug {
this.Bot.Log.logWarning(`已开启debug模式,所有api调用都不会真正执行`, 'DEBUG')
}
}
/**
* 是否为调试模式
*/
/** 是否为调试模式 */
get debug(): boolean {
return this._debug
}

/**
* 可以用于群消息相功能的测试
*/
/** 可以用于群消息相功能的测试 */
groupMsgTest(data: Partial<_GroupMsg>): void
groupMsgTest(msg: string, user_id?: number, group_id?: number): void
groupMsgTest(data: Partial<_GroupMsg> | string, user_id?: number, group_id?: number): void {
Expand Down Expand Up @@ -65,9 +61,7 @@ export class Debug {
this.Bot.Conn.eventTest(test)
}

/**
* 可以用于私聊消息相功能的测试
*/
/** 可以用于私聊消息相功能的测试 */
privateMsgTest(data: Partial<_PrivateMsg>): void
privateMsgTest(msg: string, user_id?: number): void
privateMsgTest(data: Partial<_PrivateMsg> | string, user_id?: number): void {
Expand Down
27 changes: 11 additions & 16 deletions src/Bot/modules/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync } from 'fs-extra'
import { readSync, writeSync } from 'node-yaml'
import { AnonymousPlugin, BotPlugin as ClassPlugin } from '../..'
import { AnonymousPlugin, } from '../../Type'
import { BotPlugin } from '../../Plugin/Plugin'
import { Bot } from '../Bot'
import path = require('path')
import { merge } from 'lodash'
Expand All @@ -13,21 +14,15 @@ export class Plugin {
}
private Bot: Omit<Bot, 'Plugin'>

/**
* 插件列表
*/
list: (ClassPlugin | AnonymousPlugin)[] = []
/** 插件列表 */
list: (BotPlugin | AnonymousPlugin)[] = []

/**
* 本地设置保存位置
*/
/** 本地设置保存位置 */
readonly dirname: string
/**
* 本地设置文件名
*/
/** 本地设置文件名 */
readonly filename: string

setConfig(name, config: any) {
setConfig(name: string, config: any) {
const plugin = this.getPlugin(name)
if (plugin && plugin.name === name) {
plugin['config'] = merge(plugin['config'], config)
Expand Down Expand Up @@ -57,10 +52,10 @@ export class Plugin {
if (!existsSync(this.dirname)) {
mkdirSync(this.dirname)
}
let config = { config: {} }
let config = { plugin: {} }
this.list.forEach(plugin => {
if (plugin['config'] && Object.keys(plugin['config']).length > 0) {
config.config[plugin.name] = plugin['config']
config.plugin[plugin.name] = plugin['config']
}
})
const ymlPath = path.join(this.dirname, `./${this.filename}.yml`)
Expand All @@ -76,7 +71,7 @@ export class Plugin {
this.saveFlag = false
}

getPlugin<T extends ClassPlugin | AnonymousPlugin>(name: string): Omit<T, 'init'> | undefined {
getPlugin<T extends BotPlugin | AnonymousPlugin>(name: string): Omit<T, 'init'> | undefined {
const plugin = this.list.find(i => i.name === name)
if (plugin) {
return plugin as T as Omit<T, 'init'>
Expand All @@ -85,4 +80,4 @@ export class Plugin {
return undefined
}
}
}
}
28 changes: 7 additions & 21 deletions src/Plugin/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export class Command {
}
})
}
/**
* 为插件指令增加白名单列表,请勿和黑名单同时使用
*/
/** 为插件指令增加白名单列表,请勿和黑名单同时使用 */
white(list: number[]) {
if (list.length < 1) return this
if (this._blacklist.size > 0) {
Expand All @@ -32,15 +30,11 @@ export class Command {
this.whitelist = list
return this
}
/**
* 获取白名单
*/
/** 获取白名单 */
getWhitelist(): Readonly<number[]> {
return [...this._whitelist]
}
/**
* 移除白名单
*/
/** 移除白名单 */
removeWhitelist(list: number[] | readonly number[]) {
for (const id of list) {
this.Bot.Command.list.forEach(item => {
Expand All @@ -60,9 +54,7 @@ export class Command {
}
})
}
/**
* 为插件指令增加黑名单列表,请勿和白名单同时使用
*/
/** 为插件指令增加黑名单列表,请勿和白名单同时使用 */
black(list: number[]) {
if (list.length < 1) return this
if (this._whitelist.size > 0) {
Expand All @@ -72,15 +64,11 @@ export class Command {
this.blacklist = list
return this
}
/**
* 获取黑名单
*/
/** 获取黑名单 */
getBlacklist(): readonly number[] {
return [...this._blacklist]
}
/**
* 移除黑名单
*/
/** 移除黑名单 */
removeBlacklist(list: number[]| readonly number[]) {
for (const id of list) {
this.Bot.Command.list.forEach(item => {
Expand All @@ -91,9 +79,7 @@ export class Command {
}
}

/**
* 增加命令
*/
/** 增加命令 */
command = (name: string) => {
const repeat = this.Bot.Command.list.some(item => {
return item.comm.includes(name)
Expand Down
Loading

0 comments on commit 13b4bde

Please sign in to comment.