diff --git a/apps/manage/remind.js b/apps/manage/remind.js index ced0f2c..6dade1c 100644 --- a/apps/manage/remind.js +++ b/apps/manage/remind.js @@ -2,6 +2,8 @@ import settings from '../../lib/settings.js'; import { rulePrefix } from '../../lib/common.js'; import { ZZZPlugin } from '../../lib/plugin.js'; +const USER_CONFIGS_KEY = 'ZZZ:REMIND:USER_CONFIGS'; + export class RemindManage extends ZZZPlugin { constructor() { super({ @@ -11,19 +13,83 @@ export class RemindManage extends ZZZPlugin { priority: 40, // 管理插件优先级较高 rule: [ { - reg: `${rulePrefix}设置提醒时间\\s*(.+)`, - fnc: 'setCron', + reg: `${rulePrefix}设置全局提醒\\s*(每日\\d+时|每周.\\d+时)`, + fnc: 'setGlobalRemind', permission: 'master', }, + { + reg: `${rulePrefix}(设置|修改)个人提醒时间\\s*(每日\\d+时|每周.\\d+时)`, + fnc: 'setMyRemindTime', + }, + { + reg: `${rulePrefix}个人提醒时间$`, + fnc: 'viewMyRemindTime', + }, + { + reg: `${rulePrefix}取消个人提醒时间`, + fnc: 'deleteMyRemindTime', + }, ], }); } - async setCron() { - const match = this.e.msg.match(/设置提醒时间\s*(.+)/); + async getUserConfig(userId) { + const userConfigJson = await redis.hGet(USER_CONFIGS_KEY, String(userId)); + return userConfigJson ? JSON.parse(userConfigJson) : null; + } + + async setUserConfig(userId, config) { + await redis.hSet(USER_CONFIGS_KEY, String(userId), JSON.stringify(config)); + } + + async setGlobalRemind() { + const match = this.e.msg.match(/(每日\d+时|每周.\d+时)/); if (!match) return; - const cron = match.trim(); - settings.setSingleConfig('remind', 'cron', cron); - await this.reply(`式舆防卫战/危局强袭战提醒的定时任务已更新为: ${cron}`); + const remindTime = match.trim(); + + // 将全局提醒时间写入yaml配置 + settings.setConfig('remind.globalRemindTime', remindTime); + await this.reply(`全局提醒时间已更新为: ${remindTime}。`); + } + + async setMyRemindTime() { + const match = this.e.msg.match(/(每日\d+时|每周.\d+时)/); + if (!match) return; + const remindTime = match.trim(); + + let userConfig = await this.getUserConfig(this.e.user_id); + if (!userConfig) { + const defaultConfig = settings.getConfig('remind'); + userConfig = { + enable: false, + abyssCheckLevel: defaultConfig.abyssCheckLevel, + deadlyStars: defaultConfig.deadlyStars, + }; + } + + userConfig.remindTime = remindTime; + await this.setUserConfig(this.e.user_id, userConfig); + await this.reply(`您的个人提醒时间已设置为: ${remindTime}`); + } + + async viewMyRemindTime() { + const userConfig = await this.getUserConfig(this.e.user_id); + if (userConfig && userConfig.remindTime) { + await this.reply(`当前提醒时间: ${userConfig.remindTime}`); + } else { + const globalRemindTime = settings.getConfig('remind.globalRemindTime') || '每日20时'; + await this.reply(`个人提醒时间未设置,默认使用全局时间: ${globalRemindTime}`); + } + } + + async deleteMyRemindTime() { + let userConfig = await this.getUserConfig(this.e.user_id); + if (userConfig && userConfig.remindTime) { + delete userConfig.remindTime; + await this.setUserConfig(this.e.user_id, userConfig); + await this.reply('个人提醒时间已取消'); + } else { + await this.reply('个人提醒时间尚未设置'); + } } } \ No newline at end of file diff --git a/apps/remind.js b/apps/remind.js index e125c92..70f3d49 100644 --- a/apps/remind.js +++ b/apps/remind.js @@ -38,16 +38,14 @@ export class Remind extends ZZZPlugin { ], }); - this.task = { - name: 'ZZZ-Plugin式舆防卫战/危局强袭战提醒任务', - cron: this.getCron(), - fnc: () => this.runTask(), - }; - } - - getCron() { - const remindConfig = settings.getConfig('remind'); - return _.get(remindConfig, 'cron', '0 30 20 * * ? *'); + const globalRemindConfig = settings.getConfig('remind'); + if (globalRemindConfig.enable) { + this.task = { + name: 'ZZZ-Plugin式舆防卫战/危局强袭战提醒任务', + cron: '0 * * * *', // 每小时的第0分钟执行 + fnc: () => this.runTask(), + }; + } } async getUserConfig(userId) { @@ -59,6 +57,31 @@ export class Remind extends ZZZPlugin { await redis.hSet(USER_CONFIGS_KEY, String(userId), JSON.stringify(config)); } + isTimeMatch(remindTime, date) { + if (!remindTime) return false; + + const currentHour = date.getHours(); + const currentDay = date.getDay(); // 0 = 周日, 1 = 周一, ..., 6 = 周六 + + if (remindTime.includes('每日')) { + const match = remindTime.match(/每日(\d+)时/); + if (match) { + const hour = parseInt(match, 10); + return currentHour === hour; + } + } else if (remindTime.includes('每周')) { + const dayMap = { '日': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6 }; + const match = remindTime.match(/每周(.)(\d+)时/); + if (match) { + const dayChar = match; + const hour = parseInt(match, 10); + const day = dayMap[dayChar]; + return currentDay === day && currentHour === hour; + } + } + return false; + } + async subscribe() { const uid = await this.getUID(); if (!uid) { @@ -101,28 +124,28 @@ export class Remind extends ZZZPlugin { } async setMyAbyssThreshold() { - const match = this.e.msg.match(/设置式舆阈值\s*(\d+)/); - if (!match) return; - const threshold = Number(match); + const match = this.e.msg.match(/设置式舆阈值\s*(\d+)/); + if (!match) return; + const threshold = Number(match); - if (threshold < 1 || threshold > 7) { - await this.reply('阈值必须在1到7之间'); - return false; - } + if (threshold < 1 || threshold > 7) { + await this.reply('阈值必须在1到7之间'); + return false; + } - let userConfig = await this.getUserConfig(this.e.user_id); - if (!userConfig) { - const defaultConfig = settings.getConfig('remind'); - userConfig = { - enable: false, - abyssCheckLevel: defaultConfig.abyssCheckLevel, - deadlyStars: defaultConfig.deadlyStars, - }; - } + let userConfig = await this.getUserConfig(this.e.user_id); + if (!userConfig) { + const defaultConfig = settings.getConfig('remind'); + userConfig = { + enable: false, + abyssCheckLevel: defaultConfig.abyssCheckLevel, + deadlyStars: defaultConfig.deadlyStars, + }; + } - userConfig.abyssCheckLevel = threshold; - await this.setUserConfig(this.e.user_id, userConfig); - await this.reply(`式舆防卫战提醒阈值已设为: 检查前 ${threshold} 层`); + userConfig.abyssCheckLevel = threshold; + await this.setUserConfig(this.e.user_id, userConfig); + await this.reply(`式舆防卫战提醒阈值已设为: 检查前 ${threshold} 层`); } async setMyDeadlyThreshold() { @@ -221,15 +244,21 @@ export class Remind extends ZZZPlugin { logger.info('[ZZZ-Plugin] 开始执行式舆防卫战/危局强袭战提醒任务'); const allUserConfigs = await redis.hGetAll(USER_CONFIGS_KEY); + const now = new Date(); + const globalRemindTime = globalRemindConfig.globalRemindTime || '每日20时'; for (const userId in allUserConfigs) { const userConfig = JSON.parse(allUserConfigs[userId]); if (!userConfig.enable) continue; - const messages = await this.checkUser(userId, userConfig); - if (messages.length > 0) { - const user = Bot.pickUser(userId); - await user.sendMsg(messages.join('\n')); + const remindTime = userConfig.remindTime || globalRemindTime; + + if (this.isTimeMatch(remindTime, now)) { + const messages = await this.checkUser(userId, userConfig); + if (messages.length > 0) { + const user = Bot.pickUser(userId); + await user.sendMsg(messages.join('\n')); + } } } logger.info('[ZZZ-Plugin] 式舆防卫战/危局强袭战提醒任务执行完毕'); diff --git a/defSet/remind.yaml b/defSet/remind.yaml index 3a7b482..b88621c 100644 --- a/defSet/remind.yaml +++ b/defSet/remind.yaml @@ -1,4 +1,4 @@ enable: true # 功能总开关 -cron: 0 30 20 * * ? * # 定时任务cron表达式,默认每天晚上8:30 +globalRemindTime: '每日20时' # 全局提醒时间,用于没有个人设置的用户 abyssCheckLevel: 7 # 式舆防卫战提醒检查的最高关卡(用户可自行设置,最高为7) deadlyStars: 6 # 危局强袭战星星阈值 \ No newline at end of file diff --git a/guoba.support.js b/guoba.support.js index 24b470e..3719524 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -205,22 +205,23 @@ export function supportGuoba() { component: 'Switch', }, { - field: 'remind.cron', - label: '提醒时间', - bottomHelpMessage: '设置新用户订阅时的默认Cron表达式', - component: 'EasyCron', + field: 'remind.globalRemindTime', + label: '全局提醒时间', + bottomHelpMessage: '设置全局默认提醒时间,格式如"每日20时"或"每周一20时"', + component: 'Input', componentProps: { - placeholder: '请输入或选择cron表达式', + placeholder: '请输入提醒时间,如"每日20时"', }, }, { - field: 'remind.abyssSCount', + field: 'remind.abyssCheckLevel', label: '默认式舆S评级阈值', bottomHelpMessage: '新用户订阅时,S评级数量低于此值会收到提醒', component: 'InputNumber', componentProps: { - min: 0, - placeholder: '请输入数字', + min: 1, + max: 7, + placeholder: '请输入数字(1-7)', }, }, {