新增 白名单用户

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:
- stoken
#白名单群,配置后只在该群生效
whiteGroup:
#白名单qq
whiteQQ:
#黑名单群
blackGroup:
- 213938015

View File

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

View File

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

View File

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