Skip to content

Commit

Permalink
fix: 修正合并转发中间件获取上下文错误
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Sep 14, 2024
1 parent 907e37d commit 324b11c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 53 deletions.
47 changes: 7 additions & 40 deletions src/core/karin/karin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @stylistic/indent */
import { stateArr } from '../plugin/base'
import onebot11 from 'karin/adapter/onebot/11'
import { render } from 'karin/render/app'
Expand All @@ -14,7 +13,6 @@ import {
RenderResult,
KarinNoticeType,
KarinRequestType,
KarinMessageType,
AllMessageSubType,
CommandInfo,
TaskInfo,
Expand All @@ -23,44 +21,17 @@ import {
UseInfo,
AllNoticeSubType,
AllRequestSubType,
UseMapType,
} from 'karin/types'

import { pluginLoader } from '../plugin/loader'
import { common } from 'karin/utils/common/common'
import { logger } from 'karin/utils/core/logger'
import { Listeners } from '../listener/listener'

type FncFunction = (e: KarinMessage) => Promise<boolean>
type FncElement = string | KarinElement | Array<KarinElement>
type UseReceive = (e: KarinMessageType, next: Function, exit: Function) => Promise<void>
type UseReply = (e: KarinMessageType, element: KarinElement[], next: Function, exit: Function) => Promise<void>
type UseRecord = (uid: string, contact: Contact, elements: KarinElement[], next: Function, exit: Function) => Promise<void>
type ForwardRecord = (contact: Contact, elements: KarinElement[], next: Function, exit: Function) => Promise<void>
type NotFoundRecord = (e: KarinMessageType, next: Function, exit: Function) => Promise<void>

type MiddlewareFn<T extends MiddlewareType> =
T extends `${MiddlewareType.ReceiveMsg}` ? UseReceive :
T extends `${MiddlewareType.ReplyMsg}` ? UseReply :
T extends `${MiddlewareType.SendMsg}` ? UseRecord :
T extends `${MiddlewareType.ForwardMsg}` ? ForwardRecord :
T extends `${MiddlewareType.NotFoundMsg}` ? NotFoundRecord :
never

/**
* 中间件类型
*/
export const enum MiddlewareType {
/** 收到消息后 */
ReceiveMsg = 'recvMsg',
/** 回复消息前 */
ReplyMsg = 'replyMsg',
/** 发送主动消息前 */
SendMsg = 'sendMsg',
/** 发送合并转发前 */
ForwardMsg = 'forwardMsg',
/** 消息事件没有找到任何匹配的插件触发 */
NotFoundMsg = 'notFound',
}
export type FncFunction = (e: KarinMessage) => Promise<boolean>
export type FncElement = string | KarinElement | Array<KarinElement>
export type UseFnType<K extends keyof UseMapType> = UseMapType[K]

export interface Options {
/**
Expand Down Expand Up @@ -364,19 +335,15 @@ export class Karin extends Listeners {
}
}

use (type: `${MiddlewareType.ReceiveMsg}`, fn: UseReceive, options?: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.ReplyMsg}`, fn: UseReply, options?: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.SendMsg}`, fn: UseRecord, options?: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.ForwardMsg}`, fn: ForwardRecord, options?: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.NotFoundMsg}`, fn: NotFoundRecord, options?: Omit<Options, 'log'>): UseInfo
/**
* 中间件
* @param type 中间件类型
* @param fn 中间件函数
* @param options 选项配置
*/
use<T extends MiddlewareType> (
use<T extends keyof UseMapType> (
type: `${T}`,
fn: MiddlewareFn<T>,
fn: UseMapType[T][number]['fn'],
options?: Omit<Options, 'log'>
): UseInfo {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/core/listener/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class Listeners extends EventEmitter {
const nextFn = () => { next = true }
const exitFn = () => { exit = true }

await info.fn(contact, elements, nextFn, exitFn)
await info.fn(data.bot, contact, elements, nextFn, exitFn)

if (exit) {
const plugin = pluginLoader.plugin.get(info.key)!
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 @@ -22,7 +22,7 @@ import {
PluginAcceptInfoType,
PluginHandlerInfoType,
PluginCommandInfoType,
PluginMiddlewareInfoType,
UseValueType,
} from 'karin/types'

type AppType = CommandInfo | TaskInfo | HandlerInfo | ButtonInfo | AcceptInfo | UseInfo
Expand Down Expand Up @@ -85,7 +85,7 @@ class PluginLoader {
/** task定时任务信息 */
task: PluginTaskInfoType[]
/** 中间件 */
use: PluginMiddlewareInfoType
use: UseValueType
/** 加载的文件数组 .js .ts */
ext: string[]

Expand All @@ -101,7 +101,7 @@ class PluginLoader {
replyMsg: [],
sendMsg: [],
forwardMsg: [],
notFound: [],
notFoundMsg: [],
}

this.ext = process.env.karin_app_lang === 'ts' ? ['.js', '.ts'] : ['.js']
Expand Down Expand Up @@ -370,7 +370,7 @@ class PluginLoader {
this.use.replyMsg = lodash.orderBy(this.use.replyMsg, ['rank'], ['asc'])
this.use.sendMsg = lodash.orderBy(this.use.sendMsg, ['rank'], ['asc'])
this.use.forwardMsg = lodash.orderBy(this.use.forwardMsg, ['rank'], ['asc'])
this.use.notFound = lodash.orderBy(this.use.notFound, ['rank'], ['asc'])
this.use.notFoundMsg = lodash.orderBy(this.use.notFoundMsg, ['rank'], ['asc'])

const handler = Object.keys(this.handler)
handler.forEach(key => {
Expand Down Expand Up @@ -656,7 +656,7 @@ class PluginLoader {
this.use.replyMsg = this.use.replyMsg.filter(val => val.key !== key)
this.use.sendMsg = this.use.sendMsg.filter(val => val.key !== key)
this.use.forwardMsg = this.use.forwardMsg.filter(val => val.key !== key)
this.use.notFound = this.use.notFound.filter(val => val.key !== key)
this.use.notFoundMsg = this.use.notFoundMsg.filter(val => val.key !== key)

/** 定时任务需要先停止 */
this.task = this.task.filter(val => {
Expand Down
2 changes: 1 addition & 1 deletion src/event/handler/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class MessageHandler extends EventBaseHandler {
* 结束中间件
*/
async endUse () {
for (const info of pluginLoader.use.notFound) {
for (const info of pluginLoader.use.notFoundMsg) {
try {
let next = false
let exit = false
Expand Down
7 changes: 3 additions & 4 deletions src/types/plugin/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { MiddlewareType } from '../../core/karin/karin'
import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, KarinMessageType, KarinNoticeType, KarinRequestType, NewMessagePlugin, Permission } from 'karin/types'
import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, KarinMessageType, KarinNoticeType, KarinRequestType, NewMessagePlugin, Permission, UseMapType } from 'karin/types'

export const enum AppType {
Command = 'command',
Task = 'task',
Handler = 'handler',
Button = 'button',
Accept = 'accept',
Use = 'use'
Use = 'use',
}

interface AppInfo {
Expand Down Expand Up @@ -74,5 +73,5 @@ export interface AcceptInfo extends AppInfo {
export interface UseInfo extends Omit<AppInfo, 'log'> {
type: `${AppType.Use}`
/** 中间件类型key */
key: `${MiddlewareType}`
key: `${keyof UseMapType}`
}
33 changes: 31 additions & 2 deletions src/types/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Plugin } from 'karin/core'
import { Reply, replyCallback, replyForward } from '../event/reply'
import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinMessageType, PermissionType, AllMessageSubType, Contact, AllNoticeSubType, AllRequestSubType } from '../event'
import { KarinElement, NodeElement } from '../element/element'
import { KarinAdapter } from '../adapter/base'

/**
* - 插件根目录名称
Expand Down Expand Up @@ -155,7 +156,7 @@ export interface PluginHandlerInfoType {
/**
* 中间件规则集信息
*/
export interface PluginMiddlewareInfoType {
export interface UseValueType {
/** 初始化消息前 */
recvMsg: Array<{
/** 插件基本信息的映射key */
Expand Down Expand Up @@ -224,6 +225,7 @@ export interface PluginMiddlewareInfoType {
name: string,
/** 插件执行方法 */
fn: (
bot: KarinAdapter,
/** 发送的目标信息 */
contact: Contact,
/** 发送的消息体 */
Expand All @@ -237,7 +239,7 @@ export interface PluginMiddlewareInfoType {
rank: number
}>
/** 消息事件没有找到任何匹配的插件触发 */
notFound: Array<{
notFoundMsg: Array<{
/** 插件基本信息的映射key */
key: number,
/** 插件包名称 */
Expand All @@ -256,6 +258,33 @@ export interface PluginMiddlewareInfoType {
}>
}

/**
* 中间件类型
*/
export const enum UseKeyType {
/** 收到消息后 */
ReceiveMsg = 'recvMsg',
/** 回复消息前 */
ReplyMsg = 'replyMsg',
/** 发送主动消息前 */
SendMsg = 'sendMsg',
/** 发送合并转发前 */
ForwardMsg = 'forwardMsg',
/** 消息事件没有找到任何匹配的插件触发 */
NotFoundMsg = 'notFoundMsg',
}

/**
* 中间件映射
*/
export interface UseMapType {
[UseKeyType.ReceiveMsg]: UseValueType['recvMsg']
[UseKeyType.ReplyMsg]: UseValueType['replyMsg']
[UseKeyType.SendMsg]: UseValueType['sendMsg']
[UseKeyType.ForwardMsg]: UseValueType['forwardMsg']
[UseKeyType.NotFoundMsg]: UseValueType['notFoundMsg']
}

/**
* 上下文状态
*/
Expand Down

0 comments on commit 324b11c

Please sign in to comment.