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

257 lines
5.3 KiB
TypeScript
Raw Normal View History

2024-06-11 20:13:21 +08:00
2024-06-11 17:41:11 +08:00
import { Common } from '../../miao.js'
2024-06-08 20:52:49 +08:00
import { EventType } from './types.js'
2024-06-12 10:40:10 +08:00
import { type EventMap } from 'icqq'
2024-06-11 19:45:04 +08:00
const State = {}
2024-06-08 20:52:49 +08:00
const SymbolTimeout = Symbol('Timeout')
const SymbolResolve = Symbol('Resolve')
2024-06-12 10:40:10 +08:00
/**
*
*/
type PluginSuperType = {
2024-06-11 19:45:04 +08:00
/**
2024-06-12 10:40:10 +08:00
* @param name
2024-06-11 19:45:04 +08:00
* @deprecated
*/
2024-06-12 10:40:10 +08:00
name?: string
2024-06-11 19:45:04 +08:00
/**
2024-06-12 10:40:10 +08:00
* @param dsc
2024-06-11 19:45:04 +08:00
* @deprecated
*/
2024-06-12 10:40:10 +08:00
dsc?: string
2024-06-11 19:45:04 +08:00
/**
2024-06-12 10:40:10 +08:00
* namespacehandler时建议设置
2024-06-11 19:45:04 +08:00
* @deprecated
*/
2024-06-12 10:40:10 +08:00
namespace?: any
2024-06-11 20:13:21 +08:00
/**
2024-06-12 10:40:10 +08:00
* @param handler handler配置
* @param handler.key handler支持的事件key
* @param handler.fn handler的处理func
* @deprecated
2024-06-11 20:13:21 +08:00
*/
2024-06-12 10:40:10 +08:00
handler?: any
2024-06-11 20:13:21 +08:00
/**
2024-06-12 10:40:10 +08:00
* task
* task.name
* task.cron cron表达式
* task.fnc
* task.log false时不显示执行日志
* @deprecated
2024-06-11 20:13:21 +08:00
*/
2024-06-12 10:40:10 +08:00
task?: any
2024-06-11 20:13:21 +08:00
/**
2024-06-12 10:40:10 +08:00
*
2024-06-11 20:13:21 +08:00
*/
2024-06-12 10:40:10 +08:00
priority?: number
2024-06-11 20:13:21 +08:00
/**
2024-06-12 10:40:10 +08:00
*
2024-06-11 20:13:21 +08:00
*/
2024-06-12 10:40:10 +08:00
event?: keyof EventMap
2024-06-11 20:13:21 +08:00
/**
2024-06-12 10:40:10 +08:00
* rule
* rule.reg
* rule.fnc
* rule.event message
* rule.log false时不显示执行日志
* rule.permission master,owner,admin,all
2024-06-11 20:13:21 +08:00
*/
2024-06-12 10:40:10 +08:00
rule?: {
2024-06-12 10:46:26 +08:00
reg?: RegExp | string,
2024-06-12 10:40:10 +08:00
fnc?: string,
event?: keyof EventMap,
log?: boolean
permission?: 'master' | 'owner' | 'admin' | 'all'
}[]
}
2024-06-11 20:13:21 +08:00
2024-06-12 10:40:10 +08:00
export class Plugin {
name: PluginSuperType['name'] = 'your-plugin'
dsc: PluginSuperType['dsc'] = '无'
task: PluginSuperType['task'] = null
rule: PluginSuperType['rule'] = []
event: PluginSuperType['event'] = 'message'
priority: PluginSuperType['priority'] = 9999
namespace: PluginSuperType['namespace'] = null
handler: PluginSuperType['handler'] = null
2024-06-11 21:33:21 +08:00
e: EventType
2024-06-08 20:52:49 +08:00
/**
* @param event message
* @param priority
2024-06-12 10:40:10 +08:00
* @param rule
2024-06-08 20:52:49 +08:00
*/
2024-06-12 21:25:21 +08:00
constructor(init?: PluginSuperType) {
const {
event,
priority = 5000,
rule,
name,
dsc,
handler,
namespace,
task,
} = init
2024-06-08 20:52:49 +08:00
name && (this.name = name)
dsc && (this.dsc = dsc)
event && (this.event = event)
priority && (this.priority = priority)
2024-06-09 23:19:06 +08:00
2024-06-12 21:25:21 +08:00
/**
*
*/
2024-06-09 23:19:06 +08:00
task &&
(this.task = {
/** 任务名 */
name: task?.name ?? '',
/** 任务方法名 */
fnc: task?.fnc ?? '',
/** 任务cron表达式 */
cron: task?.cron ?? ''
})
2024-06-09 23:18:36 +08:00
2024-06-12 21:25:21 +08:00
/**
*
*/
2024-06-09 23:18:36 +08:00
rule && (this.rule = rule)
2024-06-08 20:52:49 +08:00
if (handler) {
this.handler = handler
this.namespace = namespace || ''
}
2024-06-11 20:13:21 +08:00
2024-06-08 20:52:49 +08:00
}
/**
* @param msg
* @param quote
* @param data.recallMsg 0-1200
* @param data.at at用户
*/
2024-06-11 21:33:21 +08:00
reply(msg: any[] | string = '', quote = false, data = {}) {
2024-06-08 20:52:49 +08:00
if (!this.e?.reply || !msg) return false
return this.e.reply(msg, quote, data)
}
/**
2024-06-11 19:45:04 +08:00
* @deprecated
2024-06-08 20:52:49 +08:00
*/
group_id: number
2024-06-11 19:45:04 +08:00
/**
* @deprecated
*/
2024-06-08 20:52:49 +08:00
groupId: number
2024-06-11 19:45:04 +08:00
/**
* @deprecated
*/
2024-06-08 20:52:49 +08:00
user_id: number
2024-06-11 19:45:04 +08:00
/**
* @deprecated
*/
2024-06-08 20:52:49 +08:00
userId: number
/**
*
* @param isGroup
* @returns
*/
conKey(isGroup = false) {
if (isGroup) {
return `${this.name}.${this.group_id || this.groupId || this.e.group_id}`
} else {
return `${this.name}.${this.user_id || this.userId || this.e.user_id}`
}
}
/**
* @param type
* @param isGroup
* @param time
* @param timeout
*/
setContext(
type: string,
isGroup = false,
time = 120,
timeout = '操作超时已取消'
) {
const key = this.conKey(isGroup)
2024-06-11 19:45:04 +08:00
if (!State[key]) State[key] = {}
State[key][type] = this.e
2024-06-08 20:52:49 +08:00
if (time)
2024-06-11 19:45:04 +08:00
State[key][type][SymbolTimeout] = setTimeout(() => {
if (State[key][type]) {
const resolve = State[key][type][SymbolResolve]
delete State[key][type]
2024-06-08 20:52:49 +08:00
resolve ? resolve(false) : this.reply(timeout, true)
}
}, time * 1000)
2024-06-11 19:45:04 +08:00
return State[key][type]
2024-06-08 20:52:49 +08:00
}
/**
*
* @param type
* @param isGroup
* @returns
*/
getContext(type: string, isGroup?: boolean) {
2024-06-11 19:45:04 +08:00
if (type) return State[this.conKey(isGroup)]?.[type]
return State[this.conKey(isGroup)]
2024-06-08 20:52:49 +08:00
}
/**
*
* @param type
* @param isGroup
*/
2024-06-08 21:01:41 +08:00
finish(type: string, isGroup?: boolean) {
2024-06-08 20:52:49 +08:00
const key = this.conKey(isGroup)
2024-06-11 19:45:04 +08:00
if (State[key]?.[type]) {
clearTimeout(State[key][type][SymbolTimeout])
delete State[key][type]
2024-06-08 20:52:49 +08:00
}
}
/**
*
* @param args
* @returns
*/
awaitContext(...args) {
return new Promise(
resolve =>
(this.setContext('resolveContext', ...args)[SymbolResolve] = resolve)
)
}
/**
*
* @param context
*/
resolveContext(context) {
this.finish('resolveContext')
context[SymbolResolve](this.e)
}
/**
2024-06-11 19:45:04 +08:00
* @deprecated
2024-06-08 20:52:49 +08:00
* @param plugin
* @param tpl
* @param data
* @param cfg
* @returns
*/
async renderImg(plugin, tpl, data, cfg) {
return Common.render(plugin, tpl, data, { ...cfg, e: this.e })
}
2024-06-08 21:07:07 +08:00
}
2024-06-12 09:47:21 +08:00
/**
* @deprecated
*/
export const plugin = Plugin