Skip to content

Commit

Permalink
fix: 优化导入 命名
Browse files Browse the repository at this point in the history
  • Loading branch information
CakmLexi committed Jun 27, 2024
1 parent 010e46d commit 8e3868c
Show file tree
Hide file tree
Showing 50 changed files with 521 additions and 360 deletions.
3 changes: 3 additions & 0 deletions config/defSet/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ log4jsCfg:
# 日志文件最大大小 MB
maxLogSize: 30

# 关闭后台进程失败后是否继续启动 继续启动会导致多进程
multi_progress: false

# 控制台触发插件日志颜色 十六进制 默认#FFFF00 不支持热更新
log_color: "#E1D919"

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "node-karin",
"version": "0.2.0",
"private": false,
"homepage": "https://github.com/KarinJS/Karin#readme",
"homepage": "https://github.com/KarinJS/Karin",
"bugs": {
"url": "https://github.com/KarinJS/Karin/issues"
},
Expand Down
50 changes: 27 additions & 23 deletions src/adapter/onebot/onebot11.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { KarinMessage } from '../../event/message'
import { KarinNotice } from '../../event/notice'
import { KarinRequest } from '../../event/request'
import WebSocket from 'ws'
import { IncomingMessage } from 'http'
import { randomUUID } from 'crypto'
import { KarinAdapter } from '../../types/adapter'
import logger from '../../utils/logger'
import common from '../../utils/common'
import listener from '../../core/listener'
import config from '../../utils/config'
import { ByPostType, CustomNodeSegment, OneBot11Api, OneBot11ApiParamsType, OneBot11Event, OneBot11Segment } from '../../types/onebots11'
import { Role, Scene, contact } from '../../types/types'
import segment from '../../utils/segment'
import { KarinElement } from '../../types/element'
import { IncomingMessage } from 'http'
import { listener } from 'karin/core/listener'
import { KarinAdapter } from 'karin/types/adapter'
import { common, config, logger, segment } from 'karin/utils/index'
import { KarinMessage, KarinNotice, KarinRequest } from 'karin/event/index'
import {
Role,
Scene,
contact,
ByPostType,
OneBot11Api,
KarinElement,
OneBot11Event,
OneBot11Segment,
CustomNodeSegment,
OneBot11ApiParamsType,
} from 'karin/types/index'

/**
* @class OneBot11
Expand Down Expand Up @@ -1145,7 +1149,7 @@ export class OneBot11 implements KarinAdapter {
let level = 0
try {
level = parseInt(groupMemberInfo.level)
} catch (e) {}
} catch (e) { }
return {
uid: groupMemberInfo.user_id,
uin: groupMemberInfo.user_id,
Expand Down Expand Up @@ -1175,7 +1179,7 @@ export class OneBot11 implements KarinAdapter {
let level = 0
try {
level = parseInt(groupMemberInfo.level)
} catch (e) {}
} catch (e) { }
return {
uid: groupMemberInfo.user_id,
uin: groupMemberInfo.user_id,
Expand Down Expand Up @@ -1298,14 +1302,14 @@ export class OneBot11 implements KarinAdapter {
throw new Error('Method not implemented.')
}

async SetEssenceMessage () {}
async DeleteEssenceMessage () {}
async SetFriendApplyResult () {}
async SetGroupApplyResultRequest () {}
async SetInvitedJoinGroupResult () {}
async ReactMessageWithEmojiRequest () {}
async UploadPrivateFile () {}
async UploadGroupFile () {}
async SetEssenceMessage () { }
async DeleteEssenceMessage () { }
async SetFriendApplyResult () { }
async SetGroupApplyResultRequest () { }
async SetInvitedJoinGroupResult () { }
async ReactMessageWithEmojiRequest () { }
async UploadPrivateFile () { }
async UploadGroupFile () { }
async sendForwardMessage () {
return {}
}
Expand Down
9 changes: 9 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './dir'
export * from './init'
export * from './karin'
export * from './listener'
export * from './plugin'
export * from './plugin.app'
export * from './plugin.loader'
export * from './process'
export * from './server'
22 changes: 21 additions & 1 deletion src/core/init.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
process.title = 'karin'
import { logger } from 'karin/utils/index'

/**
* 启动日志
*/
logger.mark('Karin 启动中...')
logger.mark('https://github.com/KarinJS/Karin')
/**
* 设置标题
*/
process.title = 'Karin'
/**
* 设置时区
*/
process.env.TZ = 'Asia/Shanghai'
/**
* 设置应用模式
*/
process.env.karin_app_mode = process.argv[2]?.includes('dev') ? 'dev' : 'prod'
/**
* 设置语言环境
*/
process.env.karin_app_lang = JSON.stringify(process.execArgv).includes('tsx@') ? 'ts' : 'js'
10 changes: 4 additions & 6 deletions src/core/karin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable no-dupe-class-members */
import common from '@/utils/common'
import { Permission } from '@/types/types'
import { PluginApps } from '@/types/plugin'
import { KarinElement } from '@/types/element'
import { KarinMessage } from '@/event/message'
import PluginApp from './plugin.app'
import { common } from 'karin/utils/index'
import { KarinMessage } from 'karin/event/message'
import { Permission, PluginApps, KarinElement } from 'karin/types/index'

