2023-11-07 03:56:44 +08:00
|
|
|
|
import { Common } from '#miao'
|
|
|
|
|
|
2024-03-14 21:05:39 +08:00
|
|
|
|
const stateArr = {}
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
export default class plugin {
|
|
|
|
|
/**
|
|
|
|
|
* @param name 插件名称
|
|
|
|
|
* @param dsc 插件描述
|
2023-10-15 05:35:17 +08:00
|
|
|
|
* @param handler handler配置
|
|
|
|
|
* @param handler.key handler支持的事件key
|
|
|
|
|
* @param handler.fn handler的处理func
|
|
|
|
|
* @param namespace namespace,设置handler时建议设置
|
2023-03-04 14:30:13 +08:00
|
|
|
|
* @param event 执行事件,默认message
|
|
|
|
|
* @param priority 优先级,数字越小优先级越高
|
2023-10-15 05:35:17 +08:00
|
|
|
|
* @param rule
|
2023-03-04 14:30:13 +08:00
|
|
|
|
* @param rule.reg 命令正则
|
|
|
|
|
* @param rule.fnc 命令执行方法
|
|
|
|
|
* @param rule.event 执行事件,默认message
|
|
|
|
|
* @param rule.log false时不显示执行日志
|
|
|
|
|
* @param rule.permission 权限 master,owner,admin,all
|
2023-10-15 05:35:17 +08:00
|
|
|
|
* @param task
|
2023-03-04 14:30:13 +08:00
|
|
|
|
* @param task.name 定时任务名称
|
|
|
|
|
* @param task.cron 定时任务cron表达式
|
|
|
|
|
* @param task.fnc 定时任务方法名
|
|
|
|
|
* @param task.log false时不显示执行日志
|
|
|
|
|
*/
|
2024-03-14 21:05:39 +08:00
|
|
|
|
constructor({
|
|
|
|
|
name = "your-plugin",
|
|
|
|
|
dsc = "无",
|
|
|
|
|
handler,
|
|
|
|
|
namespace,
|
|
|
|
|
event = "message",
|
|
|
|
|
priority = 5000,
|
|
|
|
|
task = { fnc: "", cron: "" },
|
|
|
|
|
rule = []
|
|
|
|
|
}) {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 插件名称 */
|
2023-04-05 22:11:54 +08:00
|
|
|
|
this.name = name
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 插件描述 */
|
2023-04-05 22:11:54 +08:00
|
|
|
|
this.dsc = dsc
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 监听事件,默认message https://oicqjs.github.io/oicq/#events */
|
2023-04-05 22:11:54 +08:00
|
|
|
|
this.event = event
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 优先级 */
|
2023-04-05 22:11:54 +08:00
|
|
|
|
this.priority = priority
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 定时任务,可以是数组 */
|
|
|
|
|
this.task = {
|
|
|
|
|
/** 任务名 */
|
2024-03-14 21:05:39 +08:00
|
|
|
|
name: "",
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 任务方法名 */
|
2024-03-14 21:05:39 +08:00
|
|
|
|
fnc: task.fnc || "",
|
2023-03-04 14:30:13 +08:00
|
|
|
|
/** 任务cron表达式 */
|
2024-03-14 21:05:39 +08:00
|
|
|
|
cron: task.cron || ""
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
/** 命令规则 */
|
2023-04-05 22:11:54 +08:00
|
|
|
|
this.rule = rule
|
2023-10-15 05:35:17 +08:00
|
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
|
this.handler = handler
|
2024-03-14 21:05:39 +08:00
|
|
|
|
this.namespace = namespace || ""
|
2023-10-15 05:35:17 +08:00
|
|
|
|
}
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-15 05:35:17 +08:00
|
|
|
|
* @param msg 发送的消息
|
|
|
|
|
* @param quote 是否引用回复
|
|
|
|
|
* @param data.recallMsg 群聊是否撤回消息,0-120秒,0不撤回
|
|
|
|
|
* @param data.at 是否at用户
|
|
|
|
|
*/
|
2024-03-14 21:05:39 +08:00
|
|
|
|
reply(msg = "", quote = false, data = {}) {
|
|
|
|
|
if (!this.e?.reply || !msg) return false
|
2023-03-04 14:30:13 +08:00
|
|
|
|
return this.e.reply(msg, quote, data)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 21:05:39 +08:00
|
|
|
|
conKey(isGroup = false) {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (isGroup) {
|
|
|
|
|
return `${this.name}.${this.e.group_id}`
|
|
|
|
|
} else {
|
2024-03-16 19:47:34 +08:00
|
|
|
|
return `${this.name}.${this.userId || this.e.user_id}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param type 执行方法
|
|
|
|
|
* @param isGroup 是否群聊
|
|
|
|
|
* @param time 操作时间,默认120秒
|
2024-03-26 13:47:35 +08:00
|
|
|
|
* @param reply 超时时回复的内容,false则不回复
|
2023-03-04 14:30:13 +08:00
|
|
|
|
*/
|
2024-03-26 13:47:35 +08:00
|
|
|
|
setContext(type, isGroup, time = 120, reply = "操作超时已取消") {
|
2024-03-14 21:05:39 +08:00
|
|
|
|
const key = this.conKey(isGroup)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (!stateArr[key]) stateArr[key] = {}
|
|
|
|
|
stateArr[key][type] = this.e
|
2024-03-14 21:05:39 +08:00
|
|
|
|
if (time) stateArr[key][type].timeout = setTimeout(() => {
|
|
|
|
|
if (stateArr[key][type]) {
|
|
|
|
|
delete stateArr[key][type]
|
2024-03-26 13:47:35 +08:00
|
|
|
|
this.reply(reply, true)
|
2024-03-14 21:05:39 +08:00
|
|
|
|
}
|
|
|
|
|
}, time * 1000)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 21:05:39 +08:00
|
|
|
|
getContext(type, isGroup) {
|
|
|
|
|
if (type) return stateArr[this.conKey(isGroup)]?.[type]
|
|
|
|
|
return stateArr[this.conKey(isGroup)]
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param type 执行方法
|
|
|
|
|
* @param isGroup 是否群聊
|
|
|
|
|
*/
|
2024-03-14 21:05:39 +08:00
|
|
|
|
finish(type, isGroup) {
|
|
|
|
|
const key = this.conKey(isGroup)
|
|
|
|
|
if (stateArr[key] && stateArr[key][type]) {
|
|
|
|
|
clearTimeout(stateArr[key][type].timeout)
|
|
|
|
|
delete stateArr[key][type]
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-07 03:56:44 +08:00
|
|
|
|
|
2024-03-14 21:05:39 +08:00
|
|
|
|
async renderImg(plugin, tpl, data, cfg) {
|
|
|
|
|
return Common.render(plugin, tpl, data, { ...cfg, e: this.e })
|
2023-11-07 03:56:44 +08:00
|
|
|
|
}
|
2024-03-14 21:05:39 +08:00
|
|
|
|
}
|