Skip to content

Commit

Permalink
fix: #49
Browse files Browse the repository at this point in the history
  • Loading branch information
CakmLexi committed Jul 4, 2024
1 parent 4df752a commit 5b5ded9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 45 deletions.
45 changes: 44 additions & 1 deletion src/core/karin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import PluginApp from './plugin.app'
import { common } from 'karin/utils'
import { render } from 'karin/render'
import { stateArr } from './plugin'
import { listener } from './listener'
import { KarinMessage } from 'karin/event/message'
import { Permission, PluginApps, KarinElement, contact, KarinRenderType } from 'karin/types'
import { render } from 'karin/render'

type FncFunction = (e: KarinMessage) => Promise<boolean>
type FncElement = string | KarinElement | Array<KarinElement>
Expand Down Expand Up @@ -222,4 +224,45 @@ export class Karin {
render (options: KarinRenderType) {
return render.render(options)
}

/**
* - 上下文
* @param e - 消息事件
*/
async ctx (e: KarinMessage, options?: {
/**
* - 指定用户id触发下文 不指定则使用默认e.user_id
*/
userId?: string
/**
* - 超时时间 默认120秒
*/
time?: number
/**
* - 超时后是否回复
*/
reply?: boolean
/**
* - 超时回复文本 默认为'操作超时已取消'
*/
replyMsg?: string
}): Promise<KarinMessage> {
const time = options?.time || 120
const userId = options?.userId || e.user_id
const key = e.group_id ? `${e.group_id}.${userId}` : userId
stateArr[key] = { type: 'ctx' }
// 返回promise 设置超时时间
return new Promise((resolve, reject) => {
setTimeout(() => {
if (stateArr[key]) {
delete stateArr[key]
if (options?.reply) e.reply(options.replyMsg || '操作超时已取消')
reject(new Error('操作超时已取消'))
return true
}
}, time * 1000)

listener.once(`ctx:${key}`, (e: KarinMessage) => resolve(e))
})
}
}
27 changes: 11 additions & 16 deletions src/core/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginType, KarinElement, KarinNodeElement, EventType, KarinNoticeEvent, KarinRequestEvent } from 'karin/types'
import { PluginType, KarinElement, KarinNodeElement, EventType, KarinNoticeEvent, KarinRequestEvent, stateArrType } from 'karin/types'

/**
* 插件基类
Expand Down Expand Up @@ -178,7 +178,7 @@ export class Plugin implements PluginType {
/**
* @param fnc - 执行方法
*/
fnc: string,
fnc: string | Function,
/**
* @param reply - 超时后是否回复
*/
Expand All @@ -189,7 +189,13 @@ export class Plugin implements PluginType {
time = 120
) {
const key = this.conKey()
stateArr[key] = { plugin: this, fnc }

if (typeof fnc === 'string') {
stateArr[key] = { type: 'class', fnc: this, name: fnc }
} else {
stateArr[key] = { type: 'fnc', fnc }
}

/** 操作时间 */
this.timeout = setTimeout(() => {
if (stateArr[key]) {
Expand All @@ -202,7 +208,7 @@ export class Plugin implements PluginType {
/**
* 获取上下文状态
*/
getContext (): { plugin: Plugin, fnc: string } {
getContext (): stateArrType[string] {
const key = this.conKey()
return stateArr[key]
}
Expand All @@ -223,18 +229,7 @@ export class Plugin implements PluginType {
/**
* 上下文状态
*/
export const stateArr: {
[key: string]: {
/**
* @param plugin - 插件实例
*/
plugin: Plugin
/**
* @param fnc - 执行方法名称
*/
fnc: string
}
} = {}
export const stateArr: stateArrType = {}

/**
* 通知事件 插件类型
Expand Down
35 changes: 27 additions & 8 deletions src/event/message.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,33 @@ export class MessageHandler extends EventHandler {
const key = this.e.isGroup ? `${this.e.group_id}.${this.e.user_id}` : this.e.user_id
const App = stateArr[key]
if (App) {
const { plugin, fnc } = App
this.e.logFnc = `[${plugin.name}][${fnc}]`
/** 计算插件处理时间 */
const start = Date.now()
plugin.e = this.e
await (plugin[fnc as keyof typeof plugin] as Function)()
logger.bot('mark', this.e.self_id, `${this.e.logFnc} 上下文处理完成 ${Date.now() - start}ms`)
return true
switch (App.type) {
case 'ctx': {
listener.emit(`ctx:${key}`, this.e)
delete stateArr[key]
return true
}
case 'class': {
const { fnc, name } = App
this.e.logFnc = `[${fnc.name}][${name}]`
/** 计算插件处理时间 */
const start = Date.now()
fnc.e = this.e
await (fnc[name as keyof typeof fnc] as Function)()
logger.bot('mark', this.e.self_id, `${this.e.logFnc} 上下文处理完成 ${Date.now() - start}ms`)
return true
}
case 'fnc': {
const { fnc } = App
this.e.logFnc = `[${fnc.name}]`
/** 计算插件处理时间 */
const start = Date.now()
await fnc(this.e)
logger.bot('mark', this.e.self_id, `${this.e.logFnc} 上下文处理完成 ${Date.now() - start}ms`)
delete stateArr[key]
return true
}
}
}
return false
}
Expand Down
32 changes: 12 additions & 20 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import schedule from 'node-schedule'
import { Reply, replyCallback, replyForward } from './reply'
import { EventType, Event, Permission, SubEvent, KarinMessageEvent, KarinNoticeEvent, KarinRequestEvent } from './event'
import { Plugin } from 'karin/core'

/**
* - 插件根目录名称
Expand All @@ -14,6 +15,16 @@ export type dirName = `karin-plugin-${string}`
*/
export type fileName = `${string}.js` | `${string}.ts`

/**
* 上下文状态
*/
export interface stateArrType {
[key: string]:
| { type: 'fnc', fnc: Function }
| { type: 'class', fnc: Plugin, name: string }
| { type: 'ctx' }
}

/**
* - 插件规则
*/
Expand Down Expand Up @@ -211,33 +222,14 @@ export interface PluginType {
/**
* - 获取上下文状态
*/
getContext: () => {
/**
* - 插件实例
*/
plugin: PluginType
/**
* - 执行方法名称
*/
fnc: string
}
getContext: () => stateArrType[string]

/**
* - accept标准方法 给通知、请求事件使用
*/
accept?(e: EventType<this>): Promise<void>
}

/**
* 上下文状态
*/
export interface stateArrType {
[key: string]: {
plugin: PluginType
fnc: string
}
}

/**
* - Apps
*/
Expand Down

0 comments on commit 5b5ded9

Please sign in to comment.