Skip to content

Commit

Permalink
fix: 修正未知的通知事件没有return
Browse files Browse the repository at this point in the history
  • Loading branch information
shijinn520 committed Jul 14, 2024
1 parent 311c448 commit 0f20b8c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
"sort": "npx sort-package-json"
},
"dependencies": {
"@grpc/grpc-js": "1.10.10",
"@grpc/grpc-js": "1.10.11",
"@grpc/proto-loader": "0.7.13",
"@inquirer/prompts": "5.1.2",
"art-template": "4.13.2",
"axios": "1.7.2",
"chalk": "5.3.0",
Expand All @@ -64,8 +63,8 @@
"moment": "2.30.1",
"node-schedule": "2.1.1",
"redis": "4.6.14",
"ws": "8.16.0",
"yaml": "2.4.1"
"ws": "8.18.0",
"yaml": "2.4.5"
},
"devDependencies": {
"@types/express": "latest",
Expand Down
4 changes: 3 additions & 1 deletion src/adapter/onebot/11/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class OB11Event {
}

const contact = {
scene: ('group_id' in data ? 'group' : 'friend') as Scene,
scene: 'group_id' in data ? Scene.Group : Scene.Private,
peer: 'group_id' in data ? data.group_id : data.user_id,
sub_peer: '',
}
Expand Down Expand Up @@ -342,6 +342,8 @@ export class OB11Event {
// todo kritor没有这个事件
this.adapter.logger('info', `[群荣誉变更]:${JSON.stringify(data)}`)
break
default:
return this.adapter.logger('error', '未知通知事件:', JSON.stringify(data))
}
break
case 'group_msg_emoji_like': {
Expand Down
14 changes: 10 additions & 4 deletions src/core/plugin.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PluginLoader {
* 插件初始化
*/
async load () {
this.getPlugins()
await this.getPlugins()
listener.once('plugin.watch', () => {
for (const v of this.watchList) {
v.name ? this.watch(v.dir, v.name) : this.watchDir(v.dir)
Expand All @@ -103,6 +103,11 @@ class PluginLoader {

/** 载入插件 */
const promises = this.FileList.map(async ({ dir, name }) => await this.createdApp(dir, name, false))
/** 获取npm插件 */
const npm = await common.getNpmPlugins(true)
/** 载入npm插件 */
promises.push(...npm.map(async ({ dir, name }) => await this.createdApp(dir, name, false, true)))

/** 等待所有插件加载完成 */
await Promise.all(promises)
/** 释放缓存 */
Expand All @@ -117,7 +122,7 @@ class PluginLoader {
/**
* 获取所有插件
*/
getPlugins () {
async getPlugins () {
/** 获取所有插件包 */
const plugins = common.getPlugins()

Expand Down Expand Up @@ -274,11 +279,12 @@ class PluginLoader {
* @param dir - 插件包路径
* @param name - 插件名称
* @param isOrderBy - 是否为动态导入 默认为静态导入
* @param isNpm - 是否为npm包
*/
async createdApp (dir: dirName, name: fileName, isOrderBy = false) {
async createdApp (dir: dirName, name: fileName, isOrderBy = false, isNpm = false) {
try {
const list: Promise<any>[] = []
let path = `${this.dirPath}plugins/${dir}/${name}`
let path = `${this.dirPath}${isNpm ? 'node_modules ' : 'plugins'}/${dir}/${name}`
if (isOrderBy) path = path + `?${Date.now()}`

const tmp: Array<(NewMessagePlugin) | PluginApps> = await import(path)
Expand Down
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/// <reference types="@types/express" />
/// <reference types="@types/lodash" />
/// <reference types="@types/node" />
/// <reference types="@types/node-schedule" />
/// <reference types="@types/ws" />

// 基本模块
export * from 'kritor-proto'
export * from 'karin/core'
Expand Down
4 changes: 3 additions & 1 deletion src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinEventTypes, Kar
* - 插件根目录名称
* - 例如: karin-plugin-example
* - 例如: karin-plugin-example/apps
* - ### npm包类型: `string`
* - ### git插件类型: `karin-plugin-${string}`
*/
export type dirName = `karin-plugin-${string}`
export type dirName = `karin-plugin-${string}` | string
/**
* - 插件名称
* - 例如: index.js index.ts
Expand Down
58 changes: 56 additions & 2 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AxiosRequestConfig } from 'axios'
import { pipeline, Readable } from 'stream'
import { logger, segment } from 'karin/utils'
import { fs, path, axios, lodash, yaml as Yaml } from 'karin/modules'
import { dirName, KarinElement, NodeElement } from 'karin/types'
import { dirName, fileName, KarinElement, NodeElement } from 'karin/types'

/**
* 常用方法
Expand Down Expand Up @@ -381,7 +381,7 @@ export const common = new (class Common {
}

/**
* 获取所有插件列表
* 获取git插件列表
* @param isPack - 是否屏蔽不带package.json的插件,默认为false
*/
getPlugins (isPack = false): Array<dirName> {
Expand All @@ -395,6 +395,60 @@ export const common = new (class Common {
return arr
}

/**
* 获取npm插件列表
* @param showDetails - 是否返回详细信息,默认为false
* 默认只返回插件npm包名,为true时返回详细的{dir, name}[]
*/
async getNpmPlugins<T extends boolean> (showDetails: T): Promise<T extends true ? { dir: string; name: fileName }[] : string[]> {
/** 屏蔽的依赖包列表 */
const pkgdependencies = [
'@grpc/grpc-js',
'@grpc/proto-loader',
'art-template',
'axios',
'chalk',
'chokidar',
'express',
'kritor-proto',
'level',
'lodash',
'log4js',
'moment',
'node-schedule',
'redis',
'ws',
'yaml',
]
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
const dependencies = Object.keys(pkg.dependencies).filter((name) => !pkgdependencies.includes(name))

if (!showDetails) {
return dependencies as T extends true ? { dir: string; name: fileName }[] : string[]
} else {
const list: { dir: string; name: string }[] = []

const readPackageJson = async (name: string) => {
try {
const pkgPath = path.join(process.cwd(), 'node_modules', name, 'package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
if (pkg?.karin && pkg?.karin?.apps?.length) {
pkg.karin.apps.forEach((app: string) => {
fs.readdirSync(`./node_modules/${name}/${app}`).forEach((dir: string) => {
/** 忽略非js */
if (!dir.endsWith('.js')) return
list.push({ dir, name })
})
})
}
} catch { }
}

await Promise.all(dependencies.map(readPackageJson))
return list as T extends true ? { dir: string; name: fileName }[] : string[]
}
}

/**
* 获取运行时间
*/
Expand Down

0 comments on commit 0f20b8c

Please sign in to comment.