diff --git a/apps/setu.js b/apps/setu.js index 771b1d0..dc183ec 100644 --- a/apps/setu.js +++ b/apps/setu.js @@ -34,7 +34,7 @@ export class sese extends plugin { permission: 'master' }, { - reg: `(c|C)(d|D)(${NumReg})(s|秒)?$`, + reg: `^#?(c|C)(d|D)(${NumReg})(s|秒)?$`, fnc: 'atSetCd', event: 'message.group', permission: 'master' @@ -51,7 +51,7 @@ export class sese extends plugin { async seturd(e) { if (!Config.getGroup(e.group_id).sesepro && !e.isMaster) return e.reply(SWITCH_ERROR) - let iscd = setu.getcd(e) + let iscd = setu.getremainingCd(e) if (iscd) return e.reply(` ${setu.CDMsg}你的CD还有${iscd}`, false, { at: true }) @@ -78,7 +78,7 @@ export class sese extends plugin { async setutag(e) { if (!Config.getGroup(e.group_id).sesepro && !e.isMaster) return e.reply(SWITCH_ERROR) - let iscd = setu.getcd(e) + let iscd = setu.getremainingCd(e) if (iscd) return e.reply(` ${setu.CDMsg}你的CD还有${iscd}`, false, { at: true }) @@ -136,7 +136,9 @@ export class sese extends plugin { //艾特设置cd async atSetCd(e) { - if (e.message[0].type != "at") return false; + let qq = e.message.find(item => item.type == 'at')?.qq + + if (!qq) return false; let cd = e.msg.match(new RegExp(NumReg)) @@ -144,8 +146,6 @@ export class sese extends plugin { cd = common.translateChinaNum(cd[0]) - let qq = e.message[0].qq - setu.setUserCd(e, qq, cd) } diff --git a/components/Data.js b/components/Data.js index 2f54a6a..6421c63 100644 --- a/components/Data.js +++ b/components/Data.js @@ -34,9 +34,7 @@ let Data = { }) }, - /* - * 读取json - * */ + /**读取json */ readJSON(file = '', root = '') { root = getRoot(root) if (fs.existsSync(`${root}/${file}`)) { @@ -49,15 +47,19 @@ let Data = { return {} }, - /* - * 写JSON - * */ - writeJSON(file, data, space = '\t', root = '') { + /**写JSON */ + writeJSON(file, data, root = '', space = '\t') { // 检查并创建目录 Data.createDir(file, root, true) root = getRoot(root) - delete data._res - return fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space)) + // delete data._res + try { + fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space)) + return true + } catch (err) { + logger.error(err) + return false; + } }, async getCacheJSON(key) { diff --git a/model/common.js b/model/common.js index 9569965..3b35a2a 100644 --- a/model/common.js +++ b/model/common.js @@ -1,7 +1,7 @@ import common from '../../../lib/common/common.js' -import fs from 'fs' import Config from '../components/Config.js' import child_process from 'child_process' +import setu from './setu.js' export default new class newCommon { @@ -13,30 +13,6 @@ export default new class newCommon { return new Promise((resolve) => setTimeout(resolve, ms)) } - /** 读取文件 */ - getJson(path) { - try { - return JSON.parse(fs.readFileSync(path, 'utf8')) - } catch (err) { - logger.error('读取失败') - logger.error(err) - return false - } - } - - /** 写入json文件 */ - setJson(path, cot = {}) { - try { - fs.writeFileSync(path, JSON.stringify(cot, '', '\t')) - return true - } catch (error) { - logger.error('写入失败') - logger.error(err) - return false - } - - } - /** 发消息 */ async sendMasterMsg(msg) { if (Config.Notice.notificationsAll) { @@ -48,7 +24,6 @@ export default new class newCommon { } else { // 发给第一个管理 await common.relpyPrivate(Config.masterQQ[0], msg) - await common.sleep(200) } } @@ -141,7 +116,7 @@ export default new class newCommon { * @return {*} */ async recallsendMsg(e, msg, time = 0, isfk = true) { - time = time || this.getRecallTime(e.group_id); + time = time || setu.getRecallTime(e.group_id); //发送消息 let res = await e.reply(msg, false, { recallMsg: time }) @@ -163,7 +138,7 @@ export default new class newCommon { * @return {Boolean} */ async getRecallsendMsg(e, msg, isBot = true, isfk = true) { - let time = this.getRecallTime(e.group_id) + let time = setu.getRecallTime(e.group_id) let res = await this.getforwardMsg(e, msg, time, isBot, isfk) @@ -172,27 +147,6 @@ export default new class newCommon { return true; } - /** - * @description: 获取群的撤回时间 - * @param {*} e oicq - * @return {Number} - */ - getRecallTime(groupId) { - if (!groupId) return 0; - let path = "./plugins/yenai-plugin/config/setu/setu.json" - //获取撤回时间 - let cfgs = {}; - let time = 120; - if (fs.existsSync(path)) { - cfgs = this.getJson(path) - } - - if (cfgs[groupId]) { - time = cfgs[groupId].recall - } - return time - } - /** * @description: 取cookie * @param {String} data 如:qun.qq.com diff --git a/model/setu.js b/model/setu.js index ff1d978..6d5569b 100644 --- a/model/setu.js +++ b/model/setu.js @@ -1,22 +1,17 @@ import { segment } from "oicq"; import fetch from 'node-fetch' -import fs from 'fs' import { common } from './index.js'; +import { Data } from '../components/index.js'; import lodash from "lodash"; +const _path = process.cwd() export default new class setu { constructor() { - //配置文件夹 - this.path_folder = "./plugins/yenai-plugin/config/setu" - //群配置路径 - this.path = "./plugins/yenai-plugin/config/setu/setu.json" - //私聊配置 - this.path_s = "./plugins/yenai-plugin/config/setu/setu_s.json" - this.apicfg = "./plugins/yenai-plugin/config/setu/api.json" + this.root = `${_path}/plugins/yenai-plugin/config/setu` //默认配置 this.def = { r18: 0, - recall: 30, + recall: 120, cd: 300, } //存cd的变量 @@ -25,9 +20,7 @@ export default new class setu { this.init() } async init() { - if (!fs.existsSync(this.path_folder)) { - fs.mkdirSync(this.path_folder) - } + Data.createDir('config/setu') } /** @@ -39,10 +32,10 @@ export default new class setu { */ async setuapi(e, r18, num = 1, tag = "") { let api = "https://api.lolicon.app/setu/v2"; - if (fs.existsSync(this.apicfg)) { - let apicfg = common.getJson(this.apicfg) - if (apicfg.api) api = apicfg.api - } + + let apicfg = Data.readJSON('api.json', this.root) + if (apicfg.api) api = apicfg.api + let size = "original" let proxy = await redis.get(`yenai:proxy`) if (num > 6) { @@ -88,46 +81,44 @@ export default new class setu { */ async sendMsg(e, msg) { //默认CD - let cd = this.def.cd + let cd = this.getCfgCd(e); //获取当前时间 let present = parseInt(new Date().getTime() / 1000) //发送消息 let res = await common.getRecallsendMsg(e, msg, false) - if (e.isGroup) { - //获取CD - if (fs.existsSync(this.path)) { - let groupCD = common.getJson(this.path) - if (groupCD[e.group_id]) cd = groupCD[e.group_id].cd - } - if (!e.isMaster && res) { - if (cd != 0) { - this.temp[e.user_id + e.group_id] = present + cd - setTimeout(() => { - delete this.temp[e.user_id + e.group_id]; - }, cd * 1000); - } - } - - } else { - //私聊 - if (fs.existsSync(this.path_s)) { - let friendCD = common.getJson(this.path_s) - if (friendCD[e.user_id]) cd = friendCD[e.user_id] - } - if (!e.isMaster && res) { - if (cd != 0) { - this.temp[e.user_id] = present + cd - setTimeout(() => { - delete this.temp[e.user_id]; - }, cd * 1000); - } + if (!res) return false; + //设置CD + if (!e.isMaster || !cd) { + if (e.isGroup) { + this.temp[e.user_id + e.group_id] = present + cd + setTimeout(() => { + delete this.temp[e.user_id + e.group_id]; + }, cd * 1000); + } else { + this.temp[e.user_id] = present + cd + setTimeout(() => { + delete this.temp[e.user_id]; + }, cd * 1000); } } } - //CD - getcd(e) { + //获取配置CD + getCfgCd(e) { + let cd = this.def.cd;//300 + if (e.isGroup) { + let groupCD = Data.readJSON(`setu.json`, this.root) + if (groupCD[e.group_id]?.cd) cd = groupCD[e.group_id].cd + } else { + let friendCD = Data.readJSON(`setu_s.json`, this.root) + if (friendCD[e.user_id]?.cd) cd = friendCD[e.user_id] + } + return cd + } + + //获取剩余CD时间 + getremainingCd(e) { //获取现在的时间并转换为秒 let present = parseInt(new Date().getTime() / 1000) @@ -162,24 +153,30 @@ export default new class setu { * @return {String} 0或1 */ getR18(e) { - let cfgs + let R18 = this.def.r18 if (e.isGroup) { - //获取配置 - if (fs.existsSync(this.path)) { - cfgs = common.getJson(this.path) - if (cfgs[e.group_id]) { - return cfgs[e.group_id].r18 - } - } + let groupCfg = Data.readJSON(`setu.json`, this.root) + if (groupCfg[e.group_id]?.r18) R18 = groupCfg[e.group_id].r18 } else { - if (fs.existsSync(this.path_s)) { - cfgs = common.getJson(this.path_s) - if (cfgs.friendr18 !== undefined) { - return cfgs.friendr18 - } - } + let friendCfg = Data.readJSON(`setu_s.json`, this.root) + if (friendCfg[e.user_id]?.friendr18) R18 = friendCfg[e.user_id].friendr18 } - return this.def.r18 + return R18 + } + + /** + * @description: 获取群的撤回时间 + * @param {*} e oicq + * @return {Number} + */ + getRecallTime(groupId) { + if (!groupId) return 0; + //获取撤回时间 + let groupCfg = Data.readJSON(`setu.json`, this.root) + let recalltime = this.def.recall; + + if (groupCfg[groupId]) recalltime = groupCfg[groupId].recall + return recalltime } /** * @description: 设置群cd和撤回时间 @@ -188,16 +185,14 @@ export default new class setu { * @param {Boolean} type 为true设置撤回时间反之设置CD * @return {Boolean} */ - setGroupRecallTimeAndCd(groupId, num, type = true) { - let res = {}; + setGroupRecallTimeAndCd(groupId, num, type) { + let data = Data.readJSON("setu.json", this.root) - if (fs.existsSync(this.path)) res = common.getJson(this.path) + if (!data[groupId]) data[groupId] = lodash.cloneDeep(this.def) - if (!res[groupId]) res[groupId] = lodash.cloneDeep(this.def) + type ? data[groupId].recall = Number(num) : data[groupId].cd = Number(num) - type ? res[groupId].recall = Number(num) : res[groupId].cd = Number(num) - - return common.setJson(this.path, res) ? true : false + return Data.writeJSON("setu.json", data, this.root) } /** * @description: 设置CD @@ -206,12 +201,10 @@ export default new class setu { * @param {String} cd 设置的cd */ setUserCd(e, qq, cd) { - let res = {}; - if (fs.existsSync(this.path_s)) { - res = common.getJson(this.path_s) - } - res[qq] = Number(cd) - if (common.setJson(this.path_s, res)) { + let data = Data.readJSON("setu_s.json", this.root) + + data[qq] = Number(cd) + if (Data.writeJSON("setu_s.json", data, this.root)) { e.reply(`✅ 设置用户${qq}的cd成功,cd时间为${cd}秒`) delete this.temp[qq] return true @@ -220,44 +213,26 @@ export default new class setu { return false } } + /** * @description: 设置r18 * @param {String|Number} groupID 群聊id为假时设置私聊 * @param {Boolean} isopen 开启或关闭 */ setR18(groupID, isopen) { - let res = {}; + let data = Data.readJSON(`setu${!groupID ? "_s" : ""}.json`, this.root) if (groupID) { - if (fs.existsSync(this.path)) { - res = common.getJson(this.path) - } - - if (!res[groupID]) res[groupID] = lodash.cloneDeep(this.def) - - - res[groupID].r18 = isopen ? 1 : 0 - - if (common.setJson(this.path, res)) { - logger.mark(`[椰奶R18][群聊]已${isopen ? "开启" : "关闭"}${groupID}的涩涩模式`) - return true - } else { - logger.mark(`[椰奶R18][群聊]设置失败`) - return false - } + if (!data[groupID]) data[groupID] = lodash.cloneDeep(this.def) + data[groupID].r18 = isopen ? 1 : 0 } else { - if (fs.existsSync(this.path_s)) { - res = common.getJson(this.path_s) - } - - res.friendr18 = isopen ? 1 : 0 - - if (common.setJson(this.path_s, res)) { - logger.mark(`[椰奶R18][私聊]已${isopen ? "开启" : "关闭"}私聊涩涩功能~`) - return true - } else { - logger.mark(`[椰奶R18][私聊]设置失败`) - return false - } + data.friendr18 = isopen ? 1 : 0 + } + if (Data.writeJSON(`setu${!groupID ? "_s" : ""}.json`, data, this.root)) { + logger.mark(`[椰奶R18][${groupID ? "群聊" : "私聊"}]已${isopen ? "开启" : "关闭"}${groupID}的涩涩模式`) + return true + } else { + logger.mark(`[椰奶R18][${groupID ? "群聊" : "私聊"}]设置失败`) + return false } } @@ -268,24 +243,13 @@ export default new class setu { */ getSeSeConfig(e) { let set = lodash.cloneDeep(this.def) - if (e.isGroup) { - //获取群聊单独cd - if (fs.existsSync(this.path)) { - let groupCD = common.getJson(this.path) - if (groupCD[e.group_id]) set.cd = groupCD[e.group_id].cd - } - set.recall = common.getRecallTime(e.group_id) - } else { - //获取私聊单独cd - if (fs.existsSync(this.path_s)) { - let friendCD = common.getJson(this.path_s) - if (friendCD[e.user_id]) set.cd = friendCD[e.user_id] - } - delete set.recall - } + set.cd = this.getCfgCd(e) set.r18 = this.getR18(e) + set.recall = this.getRecallTime(e.group_id) + if (!e.isGroup) delete set.recall return set } + /** * @description: 格式化秒 * @param {Number} value 秒 @@ -312,6 +276,10 @@ export default new class setu { return result } + + + + /**开始执行文案 */ get startMsg() { return lodash.sample([ "正在给你找setu了,你先等等再冲~", @@ -320,7 +288,7 @@ export default new class setu { "奴家马上去给你找瑟瑟的图片~" ]) } - + /**CD中文案 */ get CDMsg() { return lodash.sample([ "你这么喜欢色图,还不快点冲!", @@ -334,7 +302,7 @@ export default new class setu { "你是被下半身控制了大脑吗?" ]) } - + /**发送图片文案 */ get sendMsgs() { return lodash.sample([ "给大佬递图",