Skip to content

Commit

Permalink
fix: 优化部分类型定义
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Sep 15, 2024
1 parent f747e0b commit 4de3ae7
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 217 deletions.
56 changes: 31 additions & 25 deletions src/core/karin/karin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
KarinElement,
KarinMessage,
KarinRenderType,
PermissionType,
RenderResult,
KarinNoticeType,
KarinRequestType,
AllMessageSubType,
CommandInfo,
TaskInfo,
Expand All @@ -22,6 +19,9 @@ import {
AllNoticeSubType,
AllRequestSubType,
UseMapType,
AppType,
ButtonInfo,
Permission,
} from 'karin/types'

import { pluginLoader } from '../plugin/loader'
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface OptionsCommand extends Options {
* - 权限
* @default 'all'
*/
permission?: PermissionType
permission?: `${Permission}`
}

export interface OptionsElement extends OptionsCommand {
Expand Down Expand Up @@ -146,11 +146,11 @@ export class Karin extends Listeners {
fn,
fnname: 'fnc',
log,
name: options.name || 'command',
name: options.name || AppType.Command,
perm: options.permission || 'all',
rank: options.priority || 10000,
reg,
type: 'command',
type: AppType.Command,
}
}

Expand All @@ -171,9 +171,9 @@ export class Karin extends Listeners {
cron,
fn,
log,
name: options?.name || 'task',
name: options?.name || AppType.Task,
fnname: name,
type: 'task',
type: AppType.Task,
}
}