type FncFunction = (e: KarinMessage) => Promise<boolean>
type FncElement = string | KarinElement | Array<KarinElement>
Expand Down Expand Up @@ -62,7 +60,7 @@ export interface OptionsElement extends OptionsCommand {
stop?: boolean
}

export default class Karin {
export class Karin {
/**
* @param reg - 正则表达式
* @param fnc - 函数
Expand Down
24 changes: 10 additions & 14 deletions src/core/listener.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { EventEmitter } from 'events'
import loader from './plugin.loader'
import { KarinAdapter } from '../types/adapter'
import Common from '../utils/common'
import logger from '../utils/logger'
import Config from '../utils/config'
import { contact } from '../types/types'
import { KarinElement } from '../types/element'
import MessageHandller from '../event/message.handler'
import { PluginLoader } from './plugin.loader'
import { common, logger, config } from 'karin/utils/index'
import { MessageHandler } from 'karin/event/message.handler'
import { KarinAdapter, contact, KarinElement } from 'karin/types/index'

/**
* 监听器管理
*/
export default new (class Listeners extends EventEmitter {
export const listener = new (class Listeners extends EventEmitter {
/**
* Bot索引
* @type - Bot索引
Expand All @@ -31,10 +27,10 @@ export default new (class Listeners extends EventEmitter {
this.list = []
this.adapter = []
this.on('error', data => logger.error(data))
this.on('plugin', () => loader.load())
this.on('plugin', () => PluginLoader.load())
this.on('adapter', data => {
let path = data.path || '无'
if (path && data.type !== 'grpc') path = `ws://127.0.0.1:/${Config.Server.http.port}${data.path}`
if (path && data.type !== 'grpc') path = `ws://127.0.0.1:/${config.Server.http.port}${data.path}`
path = logger.green(path)
logger.info(`[适配器][注册][${data.type}] ` + path)
this.addAdapter(data)
Expand All @@ -44,7 +40,7 @@ export default new (class Listeners extends EventEmitter {
logger.info(`[机器人][注册][${data.type}] ` + logger.green(`[account:${data.bot.account.uid || data.bot.account.uin}(${data.bot.account.name})]`))
this.emit('karin:online', data.bot.account.uid || data.bot.account.uin)
})
this.on('message', data => new MessageHandller(data))
this.on('message', data => new MessageHandler(data))
}

/**
Expand Down Expand Up @@ -169,12 +165,12 @@ export default new (class Listeners extends EventEmitter {
if (!bot) throw new Error('发送消息失败: 未找到对应Bot实例')
const { recallMsg, retry_count } = options
/** 标准化 */
const NewElements = Common.makeMessage(elements)
const NewElements = common.makeMessage(elements)

/** 结果 */
let result = { message_id: '' }

const reply_log = Common.makeMessageLog(NewElements)
const reply_log = common.makeMessageLog(NewElements)
const self_id = bot.account.uid || bot.account.uin
if (contact.scene === 'group') {
logger.bot('info', self_id, `Send Proactive Group ${contact.peer}: ${reply_log}`)
Expand Down
2 changes: 1 addition & 1 deletion src/core/plugin.app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirName, fileName, PluginApps } from '@/types/plugin'
import { dirName, fileName, PluginApps } from 'karin/types/index'

export interface PluginAppType {
file?: {
Expand Down
33 changes: 15 additions & 18 deletions src/core/plugin.loader.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import fs from 'fs'
import lodash from 'lodash'
import path from 'path'
import lodash from 'lodash'
import chokidar from 'chokidar'
import schedule from 'node-schedule'
import Renderer from '../renderer/app'
import button from '../utils/button'
import Common from '../utils/common'
import handler from '../utils/handler'
import listener from './listener'
import logger from '../utils/logger'
import Plugin from './plugin'
import { Plugin as PluginType, PluginApps, PluginTask, dirName, fileName, AppInfo } from '../types/plugin'
import { Plugin } from './plugin'
import { listener } from './listener'
import PluginApp from './plugin.app'
import { render } from 'karin/renderer/index'
import { button, common, handler, logger } from 'karin/utils/index'
import { PluginType as PluginType, PluginApps, PluginTask, dirName, fileName, AppInfo } from 'karin/types/index'

/**
* 加载插件
*/
export default new (class PluginLoader {
export const PluginLoader = new (class PluginLoader {
dir: './plugins'
dirPath: string
/**
Expand Down Expand Up @@ -71,7 +68,7 @@ export default new (class PluginLoader {

constructor () {
this.dir = './plugins'
this.dirPath = Common.urlToPath(import.meta.url)
this.dirPath = common.urlToPath(import.meta.url)
this.Apps = []
this.task = []
this.watcher = new Map()
Expand Down Expand Up @@ -112,7 +109,7 @@ export default new (class PluginLoader {

logger.info(`[按钮][${button.Apps.length}个] 加载完成`)
logger.info(`[插件][${this.Apps.length}个] 加载完成`)
logger.info(`[渲染器][${Renderer.Apps.length}个] 加载完成`)
logger.info(`[渲染器][${render.Apps.length}个] 加载完成`)
logger.info(`[定时任务][${this.task.length}个] 加载完成`)
logger.info(`[Handler][Key:${handlerKeys.length}个][fnc:${handlerCount}个] 加载完成`)
logger.info(logger.green('-----------'))
Expand All @@ -129,7 +126,7 @@ export default new (class PluginLoader {
getPlugins () {
const Apps: Array<AppInfo> = []
/** 获取所有插件包 */
const plugins = Common.getPlugins()
const plugins = common.getPlugins()

const isTs = process.env.karin_app_lang === 'ts'

Expand All @@ -141,7 +138,7 @@ export default new (class PluginLoader {
const PluginPath = `${this.dir}/${dir}`

// 非插件包
if (!Common.isPlugin(PluginPath)) {
if (!common.isPlugin(PluginPath)) {
const list = fs.readdirSync(`${this.dir}/${dir}`, {
withFileTypes: true,
})
Expand All @@ -154,25 +151,25 @@ export default new (class PluginLoader {
}

/** js环境 */
if (Common.exists(`${PluginPath}/index.js`)) {
if (common.exists(`${PluginPath}/index.js`)) {
Apps.push({ dir, name: 'index.js' as fileName })
}

const appList = ['apps', 'dist/apps']
appList.forEach(app => {
if (Common.isDir(`${PluginPath}/${app}`)) {
if (common.isDir(`${PluginPath}/${app}`)) {
const result = this.getApps((`${dir}/${app}`))
Apps.push(...result)
}
})

/** ts环境 */
if (isTs) {
if (Common.exists(`${PluginPath}/index.ts`)) {
if (common.exists(`${PluginPath}/index.ts`)) {
Apps.push({ dir, name: 'index.ts' as fileName })
}

if (Common.isDir(`${PluginPath}/src/apps`)) {
if (common.isDir(`${PluginPath}/src/apps`)) {
const result = this.getApps((`${dir}/src/apps`), true)
Apps.push(...result)
}
Expand Down
6 changes: 2 additions & 4 deletions src/core/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Plugin as PluginType } from '../types/plugin'
import { KarinElement, KarinNodeElement } from '../types/element'
import { E } from '@/types/types'
import { PluginType as PluginType, KarinElement, KarinNodeElement, E } from 'karin/types/index'

/**
* 插件基类
*/
export default class Plugin implements PluginType {
export class Plugin implements PluginType {
e!: PluginType['e']
init?: () => void
accept?: (e: E) => Promise<void>
Expand Down
Loading

0 comments on commit 8e3868c

Please sign in to comment.