Skip to content

Commit

Permalink
fix: 插件实例化的时候传递事件
Browse files Browse the repository at this point in the history
  • Loading branch information
shijinn520 committed Jul 8, 2024
1 parent 0efaede commit f9e9c61
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/adapter/kritor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ export default class AdapterKritor implements KarinAdapter {
* 响应解码
*/
const response = kritor[type][`${cmd}Response`].decode(res.buf)
return response
return {
message_id: response.message_id,
message_time: Number(response.message_time),
}
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/core/plugin.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { listener } from './listener'
import PluginApp from './plugin.app'
import { render } from 'karin/render'
import { common, handler, logger } from 'karin/utils'
import { PluginApps, PluginTask, dirName, fileName, AppInfo } from 'karin/types'
import { PluginApps, PluginTask, dirName, fileName, AppInfo, NewPlugin } from 'karin/types'

class PluginLoader {
dir: './plugins'
Expand Down Expand Up @@ -52,7 +52,7 @@ class PluginLoader {
/**
* - 定时任务
*/
task: Array<PluginTask & { App: new () => Plugin, schedule?: schedule.Job, file: { dir: dirName, name: fileName } }>
task: Array<PluginTask & { App: NewPlugin, schedule?: schedule.Job, file: { dir: dirName, name: fileName } }>

/**
* - 监听器
Expand Down Expand Up @@ -277,7 +277,7 @@ class PluginLoader {
let path = `${this.dirPath}plugins/${dir}/${name}`
if (isOrderBy) path = path + `?${Date.now()}`

const tmp: Array<(new () => Plugin) | PluginApps> = await import(path)
const tmp: Array<(NewPlugin) | PluginApps> = await import(path)

lodash.forEach(tmp, (App) => {
const index = this.index
Expand All @@ -301,7 +301,7 @@ class PluginLoader {

if (typeof App !== 'function' || !App?.prototype?.constructor) return

const Class = new (App as new () => Plugin)()
const Class = new (App as NewPlugin)()
if (!Class.name) return logger.error(`[${dir}][${name}] 插件名称错误`)
logger.debug(`载入插件 [${name}][${Class.name}]`)

Expand Down Expand Up @@ -390,7 +390,7 @@ class PluginLoader {
/**
* 新增task
*/
async addTask (dir: dirName, name: fileName, index: number, Class: Plugin, App: new () => Plugin) {
async addTask (dir: dirName, name: fileName, index: number, Class: Plugin, App: NewPlugin) {
/** 定时任务 */
lodash.forEach(Class.task, val => {
if (!val.name) return logger.error(`[${dir}][${name}] 定时任务name错误`)
Expand Down
6 changes: 3 additions & 3 deletions src/event/message.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import lodash from 'lodash'
import { review } from './review.handler'
import EventHandler from './event.handler'
import { logger, config } from 'karin/utils'
import { KarinMessageEvent } from 'karin/types'
import { listener, Plugin, stateArr, pluginLoader } from 'karin/core'
import { KarinMessageEvent, NewPlugin } from 'karin/types'
import { listener, stateArr, pluginLoader } from 'karin/core'

/**
* 消息事件
Expand Down Expand Up @@ -74,7 +74,7 @@ export class MessageHandler extends EventHandler {
if (app.file.type === 'function' && typeof v.fnc === 'function') {
res = await v.fnc(this.e)
} else {
const cla = new (app.file.Fnc as new () => Plugin)()
const cla = new (app.file.Fnc as NewPlugin)()
cla.e = this.e
res = await (cla[v.fnc as keyof typeof cla] as Function)(this.e) as Promise<boolean>
}
Expand Down
8 changes: 7 additions & 1 deletion src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import schedule from 'node-schedule'
import { Plugin } from 'karin/core'
import { Reply, replyCallback, replyForward } from './reply'
import { EventType, Event, Permission, SubEvent, KarinMessageEvent, KarinNoticeEvent, KarinRequestEvent } from './event'
import { Plugin } from 'karin/core'
import { KarinMessage } from 'karin/event'

/**
* - 插件根目录名称
Expand Down Expand Up @@ -282,3 +283,8 @@ export interface PluginApps {
*/
handler: Array<PluginHandler>
}

/**
* 未实例化的插件
*/
export type NewPlugin = new (e?: KarinMessage) => Plugin
6 changes: 3 additions & 3 deletions src/utils/button.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logger from './logger'
import { KarinMessageEvent } from 'karin/types'
import { pluginLoader as loader, Plugin } from 'karin/core'
import { KarinMessageEvent, NewPlugin } from 'karin/types'
import { pluginLoader as loader } from 'karin/core'

export const button = async (e: KarinMessageEvent) => {
const button = []
Expand All @@ -19,7 +19,7 @@ export const button = async (e: KarinMessageEvent) => {
if (typeof v.fnc === 'function') {
res = await v.fnc(e, reject)
} else {
const cla = new (info.file.Fnc as new () => Plugin)()
const cla = new (info.file.Fnc as NewPlugin)()
cla.e = e
res = await (cla[v.fnc as keyof typeof cla] as Function)(e, reject)
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import lodash from 'lodash'
import logger from './logger'
import { EventType, PluginType, PluginApps } from 'karin/types'
import { Plugin, pluginLoader as loader } from 'karin/core'
import { pluginLoader as loader } from 'karin/core'
import { EventType, PluginType, PluginApps, NewPlugin } from 'karin/types'

/**
* 事件处理器类
Expand Down Expand Up @@ -92,7 +92,7 @@ export const handler = new (class EventHandler {
if (info.file.type === 'function' && typeof v.fnc === 'function') {
res = await v.fnc(args, reject)
} else {
const cla = new (info.file.Fnc as new () => Plugin)()
const cla = new (info.file.Fnc as NewPlugin)()
cla.e = args.e as EventType<typeof args.e>
res = await (cla[v.fnc as keyof typeof cla] as Function)(args, reject)
}
Expand Down

0 comments on commit f9e9c61

Please sign in to comment.