Miao-Yunzai/src/core/plugin.ts

214 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-06-08 20:52:49 +08:00
import { Common } from './local.js'
import { EventType } from './types.js'
const stateArr = {}
const SymbolTimeout = Symbol('Timeout')
const SymbolResolve = Symbol('Resolve')
2024-06-08 21:01:41 +08:00
export class plugin {
2024-06-08 20:52:49 +08:00
name = 'your-plugin'
dsc = '无'
rule: {
reg?: RegExp | string
fnc: string
event?: string
log?: boolean
permission?: string
}[] = []
event = 'message'
priority = 9999
task = null
namespace = null
handler = null
e: EventType
/**
* @param name
* @param dsc
* @param handler handler配置
* @param handler.key handler支持的事件key
* @param handler.fn handler的处理func
* @param namespace namespacehandler时建议设置
* @param event message
* @param priority
* @param rule
* @param rule.reg
* @param rule.fnc
* @param rule.event message
* @param rule.log false时不显示执行日志
* @param rule.permission master,owner,admin,all
* @param task
* @param task.name
* @param task.cron cron表达式
* @param task.fnc
* @param task.log false时不显示执行日志
*/
constructor({
name,
dsc,
handler,
namespace,
event,
priority = 5000,
task,
rule
}: {
name?: typeof this.name
dsc?: typeof this.dsc
namespace?: typeof this.namespace
priority?: typeof this.priority
handler?: typeof this.handler
event?: typeof this.event
task?: typeof this.task
rule?: typeof this.rule
}) {
name && (this.name = name)
dsc && (this.dsc = dsc)
event && (this.event = event)
priority && (this.priority = priority)
/** 插件名称 */
this.name = name
/** 插件描述 */
this.dsc = dsc
/** 监听事件默认message https://oicqjs.github.io/oicq/#events */
this.event = event
/** 优先级 */
this.priority = priority
/** 定时任务,可以是数组 */
this.task = {
/** 任务名 */
name: '',
/** 任务方法名 */
fnc: task.fnc || '',
/** 任务cron表达式 */
cron: task.cron || ''
}
/** 命令规则 */
this.rule = rule
if (handler) {
this.handler = handler
this.namespace = namespace || ''
}
}
/**
* @param msg
* @param quote
* @param data.recallMsg 0-1200
* @param data.at at用户
*/
reply(msg = '', quote = false, data = {}) {
if (!this.e?.reply || !msg) return false
return this.e.reply(msg, quote, data)
}
/**
* ******
* tudo
*
* *****
*/
group_id: number
groupId: number
user_id: number
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)
if (!stateArr[key]) stateArr[key] = {}
stateArr[key][type] = this.e
if (time)
stateArr[key][type][SymbolTimeout] = setTimeout(() => {
if (stateArr[key][type]) {
const resolve = stateArr[key][type][SymbolResolve]
delete stateArr[key][type]
resolve ? resolve(false) : this.reply(timeout, true)
}
}, time * 1000)
return stateArr[key][type]
}
/**
*
* @param type
* @param isGroup
* @returns
*/
getContext(type: string, isGroup?: boolean) {
if (type) return stateArr[this.conKey(isGroup)]?.[type]
return stateArr[this.conKey(isGroup)]
}
/**
*
* @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)
if (stateArr[key]?.[type]) {
clearTimeout(stateArr[key][type][SymbolTimeout])
delete stateArr[key][type]
}
}
/**
*
* @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)
}
/**
*
* @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
}