Miao-Yunzai/src/core/plugins/functional.ts

103 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-06-08 20:52:49 +08:00
import { MessageCallBackType } from './types.js'
2024-06-10 18:50:22 +08:00
import { plugin } from './index.js'
2024-06-08 20:52:49 +08:00
2024-06-09 11:40:24 +08:00
/**
* super默认值
*/
2024-06-09 22:01:31 +08:00
export const PluginSuperDefine: {
2024-06-09 22:01:54 +08:00
name?: string
dsc?: string
event?: string
2024-06-09 22:01:31 +08:00
priority?: number
} = {
2024-06-08 20:52:49 +08:00
name: 'group-app',
dsc: 'group-dsc',
event: 'message',
priority: 9999
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-08 20:52:49 +08:00
export class Messages {
count = 0
rule: {
reg: RegExp
fnc: string
}[] = []
2024-06-09 11:40:24 +08:00
2024-06-09 22:01:31 +08:00
#init = PluginSuperDefine
constructor(init?: typeof PluginSuperDefine) {
this.#init = init
}
2024-06-09 11:40:24 +08:00
/**
*
* @param reg
* @param fnc
*/
2024-06-08 20:52:49 +08:00
response(reg: RegExp, fnc: MessageCallBackType) {
this.count++
const propName = `prop_${this.count}`
this[propName] = fnc
this.rule.push({
reg,
fnc: propName
})
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-08 20:52:49 +08:00
get ok() {
const App = this
class Children extends plugin {
constructor() {
super({
2024-06-09 22:01:31 +08:00
...App.#init,
2024-06-08 20:52:49 +08:00
rule: App.rule
})
for (const key of App.rule) {
if (App[key.fnc] instanceof Function) {
this[key.fnc] = App[key.fnc].bind(App)
}
}
}
}
return Children
}
}
/**
*
*/
export class Events {
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-08 20:52:49 +08:00
count = 0
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-08 20:52:49 +08:00
data: {
[key: string]: typeof plugin
} = {}
2024-06-09 11:40:24 +08:00
/**
*
* @param val
*/
2024-06-08 20:52:49 +08:00
use(val: typeof plugin) {
this.count++
this.data[this.count] = val
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-08 20:52:49 +08:00
get ok() {
return this.data
}
2024-06-08 21:01:41 +08:00
}