From 56fecdb35f0a5b3269fae4211c459aa829585cdc Mon Sep 17 00:00:00 2001 From: yeyang <746659424@qq.com> Date: Sat, 8 Apr 2023 01:35:54 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E7=BE=A4=E5=A4=B4=E8=A1=94?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E8=AF=8D=E5=88=86=E5=BC=80=E7=BE=A4=E5=8D=95?= =?UTF-8?q?=E7=8B=AC=EF=BC=8C=E9=9C=80=E8=A6=81=E9=87=8D=E6=96=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.cjs | 2 +- apps/admin.js | 75 ------------------- apps/groupAdmin.js | 16 ++-- apps/groupBannedWords.js | 101 ++++++++++++++++++++++---- config/default_config/groupTitle.yaml | 5 -- config/system/gpAdmin_system.js | 4 +- model/GroupBannedWords.js | 35 +++++++++ 7 files changed, 133 insertions(+), 105 deletions(-) delete mode 100644 config/default_config/groupTitle.yaml diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 71c19cb..36004c3 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -20,6 +20,6 @@ module.exports = { 'prefer-const': ['off'], 'arrow-body-style': 'off', 'camelcase': 'off', - 'quote-props': ['error', 'consistent-as-needed'] + 'quote-props': ['error', 'consistent'] } } diff --git a/apps/admin.js b/apps/admin.js index 02b35d9..2680176 100644 --- a/apps/admin.js +++ b/apps/admin.js @@ -91,16 +91,6 @@ export class Admin extends plugin { reg: '^#椰奶(启用|禁用)全部通知$', fnc: 'SetAllNotice', permission: 'master' - }, - { - reg: '^#(增加|减少|查看)头衔屏蔽词.*$', - fnc: 'ProhibitedTitle', - permission: 'master' - }, - { - reg: '^#切换头衔屏蔽词匹配(模式)?$', - fnc: 'ProhibitedTitlePattern', - permission: 'master' } ] }) @@ -218,71 +208,6 @@ export class Admin extends plugin { scale: 1.4 }) } - - // 增删查头衔屏蔽词 - async ProhibitedTitle (e) { - // 获取现有的头衔屏蔽词 - let shieldingWords = Config.groupTitle.Shielding_words - // 判断是否需要查看头衔屏蔽词 - if (/查看/.test(e.msg)) { - // 返回已有的头衔屏蔽词列表 - return e.reply(`现有的头衔屏蔽词如下:${shieldingWords.join('\n')}`) - } - - // 获取用户输入的要增加或删除的屏蔽词 - let message = e.msg.replace(/#|(增加|减少)头衔屏蔽词/g, '').trim().split(',') - // 判断用户是要增加还是删除屏蔽词 - let isAddition = /增加/.test(e.msg) - let existingWords = [] - let newWords = [] - - // 遍历用户输入的屏蔽词,区分已有和新的屏蔽词 - for (let word of message) { - if (shieldingWords.includes(word)) { - existingWords.push(word) - } else { - newWords.push(word) - } - } - - // 去重 - existingWords = _.compact(_.uniq(existingWords)) - newWords = _.compact(_.uniq(newWords)) - - // 判断是要增加还是删除屏蔽词 - if (isAddition) { - // 添加新的屏蔽词 - if (!_.isEmpty(newWords)) { - for (let word of newWords) { - Config.modifyarr('groupTitle', 'Shielding_words', word, 'add') - } - e.reply(`✅ 成功添加:${newWords.join(',')}`) - } - // 提示已有的屏蔽词 - if (!_.isEmpty(existingWords)) { - e.reply(`❎ 以下词已存在:${existingWords.join(',')}`) - } - } else { - // 删除已有的屏蔽词 - if (!_.isEmpty(existingWords)) { - for (let word of existingWords) { - Config.modifyarr('groupTitle', 'Shielding_words', word, 'del') - } - e.reply(`✅ 成功删除:${existingWords.join(',')}`) - } - // 提示不在屏蔽词中的词 - if (!_.isEmpty(newWords)) { - e.reply(`❎ 以下词未在屏蔽词中:${newWords.join(',')}`) - } - } - } - - // 修改头衔匹配模式 - async ProhibitedTitlePattern (e) { - let data = Config.groupTitle.Match_pattern ? 0 : 1 - Config.modify('groupTitle', 'Match_pattern', data) - e.reply(`✅ 已修改匹配模式为${data ? '精确' : '模糊'}匹配`) - } } // 随机底图 diff --git a/apps/groupAdmin.js b/apps/groupAdmin.js index 86f58b6..9fc89f2 100644 --- a/apps/groupAdmin.js +++ b/apps/groupAdmin.js @@ -2,7 +2,7 @@ import _ from 'lodash' import moment from 'moment' import plugin from '../../../lib/plugins/plugin.js' import { Config } from '../components/index.js' -import { common, GroupAdmin as Ga, puppeteer, QQApi } from '../model/index.js' +import { common, GroupAdmin as Ga, puppeteer, QQApi, GroupBannedWords } from '../model/index.js' import cronValidate from '../tools/cronValidate.js' import { groupTitleMsg } from '../constants/msg.js' import { Time_unit } from '../constants/other.js' @@ -306,15 +306,15 @@ export class GroupAdmin extends plugin { let Title = e.msg.replace(/#|申请头衔/g, '') // 屏蔽词处理 - let { Match_pattern, Shielding_words } = Config.groupTitle - - Shielding_words = _.compact(Shielding_words) - if (!e.isMaster && !_.isEmpty(Shielding_words)) { - if (Match_pattern) { - let reg = new RegExp(Shielding_words.join('|')) + let TitleFilterModeChange = GroupBannedWords.getTitleFilterModeChange(e.group_id) + let TitleBannedWords = GroupBannedWords.getTitleBannedWords(e.group_id) + TitleBannedWords = _.compact(TitleBannedWords) + if (!e.isMaster && !_.isEmpty(TitleBannedWords)) { + if (TitleFilterModeChange) { + let reg = new RegExp(TitleBannedWords.join('|')) if (reg.test(Title)) return e.reply('这里面有不好的词汇哦~', true) } else { - if (Shielding_words.includes(Title)) return e.reply('这是有不好的词汇哦~', true) + if (TitleBannedWords.includes(Title)) return e.reply('这是有不好的词汇哦~', true) } } let res = await e.group.setTitle(e.user_id, Title) diff --git a/apps/groupBannedWords.js b/apps/groupBannedWords.js index 95e530e..6c5680e 100644 --- a/apps/groupBannedWords.js +++ b/apps/groupBannedWords.js @@ -32,6 +32,14 @@ export class NewGroupBannedWords extends plugin { { reg: '^#?设置违禁词禁言时间(\\d+)$', fnc: 'muteTime' + }, + { + reg: '^#(增加|减少|查看)头衔屏蔽词.*$', + fnc: 'ProhibitedTitle' + }, + { + reg: '^#切换头衔屏蔽词匹配(模式)?$', + fnc: 'ProhibitedTitlePattern' } ] @@ -46,36 +54,40 @@ export class NewGroupBannedWords extends plugin { if (_.isEmpty(groupBannedWords)) { return false } - let keyWord = e.toString() + const KeyWord = e.toString() .replace(/#|#/g, '') .replace(`{at:${Bot.uin}}`, '') .trim() - keyWord = this.trimAlias(keyWord) - const word = _.find(Object.keys(groupBannedWords), wrd => _.includes(keyWord, wrd)) - if (!word) return false + const trimmedKeyWord = this.trimAlias(KeyWord) + const matchingWord = Object.keys(groupBannedWords).find((word) => + _.includes(trimmedKeyWord, word) + ) + if (!matchingWord) return false - const type = groupBannedWords[word] - const isAccurateModeOK = type.matchType == 1 && keyWord === word - const isVagueModeOK = type.matchType == 2 && _.includes(keyWord, word) + const type = groupBannedWords[matchingWord] + const isAccurateModeOK = type.matchType == 1 && trimmedKeyWord === matchingWord + const isVagueModeOK = type.matchType == 2 && _.includes(trimmedKeyWord, matchingWord) const isOK = isAccurateModeOK || isVagueModeOK const punishments = { - 1: async () => await e.member.kick(), - 2: async () => await e.member.mute(GroupBannedWords.getMuteTime(e.group_id)), - 3: async () => await e.recall(), - 4: async () => { + '1': async () => await e.member.kick(), + '2': async () => await e.member.mute(GroupBannedWords.getMuteTime(e.group_id)), + '3': async () => await e.recall(), + '4': async () => { await e.member.kick() await e.recall() }, - 5: async () => { + '5': async () => { await e.member.mute(GroupBannedWords.getMuteTime(e.group_id)) await e.recall() } } if (isOK && punishments[type.penaltyType]) { await punishments[type.penaltyType]() + const keyWordTran = await GroupBannedWords.keyWordTran(matchingWord) + const senderCard = e.sender.card || e.sender.nickname e.reply([ - `触发违禁词:${await GroupBannedWords.keyWordTran(word)}\n`, - `触发者:${e.sender.card || e.sender.nickname}(${e.user_id})\n`, + `触发违禁词:${keyWordTran}\n`, + `触发者:${senderCard}(${e.user_id})\n`, `执行:${GroupBannedWords.penaltyTypeMap[type.penaltyType]}` ]) } @@ -169,4 +181,65 @@ export class NewGroupBannedWords extends plugin { return msg } + + // 增删查头衔屏蔽词 + async ProhibitedTitle (e) { + // 获取现有的头衔屏蔽词 + let shieldingWords = GroupBannedWords.getTitleBannedWords(e.group_id) + // 判断是否需要查看头衔屏蔽词 + if (/查看/.test(e.msg)) { + // 返回已有的头衔屏蔽词列表 + return e.reply(`现有的头衔屏蔽词如下:${shieldingWords.join('\n')}`) + } + + // 获取用户输入的要增加或删除的屏蔽词 + let message = e.msg.replace(/#|(增加|减少)头衔屏蔽词/g, '').trim().split(',') + // 判断用户是要增加还是删除屏蔽词 + let isAddition = /增加/.test(e.msg) + let existingWords = [] + let newWords = [] + + // 遍历用户输入的屏蔽词,区分已有和新的屏蔽词 + for (let word of message) { + if (shieldingWords.includes(word)) { + existingWords.push(word) + } else { + newWords.push(word) + } + } + + // 去重 + existingWords = _.compact(_.uniq(existingWords)) + newWords = _.compact(_.uniq(newWords)) + + // 判断是要增加还是删除屏蔽词 + if (isAddition) { + // 添加新的屏蔽词 + if (!_.isEmpty(newWords)) { + GroupBannedWords.addTitleBannedWords(e.group_id, newWords) + e.reply(`✅ 成功添加:${newWords.join(',')}`) + } + // 提示已有的屏蔽词 + if (!_.isEmpty(existingWords)) { + e.reply(`❎ 以下词已存在:${existingWords.join(',')}`) + } + } else { + // 删除已有的屏蔽词 + if (!_.isEmpty(existingWords)) { + GroupBannedWords.delTitleBannedWords(e.group_id, existingWords) + e.reply(`✅ 成功删除:${existingWords.join(',')}`) + } + // 提示不在屏蔽词中的词 + if (!_.isEmpty(newWords)) { + e.reply(`❎ 以下词未在屏蔽词中:${newWords.join(',')}`) + } + } + } + + // 修改头衔匹配模式 + async ProhibitedTitlePattern (e) { + if (!common.Authentication(e, 'admin', 'admin')) return false + let res = GroupBannedWords.setTitleFilterModeChange(e.group_id) + e.reply(`✅ 已修改匹配模式为${res ? '精确' : '模糊'}匹配`) + } } diff --git a/config/default_config/groupTitle.yaml b/config/default_config/groupTitle.yaml deleted file mode 100644 index 621b7d0..0000000 --- a/config/default_config/groupTitle.yaml +++ /dev/null @@ -1,5 +0,0 @@ -#匹配模式 1为模糊 0为精确 -Match_pattern: 1 -#屏蔽词 -Shielding_words: - - \ No newline at end of file diff --git a/config/system/gpAdmin_system.js b/config/system/gpAdmin_system.js index 9686c3e..8438754 100644 --- a/config/system/gpAdmin_system.js +++ b/config/system/gpAdmin_system.js @@ -245,8 +245,8 @@ export const helpList = [{ group: '违禁词', list: [ { - title: '#新增(模糊|精确)?(踢|禁|撤|踢撤|禁撤)?违禁词.*', - desc: '前面为匹配模式后面为惩罚', + title: '#新增(模糊|精确)?<惩罚>?违禁词.*', + desc: '惩罚包括踢、禁、撤、踢撤、禁撤', icon: 7 }, { diff --git a/model/GroupBannedWords.js b/model/GroupBannedWords.js index c6314aa..40e69bf 100644 --- a/model/GroupBannedWords.js +++ b/model/GroupBannedWords.js @@ -17,6 +17,7 @@ export default new class { } this.dataCach = {} this.muteTimeCach = {} + this.groupTitleCach = {} } addBannedWords ( @@ -124,4 +125,38 @@ export default new class { return false } } + + setTitleFilterModeChange (groupId) { + let data = Data.readJSON(`${groupId}.json`, this.root) + data.TitleFilterModeChange = data.TitleFilterModeChange ? 0 : 1 + Data.writeJSON(`${groupId}.json`, data, this.root) + return data.TitleFilterModeChange + } + + getTitleFilterModeChange (groupId) { + let data = Data.readJSON(`${groupId}.json`, this.root) + return data.TitleFilterModeChange ?? 0 + } + + addTitleBannedWords (groupId, arr) { + let data = Data.readJSON(`${groupId}.json`, this.root) + if (!data.TitleBannedWords)data.TitleBannedWords = [] + data.TitleBannedWords.push(...arr) + Data.writeJSON(`${groupId}.json`, data, this.root) + delete this.groupTitleCach[groupId] + } + + getTitleBannedWords (groupId) { + if (this.groupTitleCach[groupId]) return this.groupTitleCach[groupId] + let data = Data.readJSON(`${groupId}.json`, this.root).TitleBannedWords ?? [] + this.groupTitleCach[groupId] = data + return data + } + + delTitleBannedWords (groupId, arr) { + let data = Data.readJSON(`${groupId}.json`, this.root) + data.TitleBannedWords = _.differenceBy(data.TitleBannedWords, arr) + Data.writeJSON(`${groupId}.json`, data, this.root) + delete this.groupTitleCach[groupId] + } }()