Expand All @@ -183,25 +183,16 @@ export class Karin extends Listeners {
* @param fn - 函数实现
* @param options - 选项
*/
handler (key: string, fn: (
/**
* - 自定义参数 由调用方传递
*/
args: { [key: string]: any },
/**
* - 停止循环函数 调用后则不再继续执行下一个处理器
*/
reject: (msg?: string) => void,
) => Promise<any>, options?: Omit<Options, 'log'>): HandlerInfo {
handler (key: string, fn: HandlerInfo['fn'], options?: Omit<Options, 'log'>): HandlerInfo {
if (!key) throw new Error('[handler]: 缺少参数[key]')
if (!fn) throw new Error('[handler]: 缺少参数[fnc]')

return {
fn,
key,
name: options?.name || 'handler',
name: options?.name || AppType.Handler,
rank: options?.priority || 10000,
type: 'handler',
type: AppType.Handler,
}
}

Expand Down Expand Up @@ -320,7 +311,7 @@ export class Karin extends Listeners {
* @param event - 监听事件
* @param fn - 实现函数
*/
accept (event: AllNoticeSubType | AllRequestSubType, fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>, options?: Options): AcceptInfo {
accept (event: AllNoticeSubType | AllRequestSubType, fn: AcceptInfo['fn'], options?: Options): AcceptInfo {
const log = options?.log === false
? (id: string, text: string) => logger.bot('debug', id, text)
: (id: string, text: string) => logger.bot('info', id, text)
Expand All @@ -329,9 +320,9 @@ export class Karin extends Listeners {
event,
fn,
log,
name: options?.name || 'accept',
name: options?.name || AppType.Accept,
rank: options?.priority || 10000,
type: 'accept',
type: AppType.Accept,
}
}

Expand All @@ -349,9 +340,24 @@ export class Karin extends Listeners {
return {
fn,
key: type,
name: options?.name || 'use',
name: options?.name || AppType.Use,
rank: options?.priority || 10000,
type: AppType.Use,
}
}

/**
* 按钮
* @param reg - 正则表达式
* @param fn - 函数
*/
button (reg: RegExp | string, fn: ButtonInfo['fn'], options?: Omit<Options, 'log'>): ButtonInfo {
return {
fn,
reg: reg instanceof RegExp ? reg : new RegExp(reg),
name: options?.name || AppType.Button,
rank: options?.priority || 10000,
type: 'use',
type: AppType.Button,
}
}

Expand Down
35 changes: 18 additions & 17 deletions src/core/plugin/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import {
CommandInfo,
HandlerInfo,
Permission,
UseMapType,
UseKeyType,
PluginInfoType,
NewMessagePlugin,
PluginTaskInfoType,
PluginButtonInfoType,
PluginAcceptInfoType,
PluginHandlerInfoType,
PluginCommandInfoType,
UseValueType,
} from 'karin/types'

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

Expand All @@ -97,11 +98,11 @@ class PluginLoader {
this.plugin = new Map()
this.task = []
this.use = {
recvMsg: [],
replyMsg: [],
sendMsg: [],
forwardMsg: [],
notFoundMsg: [],
[UseKeyType['ReceiveMsg']]: [],
[UseKeyType['ReplyMsg']]: [],
[UseKeyType['SendMsg']]: [],
[UseKeyType['ForwardMsg']]: [],
[UseKeyType['NotFoundMsg']]: [],
}

this.ext = process.env.karin_app_lang === 'ts' ? ['.js', '.ts'] : ['.js']
Expand Down Expand Up @@ -366,11 +367,11 @@ class PluginLoader {
this.button = lodash.orderBy(this.button, ['rank'], ['asc'])
this.command = lodash.orderBy(this.command, ['rank'], ['asc'])
this.task = lodash.orderBy(this.task, ['rank'], ['asc'])
this.use.recvMsg = lodash.orderBy(this.use.recvMsg, ['rank'], ['asc'])
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.notFoundMsg = lodash.orderBy(this.use.notFoundMsg, ['rank'], ['asc'])
this.use[UseKeyType['ReceiveMsg']] = lodash.orderBy(this.use[UseKeyType['ReceiveMsg']], ['rank'], ['asc'])
this.use[UseKeyType['ReplyMsg']] = lodash.orderBy(this.use[UseKeyType['ReplyMsg']], ['rank'], ['asc'])
this.use[UseKeyType['SendMsg']] = lodash.orderBy(this.use[UseKeyType['SendMsg']], ['rank'], ['asc'])
this.use[UseKeyType['ForwardMsg']] = lodash.orderBy(this.use[UseKeyType['ForwardMsg']], ['rank'], ['asc'])
this.use[UseKeyType['NotFoundMsg']] = lodash.orderBy(this.use[UseKeyType['NotFoundMsg']], ['rank'], ['asc'])

const handler = Object.keys(this.handler)
handler.forEach(key => {
Expand Down Expand Up @@ -652,11 +653,11 @@ class PluginLoader {
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)
this.use.recvMsg = this.use.recvMsg.filter(val => val.key !== key)
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.notFoundMsg = this.use.notFoundMsg.filter(val => val.key !== key)
this.use[UseKeyType['ReceiveMsg']] = this.use[UseKeyType['ReceiveMsg']].filter(val => val.key !== key)
this.use[UseKeyType['ReplyMsg']] = this.use[UseKeyType['ReplyMsg']].filter(val => val.key !== key)
this.use[UseKeyType['SendMsg']] = this.use[UseKeyType['SendMsg']].filter(val => val.key !== key)
this.use[UseKeyType['ForwardMsg']] = this.use[UseKeyType['ForwardMsg']].filter(val => val.key !== key)
this.use[UseKeyType['NotFoundMsg']] = this.use[UseKeyType['NotFoundMsg']].filter(val => val.key !== key)

/** 定时任务需要先停止 */
this.task = this.task.filter(val => {
Expand Down
10 changes: 2 additions & 8 deletions src/types/event/sender.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-unused-vars */
/**
* - 群身份枚举 值不存在时为unknown
*/
export const enum Role {
Owner = 'owner',
Admin = 'admin',
Member = 'member',
Unknown = 'unknown'
Unknown = 'unknown',
}

/**
Expand All @@ -17,14 +16,9 @@ export const enum Permission {
Master = 'master',
Admin = 'admin',
GroupOwner = 'group.owner',
GroupAdmin = 'group.admin'
GroupAdmin = 'group.admin',
}

/**
* - 权限类型 纯文本
*/
export type PermissionType = 'all' | 'master' | 'admin' | 'group.owner' | 'group.admin'

/**
* - 事件发送者信息
*/
Expand Down
108 changes: 99 additions & 9 deletions src/types/plugin/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, KarinMessageType, KarinNoticeType, KarinRequestType, NewMessagePlugin, Permission, UseMapType } from 'karin/types'
import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, ButtonElement, Contact, KarinAdapter, KarinElement, KarinMessage, KarinMessageType, KarinNoticeType, KarinRequestType, KeyBoardElement, NewMessagePlugin, NodeElement, Permission, UseMapType } from 'karin/types'

export const enum AppType {
Command = 'command',
Expand All @@ -9,7 +9,7 @@ export const enum AppType {
Use = 'use',
}

interface AppInfo {
export interface AppBase {
/** 实现方法 */
fn: Function
/** 插件名称 */
Expand All @@ -23,7 +23,7 @@ interface AppInfo {
}

/** command规则集类型 */
export interface CommandInfo extends AppInfo {
export interface CommandInfo extends AppBase {
fn: (e: KarinMessageType) => Promise<boolean>
/** 插件执行方法名称 */
fnname: string
Expand All @@ -39,7 +39,7 @@ export interface CommandInfo extends AppInfo {
}

/** task规则集类型 */
export interface TaskInfo extends Omit<AppInfo, 'rank'> {
export interface TaskInfo extends Omit<AppBase, 'rank'> {
type: `${AppType.Task}`
/** 任务名称 */
fnname: string
Expand All @@ -48,30 +48,120 @@ export interface TaskInfo extends Omit<AppInfo, 'rank'> {
}

/** handler规则集类型 */
export interface HandlerInfo extends Omit<AppInfo, 'log'> {
export interface HandlerInfo extends Omit<AppBase, 'log'> {
type: `${AppType.Handler}`
/** 入口秘钥 */
key: string
/** 实现方法 */
fn: (
/**
* - 自定义参数 由调用方传递
*/
args: { [key: string]: any },
/**
* - 停止循环函数 调用后则不再继续执行下一个处理器
*/
next: (msg?: string) => void,
) => Promise<any>
}

/** button规则集类型 */
export interface ButtonInfo extends AppInfo {
export interface ButtonInfo extends Omit<AppBase, 'log'> {
type: `${AppType.Button}`
/** 正则 */
reg: RegExp
fn: (
/** 是否继续匹配下一个按钮 默认否 调用后则继续 */
next: () => void,
/** 消息事件 可能不存在~ */
e?: KarinMessage,
/** 自定义参数 */
...args: any[]
) => Promise<ButtonElement | KeyBoardElement | Array<ButtonElement | KeyBoardElement>>
}

/** accept规则集类型 */
export interface AcceptInfo extends AppInfo {
export interface AcceptInfo extends AppBase {
fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>
type: `${AppType.Accept}`
/** 监听事件 */
event: `${AllNoticeSubType}` | `${AllRequestSubType}`
}

/**
* 初始化消息前 中间件实现方法
*/
export type UseRecvMsgFn = (
/** 消息事件方法 */
e: KarinMessageType,
/** 是否继续执行下一个中间件 */
next: () => void,
/** 是否退出此条消息 不再执行匹配插件 */
exit: () => void
) => Promise<boolean>

/**
* 回复消息前 中间件实现方法
*/
export type UseSendMsgFn = (
/** 消息事件方法 */
e: KarinMessageType,
/** 回复的消息体 */
element: KarinElement[],
/** 是否继续执行下一个中间件 */
next: () => void,
/** 是否不发送此条消息 */
exit: () => void
) => Promise<boolean>

/**
* 发送主动消息前 中间件实现方法
*/
export type UseSendNoticeFn = (
/** 发送的bot */
uid: string,
/** 发送目标 */
contact: Contact,
/** 发送的消息体 */
element: KarinElement[],
/** 是否继续执行下一个中间件 */
next: () => void,
/** 是否不发送此条消息 */
exit: () => void
) => Promise<boolean>

/**
* 发送合并转发前 中间件实现方法
*/
export type UseForwardMsgFn = (
bot: KarinAdapter,
/** 发送的目标信息 */
contact: Contact,
/** 发送的消息体 */
elements: Array<NodeElement>,
/** 是否继续执行下一个中间件 */
next: () => void,
/** 是否不发送此条消息 */
exit: () => void
) => Promise<boolean>

/**
* 消息事件没有找到任何匹配的插件触发 中间件实现方法
*/
export type UseNotFoundMsgFn = (
/** 消息事件方法 */
e: KarinMessageType,
/** 是否继续执行下一个中间件 */
next: () => void,
/** 是否退出此条消息 不再执行匹配插件 */
exit: () => void
) => Promise<boolean>

/** use规则集类型 */
export interface UseInfo extends Omit<AppInfo, 'log'> {
export interface UseInfo<T extends keyof UseMapType = keyof UseMapType> extends Omit<AppBase, 'log'> {
type: `${AppType.Use}`
/** 中间件类型key */
key: `${keyof UseMapType}`
key: `${T}`
/** 中间件实现方法 */
fn: UseMapType[T][number]['fn']
}
Loading

0 comments on commit 4de3ae7

Please sign in to comment.