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
|
|
|
|
* namespace,设置handler时建议设置
|
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-120秒,0不撤回
|
|
|
|
|
* @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
|