新增 白名单用户

This commit is contained in:
🌌 2024-03-14 21:05:39 +08:00
parent f3ea055694
commit ca46134b81
4 changed files with 111 additions and 163 deletions

View File

@ -14,9 +14,11 @@ disableMsg: "私聊功能已禁用仅支持发送cookie抽卡记录链接
# 私聊通行字符串 # 私聊通行字符串
disableAdopt: disableAdopt:
- stoken - stoken
#白名单群,配置后只在该群生效 #白名单群,配置后只在该群生效
whiteGroup: whiteGroup:
#白名单qq
whiteQQ:
#黑名单群 #黑名单群
blackGroup: blackGroup:
- 213938015 - 213938015

View File

@ -2,9 +2,9 @@
post_type: message post_type: message
message_type: group message_type: group
sub_type: normal sub_type: normal
group_id: 213938015 group_id: 123456789
group_name: '2333' group_name: '喵崽'
user_id: 805475874 user_id: 1145141919
# 测试命令 # 测试命令
text: 十连 text: 十连
card: 测试104070461 card: 测试104070461

View File

@ -8,7 +8,6 @@ import { segment } from 'icqq'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import moment from 'moment' import moment from 'moment'
import path from 'node:path' import path from 'node:path'
import common from '../common/common.js'
import Runtime from './runtime.js' import Runtime from './runtime.js'
import Handler from './handler.js' import Handler from './handler.js'
@ -34,6 +33,11 @@ class PluginsLoader {
/** 插件监听 */ /** 插件监听 */
this.watcher = {} this.watcher = {}
this.eventMap = {
message: ["post_type", "message_type", "sub_type"],
notice: ["post_type", "notice_type", "sub_type"],
request: ["post_type", "request_type", "sub_type"],
}
this.msgThrottle = {} this.msgThrottle = {}
@ -79,7 +83,8 @@ class PluginsLoader {
* @param isRefresh 是否刷新 * @param isRefresh 是否刷新
*/ */
async load(isRefresh = false) { async load(isRefresh = false) {
if (!lodash.isEmpty(this.priority) && !isRefresh) return if (isRefresh) this.priority = []
if (this.priority.length) return
const files = await this.getPlugins() const files = await this.getPlugins()
@ -94,7 +99,7 @@ class PluginsLoader {
)) ))
this.packageTips(packageErr) this.packageTips(packageErr)
this.creatTask() this.createTask()
logger.info(`加载定时任务[${this.task.length}个]`) logger.info(`加载定时任务[${this.task.length}个]`)
logger.info(`加载插件[${this.pluginCount}个]`) logger.info(`加载插件[${this.pluginCount}个]`)
@ -188,45 +193,32 @@ class PluginsLoader {
if (!this.checkBlack(e)) return if (!this.checkBlack(e)) return
/** 处理回复 */ /** 处理回复 */
this.reply(e) this.reply(e)
/** 过滤事件 */
let priority = []
/** 注册runtime */ /** 注册runtime */
await Runtime.init(e) await Runtime.init(e)
this.priority.forEach(v => { const priority = []
let p = new v.class(e) for (const i of this.priority) {
const p = new i.class(e)
p.e = e p.e = e
/** 判断是否启用功能 */ /** 判断是否启用功能,过滤事件 */
if (!this.checkDisable(e, p)) return if (this.checkDisable(p) && this.filtEvent(e, p))
/** 过滤事件 */
if (!this.filtEvent(e, p)) return
priority.push(p) priority.push(p)
}) }
for (let plugin of priority) { for (const plugin of priority) {
/** 上下文hook */ /** 上下文hook */
if (plugin.getContext) { if (!plugin.getContext) continue
let context = plugin.getContext() const context = {
...plugin.getContext(),
...plugin.getContext(false, true),
}
if (!lodash.isEmpty(context)) { if (!lodash.isEmpty(context)) {
for (let fnc in context) { for (const fnc in context)
plugin[fnc](context[fnc]) plugin[fnc](context[fnc])
}
return return
} }
} }
/** 群上下文hook */
if (plugin.getContextGroup) {
let context = plugin.getContextGroup()
if (!lodash.isEmpty(context)) {
for (let fnc in context) {
plugin[fnc](context[fnc])
}
return
}
}
}
/** 是否只关注主动at */ /** 是否只关注主动at */
if (!this.onlyReplyAt(e)) return if (!this.onlyReplyAt(e)) return
@ -290,23 +282,16 @@ class PluginsLoader {
/** 过滤事件 */ /** 过滤事件 */
filtEvent(e, v) { filtEvent(e, v) {
if (!v.event) return false if (!v.event) return false
let event = v.event.split('.') const event = v.event.split(".")
let eventMap = { const eventMap = this.eventMap[e.post_type] || []
message: ['post_type', 'message_type', 'sub_type'], const newEvent = []
notice: ['post_type', 'notice_type', 'sub_type'], for (const i in event) {
request: ['post_type', 'request_type', 'sub_type'] if (event[i] == "*")
newEvent.push(event[i])
else
newEvent.push(e[eventMap[i]])
} }
let newEvent = [] return v.event == newEvent.join(".")
event.forEach((val, index) => {
if (val === '*') {
newEvent.push(val)
} else if (eventMap[e.post_type]) {
newEvent.push(e[eventMap[e.post_type][index]])
}
})
newEvent = newEvent.join('.')
return v.event === newEvent
} }
/** 判断权限 */ /** 判断权限 */
@ -327,13 +312,13 @@ class PluginsLoader {
e.reply('数据加载中,请稍后再试') e.reply('数据加载中,请稍后再试')
return false return false
} }
if (v.permission === 'owner') { if (v.permission == 'owner') {
if (!e.member.is_owner) { if (!e.member.is_owner) {
e.reply('暂无权限,只有群主才能操作') e.reply('暂无权限,只有群主才能操作')
return false return false
} }
} }
if (v.permission === 'admin') { if (v.permission == 'admin') {
if (!e.member.is_admin) { if (!e.member.is_admin) {
e.reply('暂无权限,只有管理员才能操作') e.reply('暂无权限,只有管理员才能操作')
return false return false
@ -586,41 +571,27 @@ class PluginsLoader {
} }
/** 收集定时任务 */ /** 收集定时任务 */
collectTask (task) { collectTask(task) {
if (Array.isArray(task)) { for (const i of Array.isArray(task) ? task : [task])
task.forEach((val) => { if (i.cron && i.name)
if (!val.cron) return this.task.push(i)
if (!val.name) throw new Error('插件任务名称错误')
this.task.push(val)
})
} else {
if (task.fnc && task.cron) {
if (!task.name) throw new Error('插件任务名称错误')
this.task.push(task)
}
}
} }
/** 创建定时任务 */ /** 创建定时任务 */
creatTask () { createTask() {
if (process.argv[1].includes('test')) return for (const i of this.task)
this.task.forEach((val) => { i.job = schedule.scheduleJob(i.cron, async () => {
val.job = schedule.scheduleJob(val.cron, async () => {
try { try {
if (val.log === true) { if (i.log == true)
logger.mark(`开始定时任务:${val.name}`) logger.mark(`开始定时任务:${i.name}`)
} await i.fnc()
let res = val.fnc() if (i.log == true)
if (util.types.isPromise(res)) res = await res logger.mark(`定时任务完成:${i.name}`)
if (val.log === true) {
logger.mark(`定时任务完成:${val.name}`)
}
} catch (error) { } catch (error) {
logger.error(`定时任务报错:${val.name}`) logger.error(`定时任务报错:${i.name}`)
logger.error(error) logger.error(error)
} }
}) })
})
} }
/** 检查命令冷却cd */ /** 检查命令冷却cd */
@ -700,54 +671,39 @@ class PluginsLoader {
/** 判断黑白名单 */ /** 判断黑白名单 */
checkBlack (e) { checkBlack (e) {
let other = cfg.getOther() const other = cfg.getOther()
let notice = cfg.getNotice()
if (e.test) return true
/** 黑名单qq */ /** 黑名单qq */
if (other.blackQQ) { if (other.blackQQ?.length) {
if (other.blackQQ.includes(Number(e.user_id) || String(e.user_id))) { if (other.blackQQ.includes(Number(e.user_id) || String(e.user_id)))
return false
if (e.at && other.blackQQ.includes(Number(e.at) || String(e.at)))
return false return false
} }
if (e.at && other.blackQQ.includes(Number(e.at) || String(e.at))) { /** 白名单qq */
if (other.whiteQQ?.length)
if (!other.whiteQQ.includes(Number(e.user_id) || String(e.user_id)))
return false return false
}
}
if (e.group_id) { if (e.group_id) {
/** 白名单群 */
if (Array.isArray(other.whiteGroup) && other.whiteGroup.length > 0) {
return other.whiteGroup.includes(Number(e.group_id) || String(e.group_id))
}
/** 黑名单群 */ /** 黑名单群 */
if (Array.isArray(other.blackGroup) && other.blackGroup.length > 0) { if (other.blackGroup?.length && other.blackGroup.includes(Number(e.group_id) || String(e.group_id)))
return !other.blackGroup.includes(Number(e.group_id) || String(e.group_id)) return false
} /** 白名单群 */
if (other.whiteGroup?.length && !other.whiteGroup.includes(Number(e.group_id) || String(e.group_id)))
return false
} }
return true return true
} }
/** 判断是否启用功能 */ /** 判断是否启用功能 */
checkDisable (e, p) { checkDisable(p) {
let groupCfg = cfg.getGroup(e.group_id) const groupCfg = cfg.getGroup(p.e.group_id)
if (!lodash.isEmpty(groupCfg.enable)) { if (groupCfg.disable?.length && groupCfg.disable.includes(p.name))
if (groupCfg.enable.includes(p.name)) {
return true
}
// logger.debug(`${e.logText}[${p.name}]功能已禁用`)
return false return false
} if (groupCfg.enable?.length && !groupCfg.enable.includes(p.name))
if (!lodash.isEmpty(groupCfg.disable)) {
if (groupCfg.disable.includes(p.name)) {
// logger.debug(`${e.logText}[${p.name}]功能已禁用`)
return false return false
}
return true
}
return true return true
} }
@ -821,5 +777,4 @@ class PluginsLoader {
this.watcher[dirName] = watcher this.watcher[dirName] = watcher
} }
} }
export default new PluginsLoader() export default new PluginsLoader()

View File

@ -1,6 +1,6 @@
import { Common } from '#miao' import { Common } from '#miao'
let stateArr = {} const stateArr = {}
export default class plugin { export default class plugin {
/** /**
@ -24,14 +24,14 @@ export default class plugin {
* @param task.fnc 定时任务方法名 * @param task.fnc 定时任务方法名
* @param task.log false时不显示执行日志 * @param task.log false时不显示执行日志
*/ */
constructor ({ constructor({
name = 'your-plugin', name = "your-plugin",
dsc = '无', dsc = "无",
handler, handler,
namespace, namespace,
event = 'message', event = "message",
priority = 5000, priority = 5000,
task = { fnc: '', cron: '' }, task = { fnc: "", cron: "" },
rule = [] rule = []
}) { }) {
/** 插件名称 */ /** 插件名称 */
@ -45,18 +45,18 @@ export default class plugin {
/** 定时任务,可以是数组 */ /** 定时任务,可以是数组 */
this.task = { this.task = {
/** 任务名 */ /** 任务名 */
name: '', name: "",
/** 任务方法名 */ /** 任务方法名 */
fnc: task.fnc || '', fnc: task.fnc || "",
/** 任务cron表达式 */ /** 任务cron表达式 */
cron: task.cron || '' cron: task.cron || ""
} }
/** 命令规则 */ /** 命令规则 */
this.rule = rule this.rule = rule
if (handler) { if (handler) {
this.handler = handler this.handler = handler
this.namespace = namespace || '' this.namespace = namespace || ""
} }
} }
@ -66,16 +66,16 @@ export default class plugin {
* @param data.recallMsg 群聊是否撤回消息0-1200不撤回 * @param data.recallMsg 群聊是否撤回消息0-1200不撤回
* @param data.at 是否at用户 * @param data.at 是否at用户
*/ */
reply (msg = '', quote = false, data = {}) { reply(msg = "", quote = false, data = {}) {
if (!this.e.reply || !msg) return false if (!this.e?.reply || !msg) return false
return this.e.reply(msg, quote, data) return this.e.reply(msg, quote, data)
} }
conKey (isGroup = false) { conKey(isGroup = false) {
if (isGroup) { if (isGroup) {
return `${this.name}.${this.e.group_id}` return `${this.name}.${this.e.group_id}`
} else { } else {
return `${this.name}.${this.userId || this.e.user_id}` return `${this.name}.${this.e.user_id}`
} }
} }
@ -84,45 +84,36 @@ export default class plugin {
* @param isGroup 是否群聊 * @param isGroup 是否群聊
* @param time 操作时间默认120秒 * @param time 操作时间默认120秒
*/ */
setContext (type, isGroup = false, time = 120) { setContext(type, isGroup, time = 120) {
let key = this.conKey(isGroup) const key = this.conKey(isGroup)
if (!stateArr[key]) stateArr[key] = {} if (!stateArr[key]) stateArr[key] = {}
stateArr[key][type] = this.e stateArr[key][type] = this.e
if (time) { if (time) stateArr[key][type].timeout = setTimeout(() => {
/** 操作时间 */
setTimeout(() => {
if (stateArr[key][type]) { if (stateArr[key][type]) {
delete stateArr[key][type] delete stateArr[key][type]
this.e.reply('操作超时已取消', true) this.reply("操作超时已取消", true)
} }
}, time * 1000) }, time * 1000)
} }
}
getContext () { getContext(type, isGroup) {
let key = this.conKey() if (type) return stateArr[this.conKey(isGroup)]?.[type]
return stateArr[key] return stateArr[this.conKey(isGroup)]
}
getContextGroup () {
let key = this.conKey(true)
return stateArr[key]
} }
/** /**
* @param type 执行方法 * @param type 执行方法
* @param isGroup 是否群聊 * @param isGroup 是否群聊
*/ */
finish (type, isGroup = false) { finish(type, isGroup) {
if (stateArr[this.conKey(isGroup)] && stateArr[this.conKey(isGroup)][type]) { const key = this.conKey(isGroup)
delete stateArr[this.conKey(isGroup)][type] if (stateArr[key] && stateArr[key][type]) {
clearTimeout(stateArr[key][type].timeout)
delete stateArr[key][type]
} }
} }
async renderImg (plugin, tpl, data, cfg) { async renderImg(plugin, tpl, data, cfg) {
return Common.render(plugin, tpl, data, { return Common.render(plugin, tpl, data, { ...cfg, e: this.e })
...cfg,
e: this.e
})
} }
} }