diff --git a/apps/assistant/BlockOne.js b/apps/assistant/BlockOne.js index dd89bf9..1cd327e 100644 --- a/apps/assistant/BlockOne.js +++ b/apps/assistant/BlockOne.js @@ -1,6 +1,4 @@ -import fs from "node:fs" -import yaml from "yaml" -import { Config, Version } from "../../components/index.js" +import { Config, Version, YamlReader } from "../../components/index.js" import { common } from "../../model/index.js" export class BlockOne extends plugin { @@ -20,111 +18,95 @@ export class BlockOne extends plugin { } ] }) + this.configPath = "config/config/other.yaml" + this.type = "" } async BlockOne(e) { if (!common.checkPermission(e, "master")) return - let type = "" - let name = "拉" - if (/^#拉白(群聊?)?/.test(this.e.msg)) { - type += "white" - name += "白" - } else { - type += "black" - name += "黑" - } - if (/^#拉[黑白](群聊?)/.test(this.e.msg)) { - type += "Group" - name += "群" - } else { - type += Version.name == "TRSS-Yunzai" ? "User" : "QQ" - } - const configPath = "config/config/other.yaml" - /** 判断at */ - if (this.e.at) { - this.blackResult = this.e.at - } else { - if (Version.name == "TRSS-Yunzai") { - /** TRSS-Yunzai匹配所有字符 */ - const blackId = this.e.msg.replace(/^#拉[黑白](群聊?)?/, "").trim() - this.blackResult = Number(blackId) || String(blackId) - } else { - const match = this.e.msg.match(/\d+/) - if (match?.[0]) { this.blackResult = Number(match[0]) || String(match[0]) } - } - } + this._getType("拉") + this._getBlackResult(/^#拉[黑白](群聊?)?/) + const { masterQQ } = Config if (!this.blackResult || common.getPermission(new Proxy({ - get isMaster() { return Config.masterQQ.includes(this.user_id) || Config.masterQQ.includes(String(this.user_id)) }, + get isMaster() { + return masterQQ.includes(this.user_id) || masterQQ.includes(String(this.user_id)) + }, user_id: this.blackResult }, { get: (target, prop, receiver) => target[prop] ?? e[prop] - }), "master") === true) { return this.e.reply(`❎ ${name}失败,没有键入用户或群号`) } + }), "master") === true) { + return this.e.reply(`❎ ${this.name}失败,没有键入用户或群号`) + } try { - const yamlContentBuffer = await fs.promises.readFile(configPath) - const yamlContent = yamlContentBuffer.toString("utf-8") - const data = yaml.parse(yamlContent) - if (!Array.isArray(data[type])) { data[type] = [] } - if (!data[type].includes(this.blackResult)) { - data[type].push(this.blackResult) - const updatedYaml = yaml.stringify(data, { quote: false }) - const resultYaml = updatedYaml.replace(/"/g, "") - await fs.promises.writeFile(configPath, resultYaml, "utf-8") - await this.e.reply(`✅ 已把这个坏淫${name}掉惹!!!`) + const yaml = new YamlReader(this.configPath) + const data = yaml.get(this.type) + if (!data?.includes?.(this.blackResult)) { + if (!data || !Array.isArray(data)) { + yaml.set(this.type, [ this.blackResult ]) + } else { + yaml.addIn(this.type, this.blackResult) + } + await this.e.reply(`✅ 已把这个坏淫${this.name}掉惹!!!`) } else { - await this.e.reply(`❎ 已把这个坏淫${name}过辣`) + await this.e.reply(`❎ 已把这个坏淫${this.name}过辣`) } } catch (error) { - await this.e.reply(`❎ 额...${name}失败哩,可能这个淫比较腻害>_<`) + await this.e.reply(`❎ 额...${this.name}失败哩,可能这个淫比较腻害>_<`) logger.error(error) } } async CancelBlockOne(e) { if (!common.checkPermission(e, "master")) return - let type = "" - let name = "取消拉" - if (/^#(取消|删除|移除)(群聊?)?拉白(群聊?)?/.test(this.e.msg)) { - type += "white" - name += "白" - } else { - type += "black" - name += "黑" + this._getType("取消拉") + this._getBlackResult(/^#(取消|(删|移)除)拉[黑白](群聊?)?/) + + if (!this.blackResult) { return this.e.reply(`❎ ${this.name}失败,没有键入用户或群号`) } + try { + const yaml = new YamlReader(this.configPath) + const data = yaml.get(this.type) + if (Array.isArray(data) && data.includes(this.blackResult)) { + const item = data.indexOf(this.blackResult) + yaml.delete(`${this.type}.${item}`) + await this.e.reply(`✅ 已把这个坏淫${this.name}掉惹!!!`) + } else { + await this.e.reply(`❎ ${this.name}失败,找不到辣>_<`) + } + } catch (error) { + await this.e.reply(`❎ 额...${this.name}失败哩,可能这个淫比较腻害>_<`) + logger.error(error) } - if (/^#(取消|删除|移除)拉[黑白](群聊?)/.test(this.e.msg)) { - type += "Group" - name += "群" - } else { - type += Version.name == "TRSS-Yunzai" ? "User" : "QQ" - } - const configPath = "config/config/other.yaml" + } + + _getBlackResult(reg) { if (this.e.at) { this.blackResult = this.e.at } else { if (Version.name == "TRSS-Yunzai") { - const blackId = this.e.msg.replace(/^#(取消|(删|移)除)拉[黑白](群聊?)?/, "").trim() + /** TRSS-Yunzai匹配所有字符 */ + const blackId = this.e.msg.replace(reg, "").trim() this.blackResult = Number(blackId) || String(blackId) } else { const match = this.e.msg.match(/\d+/) if (match?.[0]) { this.blackResult = Number(match[0]) || String(match[0]) } } } - if (!this.blackResult) { return this.e.reply(`❎ ${name}失败,没有键入用户或群号`) } - try { - const yamlContentBuffer = await fs.promises.readFile(configPath) - const yamlContent = yamlContentBuffer.toString("utf-8") - const data = yaml.parse(yamlContent) - if (Array.isArray(data[type]) && data[type].includes(this.blackResult)) { - const itemToRemove = this.blackResult.toString() - data[type] = data[type].filter(item => item.toString() !== itemToRemove) - const updatedYaml = yaml.stringify(data) - await fs.promises.writeFile(configPath, updatedYaml, "utf-8") - await this.e.reply(`✅ 已把这个坏淫${name}掉惹!!!`) - } else { - await this.e.reply(`❎ ${name}失败,找不到辣>_<`) - } - } catch (error) { - await this.e.reply(`❎ 额...${name}失败哩,可能这个淫比较腻害>_<`) - logger.error(error) + } + + _getType(name) { + this.name = name + if (/拉白/.test(this.e.msg)) { + this.type += "white" + this.name += "白" + } else { + this.type += "black" + this.name += "黑" + } + if (/群/.test(this.e.msg)) { + this.type += "Group" + this.name += "群" + } else { + this.type += Version.name == "TRSS-Yunzai" ? "User" : "QQ" } } } diff --git a/components/YamlReader.js b/components/YamlReader.js index 65b1317..317cdf1 100644 --- a/components/YamlReader.js +++ b/components/YamlReader.js @@ -54,7 +54,7 @@ export default class YamlReader { this.save() } - /* 删除key */ + /* 删除数组数据 */ delete(keyPath) { this.document.deleteIn(keyPath.split(".")) this.save() diff --git a/lib/common/common.js b/lib/common/common.js index 8fbd0d6..34bb6dd 100644 --- a/lib/common/common.js +++ b/lib/common/common.js @@ -21,7 +21,9 @@ export default new class extends sendMsgMod { * @param {object} opts.groupObj - 群对象 * @returns {boolean|string} - 是否具有权限 */ - getPermission(e, permission = "all", role = "all", { groupObj = e.group || e.bot.pickGroup(e.group_id) } = {}) { + getPermission(e, permission = "all", role = "all", { + groupObj = e.group || e.bot.pickGroup(e.group_id) + } = {}) { if (!groupObj && permission != "master" && role != "all") throw new Error("未获取到群对象") if (role == "owner" && !groupObj.is_owner) { return "❎ Bot权限不足,需要群主权限"