Skip to content

Commit

Permalink
fix: close #123
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Aug 13, 2024
1 parent c7474e3 commit 707c3c0
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 52 deletions.
18 changes: 14 additions & 4 deletions src/core/listener/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,25 @@ export class Listeners extends EventEmitter {
recallMsg?: number
retry_count?: number
} = { recallMsg: 0, retry_count: 1 }): Promise<{ message_id: string }> {
/** 结果 */
let result: any = {}

/** 先调用中间件 */
for (const info of pluginLoader.use.sendMsg) {
try {
let next = false
let exit = false
const nextFn = () => { next = true }
await info.fn(uid, contact, elements, nextFn)
const exitFn = () => { exit = true }

await info.fn(uid, contact, elements, nextFn, exitFn)

if (exit) {
const plugin = pluginLoader.plugin.get(info.key)!
logger.debug(`[消息中间件][${plugin.plugin}][${plugin.file}] 主动操作退出`)
return result
}

if (!next) break
} catch (e) {
logger.error('[消息中间件] 调用失败,已跳过')
Expand All @@ -211,9 +224,6 @@ export class Listeners extends EventEmitter {
/** 标准化 */
const NewElements = common.makeMessage(elements)

/** 结果 */
let result: any = {}

const reply_log = common.makeMessageLog(NewElements)
const self_id = bot.account.uid || bot.account.uin
if (contact.scene === 'group') {
Expand Down
23 changes: 11 additions & 12 deletions src/core/plugin/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PluginLoader {
handler: { [key: string]: PluginHandlerInfoType[] }

/** plugin基本信息 */
plugin: PluginInfoType
plugin: Map<number, PluginInfoType>
/** task定时任务信息 */
task: PluginTaskInfoType[]
/** 中间件 */
Expand All @@ -94,7 +94,7 @@ class PluginLoader {
this.button = []
this.command = []
this.handler = {}
this.plugin = {}
this.plugin = new Map()
this.task = []
this.use = {
recvMsg: [],
Expand Down Expand Up @@ -394,7 +394,7 @@ class PluginLoader {
try {
const index = ++this.index
/** 缓存基本信息 */
this.plugin[index] = { type, plugin, path: _path, file }
this.plugin.set(index, { type, plugin, path: _path, file })

const list: any[] = []
let rootPath = 'file://' + path.join(process.cwd(), type === 'npm' ? 'node_modules' : 'plugins', plugin, _path, file)
Expand Down Expand Up @@ -524,7 +524,7 @@ class PluginLoader {
name: info.name,
event: info.event,
fn: info.fn,
key: index + '',
key: index,
log: info.log,
rank: info.rank,
})
Expand All @@ -535,7 +535,7 @@ class PluginLoader {
name: info.name,
reg: info.reg,
fn: info.fn as any,
key: index + '',
key: index,
rank: info.rank,
})
return true
Expand All @@ -545,7 +545,7 @@ class PluginLoader {
this.handler[info.key].push({
name: info.name,
fn: info.fn as any,
key: index + '',
key: index,
rank: info.rank,
})
return true
Expand All @@ -557,7 +557,7 @@ class PluginLoader {
event: info.event,
fn: info.fn,
fnname: info.fnname,
key: index + '',
key: index,
log: info.log,
perm: info.perm,
rank: info.rank,
Expand All @@ -571,7 +571,7 @@ class PluginLoader {
name: info.name,
cron: info.cron,
fn: info.fn,
key: index + '',
key: index,
taskname: info.fnname,
data: App,
log: info.log,
Expand All @@ -597,7 +597,7 @@ class PluginLoader {
this.use[info.key].push({
name: info.name,
fn: info.fn as any,
key: index + '',
key: index,
rank: info.rank,
})
return true
Expand Down Expand Up @@ -632,11 +632,10 @@ class PluginLoader {
* 卸载插件
*/
uninstallApp (plugin: string, _path: string, file: string) {
Object.keys(this.plugin).forEach(key => {
const info = this.plugin[key]
this.plugin.forEach((info, key) => {
if (info.plugin === plugin && info.path === _path && info.file === file) {
/** 删除缓存 */
delete this.plugin[key]
this.plugin.delete(key)
this.accept = this.accept.filter(val => val.key !== key)
this.button = this.button.filter(val => val.key !== key)
this.command = this.command.filter(val => val.key !== key)
Expand Down
26 changes: 19 additions & 7 deletions src/event/handler/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,31 @@ export class EventBaseHandler {
* @param options 回复选项
*/
this.e.reply = async (elements = '', options = { reply: false, recallMsg: 0, at: false, retry_count: 1 }) => {
const request: ReplyReturn = {
message_id: '',
message_time: 0,
raw_data: undefined,
}

const message = common.makeMessage(elements)

/** 先调用中间件 */
for (const info of pluginLoader.use.replyMsg) {
try {
let next = false
let exit = false
const nextFn = () => { next = true }
await info.fn(this.e as KarinMessageType, message, nextFn)
const exitFn = () => { exit = true }

await info.fn(this.e as KarinMessageType, message, nextFn, exitFn)

if (exit) {
const plugin = pluginLoader.plugin.get(info.key)!
logger.debug(`[消息中间件][${plugin.plugin}][${plugin.file}] 主动操作退出`)
return request
}

if (!next) break
if (!next) break
} catch (e) {
logger.error('[消息中间件] 调用失败,已跳过')
Expand All @@ -169,12 +187,6 @@ export class EventBaseHandler {
this.e.self_id !== 'input' && logger.bot('info', this.e.self_id, `${logger.green(`Send private ${this.e.user_id}: `)}${ReplyLog}`)
}

const request: ReplyReturn = {
message_id: '',
message_time: 0,
raw_data: undefined,
}

try {
listener.emit('karin:count:send', 1)
/** 取结果 */
Expand Down
12 changes: 11 additions & 1 deletion src/event/handler/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,18 @@ export class MessageHandler extends EventBaseHandler {
for (const info of pluginLoader.use.recvMsg) {
try {
let next = false
let exit = false
const nextFn = () => { next = true }
await info.fn(this.e, nextFn)
const exitFn = () => { exit = true }

await info.fn(this.e, nextFn, exitFn)

if (exit) {
const plugin = pluginLoader.plugin.get(info.key)!
logger.debug(`[消息中间件][${plugin.plugin}][${plugin.file}] 主动操作退出`)
return
}

if (!next) break
} catch (e) {
logger.error('[消息中间件] 调用失败,已跳过')
Expand Down
6 changes: 3 additions & 3 deletions src/event/handler/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export const review = new (class Handler {
/** 同时启用 */
if (this.App.GroupConfig.enable && this.App.GroupConfig.disable) {
this.PluginEnable = (app, config) => {
const plugin = pluginLoader.plugin[app.key]
const plugin = pluginLoader.plugin.get(app.key)!
/** 白名单不为空 */
if (Array.isArray(config.enable) && config.enable.length) {
/** 插件包是否处于功能白名单 */
Expand Down Expand Up @@ -256,7 +256,7 @@ export const review = new (class Handler {
/** 白名单启用 */
if (this.App.GroupConfig.enable) {
this.PluginEnable = (app, config) => {
const plugin = pluginLoader.plugin[app.key]
const plugin = pluginLoader.plugin.get(app.key)!

if (Array.isArray(config.enable) && config.enable.length) {
for (const key of config.enable) {
Expand All @@ -275,7 +275,7 @@ export const review = new (class Handler {
/** 黑名单启用 */
if (this.App.GroupConfig.disable) {
this.PluginEnable = (app, config) => {
const plugin = pluginLoader.plugin[app.key]
const plugin = pluginLoader.plugin.get(app.key)!

if (Array.isArray(config.disable) && config.disable.length) {
for (const key of config.disable) {
Expand Down
52 changes: 28 additions & 24 deletions src/types/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,22 @@ export const enum MethodType {
* plugin基本信息
*/
export interface PluginInfoType {
[key: string]: {
/** 插件包类型 */
type: `${AppsType}`,
/** 插件包名称 例: `karin-plugin-example` `@karinjs/adapter-qqbot` */
plugin: string,
/** 插件路径 在type为js下,path为空 */
path: string,
/** 插件文件名称 index.js index.ts */
file: string,
}
/** 插件包类型 */
type: `${AppsType}`,
/** 插件包名称 例: `karin-plugin-example` `@karinjs/adapter-qqbot` */
plugin: string,
/** 插件路径 在type为js下,path为空 */
path: string,
/** 插件文件名称 index.js index.ts */
file: string,
}

/**
* Command规则集信息
*/
export interface PluginCommandInfoType {
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 插件正则 */
Expand Down Expand Up @@ -89,7 +87,7 @@ export interface PluginCommandInfoType {
*/
export interface PluginAcceptInfoType {
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 插件执行方法 */
Expand All @@ -107,7 +105,7 @@ export interface PluginAcceptInfoType {
*/
export interface PluginTaskInfoType {
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 任务名称 */
Expand All @@ -129,7 +127,7 @@ export interface PluginTaskInfoType {
*/
export interface PluginButtonInfoType {
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 插件正则 */
Expand All @@ -145,7 +143,7 @@ export interface PluginButtonInfoType {
*/
export interface PluginHandlerInfoType {
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** handler的处理方法 */
Expand All @@ -161,23 +159,25 @@ export interface PluginMiddlewareInfoType {
/** 初始化消息前 */
recvMsg: Array<{
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 插件执行方法 */
fn: (
/** 消息事件方法 */
e: KarinMessageType,
/** 是否继续执行插件 */
next: Function
/** 是否继续执行下一个中间件 */
next: Function,
/** 是否退出此条消息 不再执行匹配插件 */
exit: Function
) => Promise<boolean>,
/** 优先级 */
rank: number
}>,
/** 回复消息前 */
replyMsg: Array<{
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 插件执行方法 */
Expand All @@ -186,16 +186,18 @@ export interface PluginMiddlewareInfoType {
e: KarinMessageType,
/** 回复的消息体 */
element: KarinElement[],
/** 是否继续执行插件 */
next: Function
/** 是否继续执行下一个中间件 */
next: Function,
/** 是否不发送此条消息 */
exit: Function
) => Promise<boolean>,
/** 优先级 */
rank: number
}>,
/** 发送主动消息前 */
sendMsg: Array<{
/** 插件基本信息的映射key */
key: string,
key: number,
/** 插件包名称 */
name: string,
/** 插件执行方法 */
Expand All @@ -206,8 +208,10 @@ export interface PluginMiddlewareInfoType {
contact: Contact,
/** 发送的消息体 */
element: KarinElement[],
/** 是否继续执行插件 */
next: Function
/** 是否继续执行下一个中间件 */
next: Function,
/** 是否不发送此条消息 */
exit: Function
) => Promise<boolean>,
/** 优先级 */
rank: number
Expand Down
Loading

0 comments on commit 707c3c0

Please sign in to comment.