Miao-Yunzai/lib/plugins/plugin.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-05-11 16:03:18 +08:00
let stateArr = {}
export default class plugin {
/**
* @param name 插件名称
* @param dsc 插件描述
* @param event 执行事件默认message
* @param priority 优先级数字越小优先级越高
* @param rule.reg 命令正则
* @param rule.fnc 命令执行方法
* @param rule.event 执行事件默认message
* @param rule.log false时不显示执行日志
* @param rule.permission 权限 master,owner,admin,all
* @param task.name 定时任务名称
* @param task.cron 定时任务cron表达式
* @param task.fnc 定时任务方法名
* @param task.log false时不显示执行日志
*/
constructor({ name = 'your-plugin', dsc = '无', event = 'message', priority = 5000, task = { fnc: '', cron: '' }, rule = [] }) {
/** 插件名称 */
this.name = name
/** 插件描述 */
this.dsc = dsc
/** 监听事件默认message */
2023-05-11 16:03:18 +08:00
this.event = event
/** 优先级 */
this.priority = priority
/** 定时任务,可以是数组 */
this.task = {
/** 任务名 */
name: '',
/** 任务方法名 */
fnc: task.fnc || '',
/** 任务cron表达式 */
cron: task.cron || ''
}
/** 命令规则 */
this.rule = rule
}
/**
2023-07-29 13:47:45 +08:00
* @param msg 发送的消息
* @param quote 是否引用回复
* @param data.recallMsg 是否撤回消息0-1200不撤回
* @param data.at 是否提及用户
*/
reply(msg = '', quote = false, data = {}) {
2023-05-11 16:03:18 +08:00
if (!this.e.reply || !msg) return false
return this.e.reply(msg, quote, data)
}
2023-07-29 13:47:45 +08:00
conKey(isGroup = false) {
2023-05-11 16:03:18 +08:00
if (isGroup) {
return `${this.name}.${this.e.group_id}`
} else {
return `${this.name}.${this.userId || this.e.user_id}`
}
}
/**
* @param type 执行方法
* @param isGroup 是否群聊
* @param time 操作时间默认120秒
*/
2023-07-29 13:47:45 +08:00
setContext(type, isGroup = false, time = 120) {
2023-05-11 16:03:18 +08:00
let key = this.conKey(isGroup)
if (!stateArr[key]) stateArr[key] = {}
stateArr[key][type] = this.e
if (time) {
/** 操作时间 */
setTimeout(() => {
if (stateArr[key][type]) {
delete stateArr[key][type]
this.e.reply('操作超时已取消', true)
}
}, time * 1000)
}
}
2023-07-29 13:47:45 +08:00
getContext() {
2023-05-11 16:03:18 +08:00
let key = this.conKey()
return stateArr[key]
}
2023-07-29 13:47:45 +08:00
getContextGroup() {
2023-05-11 16:03:18 +08:00
let key = this.conKey(true)
return stateArr[key]
}
/**
* @param type 执行方法
* @param isGroup 是否群聊
*/
2023-07-29 13:47:45 +08:00
finish(type, isGroup = false) {
2023-05-11 16:03:18 +08:00
if (stateArr[this.conKey(isGroup)] && stateArr[this.conKey(isGroup)][type]) {
delete stateArr[this.conKey(isGroup)][type]
}
}
2023-07-29 13:47:45 +08:00
}