From 2770eed4901ea7eccae1d7ed0c32ae4f9c155ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Fri, 19 Apr 2024 00:45:50 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20`=E5=8E=9F=E7=A5=9E?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E5=88=B0=E6=9C=9F=E6=8F=90=E9=86=92`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/apps/mysNews.js | 128 +++++++++++++++++++++++ plugins/genshin/defSet/mys/pushNews.yaml | 3 + 2 files changed, 131 insertions(+) diff --git a/plugins/genshin/apps/mysNews.js b/plugins/genshin/apps/mysNews.js index f10fe04..3569ae4 100644 --- a/plugins/genshin/apps/mysNews.js +++ b/plugins/genshin/apps/mysNews.js @@ -4,6 +4,8 @@ import fs from 'node:fs' import lodash from 'lodash' import gsCfg from '../model/gsCfg.js' import YAML from 'yaml' +import common from '../../../lib/common/common.js' +import fetch from 'node-fetch' gsCfg.cpCfg('mys', 'pushNews') export class mysNews extends plugin { @@ -38,6 +40,9 @@ export class mysNews extends plugin { reg: '^#(星铁|原神|崩坏三|崩三|绝区零|崩坏二|崩二|崩坏学园二|未定|未定事件簿)?推送(公告|资讯)$', permission: 'master', fnc: 'mysNewsTask' + },{ + reg: '^#原神(开启|关闭)到期活动(预警)?(推送)?$', + fnc: 'setActivityPush' } ] }) @@ -68,9 +73,132 @@ export class mysNews extends plugin { async mysNewsTask() { let mysNews = new MysNews(this.e) + this.ActivityPush() await mysNews.mysNewsTask() } + async ActivityPush() { + let now = new Date() + now = now.getHours(); + if(now < 10) return + let pushGroupList + try { + pushGroupList = YAML.parse(fs.readFileSync(this.file, `utf8`)) + } catch (error) { + logger.error(`${this.logFnc} 原神活动到期预警推送失败:无法获取配置文件信息\n${error}`) + return + } + if(!pushGroupList.gsActivityPush || pushGroupList.gsActivityPush == {}) return + let BotidList = [] + for (let item in pushGroupList.gsActivityPush) { + BotidList.push(item) + } + let ActivityList = await this.getGsActivity() + if(ActivityList.length === 0) return + for (let item of BotidList) { + let redisapgl = await redis.get(`Yz:apgl:${item}`) + let date = await this.getDate() + redisapgl = JSON.parse(redisapgl) + if(!redisapgl || redisapgl.date !== date) { + redisapgl = { + date, + GroupList: pushGroupList.gsActivityPush[item] + } + } + if(!Array.isArray(redisapgl.GroupList) || redisapgl.GroupList.length == 0) break + if(!Bot[item]) break + for (let a of ActivityList) { + let endDt = a.end_time + endDt = endDt.replace(/\s/, `T`) + let todayt = new Date() + endDt = new Date(endDt) + let sydate = await this.calculateRemainingTime(todayt, endDt) + let msgList = [ + `【原神活动即将结束通知】`, + `\n活动:${a.subtitle}`, + segment.image(a.banner), + `描述:${a.title}`, + `\n活动剩余时间:${sydate.days}天${sydate.hours}小时${sydate.minutes}分钟${sydate.seconds}秒`, + `\n活动结束时间:${a.end_time}` + ] + await common.sleep(5000) + Bot[item].pickGroup(redisapgl.GroupList[0]).sendMsg(msgList) + } + redisapgl.GroupList.shift() + await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) + } + } + async getDate() { + const currentDate = new Date(); + const year = currentDate.getFullYear(); + const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); + const day = currentDate.getDate().toString().padStart(2, '0'); + return `${year}-${month}-${day}` + } + async getGsActivity() { + let gshd + try { + gshd = await fetch(`https://hk4e-api.mihoyo.com/common/hk4e_cn/announcement/api/getAnnList?game=hk4e&game_biz=hk4e_cn&lang=zh-cn&bundle_id=hk4e_cn&platform=pc®ion=cn_gf01&level=55&uid=100000000`) + gshd = await gshd.json() + } catch { + return [] + } + let hdlist = [] + let result = [] + for (let item of gshd.data.list[1].list) { + if(item.tag_label.includes(`活动`) && !item.title.includes(`传说任务`) && !item.title.includes(`游戏公告`)) hdlist.push(item) + } + for (let item of hdlist) { + let endDt = item.end_time + endDt = endDt.replace(/\s/, `T`) + let todayt = new Date() + endDt = new Date(endDt) + let sydate = await this.calculateRemainingTime(todayt, endDt) + if(sydate.days <= 1) result.push(item) + } + return result + } + async calculateRemainingTime(startDate, endDate) { + const difference = endDate - startDate; + const days = Math.floor(difference / (1000 * 60 * 60 * 24)); + const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((difference % (1000 * 60)) / 1000); + + return { days, hours, minutes, seconds }; + } + async setActivityPush() { + if (!this.e.isGroup) { + await this.reply('推送请在群聊中设置') + return + } + if (!this.e.member?.is_admin && !this.e.isMaster) { + await this.reply('暂无权限,只有管理员才能操作', true) + return true + } + let cfg = gsCfg.getConfig('mys', 'pushNews') + if(!cfg.gsActivityPush) cfg.gsActivityPush = {} + if(!Array.isArray(cfg.gsActivityPush[this.e.self_id])) cfg.gsActivityPush[this.e.self_id] = [] + let model + let msg = `原神活动到期预警推送已` + if(this.e.msg.includes('开启')) { + model = '开启' + cfg.gsActivityPush[this.e.self_id].push(this.e.group_id) + cfg.gsActivityPush[this.e.self_id] = lodash.uniq(cfg.gsActivityPush[this.e.self_id]) + msg += `${model}\n如有即将到期的活动将自动推送至此` + } else { + model = '关闭' + msg += model + cfg.gsActivityPush[this.e.self_id] = lodash.difference(cfg.gsActivityPush[this.e.self_id], [this.e.group_id]) + if (lodash.isEmpty(cfg.gsActivityPush[this.e.self_id])) + delete cfg.gsActivityPush[this.e.self_id] + } + let yaml = YAML.stringify(cfg) + fs.writeFileSync(this.file, yaml, 'utf8') + + logger.mark(`${this.e.logFnc} ${model}原神活动到期预警:${this.e.group_id}`) + await this.reply(msg) + } async mysSearch() { if (/签到/g.test(this.e.msg)) return false let data = await new MysNews(this.e).mysSearch() diff --git a/plugins/genshin/defSet/mys/pushNews.yaml b/plugins/genshin/defSet/mys/pushNews.yaml index 2435a78..cab1f2c 100644 --- a/plugins/genshin/defSet/mys/pushNews.yaml +++ b/plugins/genshin/defSet/mys/pushNews.yaml @@ -19,6 +19,9 @@ gsannounceGroup: {} #原神资讯推送群 gsinfoGroup: {} +#原神活动到期推送群 +ActivityPush: {} + #崩坏星穹铁道公告推送群 srannounceGroup: {} From 1c04530a0e1f91d645d01be7b4f45b9caca1b216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Fri, 19 Apr 2024 00:54:21 +0800 Subject: [PATCH 2/9] update: plugins/genshin/README.md --- plugins/genshin/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/genshin/README.md b/plugins/genshin/README.md index 0f6beb0..61d6d04 100644 --- a/plugins/genshin/README.md +++ b/plugins/genshin/README.md @@ -22,6 +22,7 @@ |#公告,#公告2,#资讯,#活动|米游社原神最新20条公告资讯| |#米游社xxx|米游社原神帖子搜索,如#米游社七七| |#开启、关闭公告推送,#开启、关闭资讯推送|米游社原神公告资讯推送| +|#原神开启、关闭到期活动预警推送|原神活动即将截止时推送| |#原石预估|新版本可获取原石数量预估| From 5c0a0efcc4fd028595178c4f29fa5a8a2aa21790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Fri, 19 Apr 2024 01:37:22 +0800 Subject: [PATCH 3/9] =?UTF-8?q?chore:=20=E6=8E=A8=E9=80=81=E6=97=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/apps/mysNews.js | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/genshin/apps/mysNews.js b/plugins/genshin/apps/mysNews.js index 3569ae4..3bd68de 100644 --- a/plugins/genshin/apps/mysNews.js +++ b/plugins/genshin/apps/mysNews.js @@ -120,6 +120,7 @@ export class mysNews extends plugin { `\n活动剩余时间:${sydate.days}天${sydate.hours}小时${sydate.minutes}分钟${sydate.seconds}秒`, `\n活动结束时间:${a.end_time}` ] + logger.mark(`[米游社活动到期推送] ${a.subtitle}`) await common.sleep(5000) Bot[item].pickGroup(redisapgl.GroupList[0]).sendMsg(msgList) } From 4734dd99ff0059c51d71520f152e701897c2fd68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Fri, 19 Apr 2024 09:25:40 +0800 Subject: [PATCH 4/9] update: plugins/genshin/defSet/mys/pushNews.yaml --- plugins/genshin/defSet/mys/pushNews.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/genshin/defSet/mys/pushNews.yaml b/plugins/genshin/defSet/mys/pushNews.yaml index cab1f2c..304a973 100644 --- a/plugins/genshin/defSet/mys/pushNews.yaml +++ b/plugins/genshin/defSet/mys/pushNews.yaml @@ -20,7 +20,7 @@ gsannounceGroup: {} gsinfoGroup: {} #原神活动到期推送群 -ActivityPush: {} +gsActivityPush: {} #崩坏星穹铁道公告推送群 srannounceGroup: {} From 3391df75b56d049bd6a61e64e1a2c642a784619f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Fri, 19 Apr 2024 12:16:55 +0800 Subject: [PATCH 5/9] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/apps/mysNews.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/genshin/apps/mysNews.js b/plugins/genshin/apps/mysNews.js index 3bd68de..ebfbb26 100644 --- a/plugins/genshin/apps/mysNews.js +++ b/plugins/genshin/apps/mysNews.js @@ -84,7 +84,7 @@ export class mysNews extends plugin { try { pushGroupList = YAML.parse(fs.readFileSync(this.file, `utf8`)) } catch (error) { - logger.error(`${this.logFnc} 原神活动到期预警推送失败:无法获取配置文件信息\n${error}`) + logger.error(`[米游社活动到期推送] 原神活动到期预警推送失败:无法获取配置文件信息\n${error}`) return } if(!pushGroupList.gsActivityPush || pushGroupList.gsActivityPush == {}) return @@ -120,9 +120,10 @@ export class mysNews extends plugin { `\n活动剩余时间:${sydate.days}天${sydate.hours}小时${sydate.minutes}分钟${sydate.seconds}秒`, `\n活动结束时间:${a.end_time}` ] - logger.mark(`[米游社活动到期推送] ${a.subtitle}`) + logger.mark(`[米游社活动到期推送] 开始推送 ${item}:${redisapgl.GroupList[0]} ${a.subtitle}`) await common.sleep(5000) Bot[item].pickGroup(redisapgl.GroupList[0]).sendMsg(msgList) + .then(() => {}).catch((err) => logger.error(`[米游社活动到期推送] ${item}:${redisapgl.GroupList[0]} 推送失败,错误信息${err}`)) } redisapgl.GroupList.shift() await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) From b94063236847974bc96bbd5c4182c23b1a171a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Sat, 20 Apr 2024 01:26:14 +0800 Subject: [PATCH 6/9] =?UTF-8?q?chore:=20=E6=94=AF=E6=8C=81=E6=98=9F?= =?UTF-8?q?=E9=93=81=E6=B4=BB=E5=8A=A8=E5=88=B0=E6=9C=9F=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/README.md | 2 +- plugins/genshin/apps/mysNews.js | 89 ++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/plugins/genshin/README.md b/plugins/genshin/README.md index 61d6d04..53ee5a9 100644 --- a/plugins/genshin/README.md +++ b/plugins/genshin/README.md @@ -22,7 +22,7 @@ |#公告,#公告2,#资讯,#活动|米游社原神最新20条公告资讯| |#米游社xxx|米游社原神帖子搜索,如#米游社七七| |#开启、关闭公告推送,#开启、关闭资讯推送|米游社原神公告资讯推送| -|#原神开启、关闭到期活动预警推送|原神活动即将截止时推送| +|#原神(星铁)开启(关闭)到期活动预警推送|原神(星铁)活动即将截止时推送| |#原石预估|新版本可获取原石数量预估| diff --git a/plugins/genshin/apps/mysNews.js b/plugins/genshin/apps/mysNews.js index ebfbb26..eb22ed5 100644 --- a/plugins/genshin/apps/mysNews.js +++ b/plugins/genshin/apps/mysNews.js @@ -40,8 +40,9 @@ export class mysNews extends plugin { reg: '^#(星铁|原神|崩坏三|崩三|绝区零|崩坏二|崩二|崩坏学园二|未定|未定事件簿)?推送(公告|资讯)$', permission: 'master', fnc: 'mysNewsTask' - },{ - reg: '^#原神(开启|关闭)到期活动(预警)?(推送)?$', + }, + { + reg: '^#(星铁|原神)(开启|关闭)到期活动(预警)?(推送)?$', fnc: 'setActivityPush' } ] @@ -84,15 +85,24 @@ export class mysNews extends plugin { try { pushGroupList = YAML.parse(fs.readFileSync(this.file, `utf8`)) } catch (error) { - logger.error(`[米游社活动到期推送] 原神活动到期预警推送失败:无法获取配置文件信息\n${error}`) + logger.error(`[米游社活动到期推送] 活动到期预警推送失败:无法获取配置文件信息\n${error}`) return } - if(!pushGroupList.gsActivityPush || pushGroupList.gsActivityPush == {}) return + if((!pushGroupList.gsActivityPush || pushGroupList.gsActivityPush == {}) && (!pushGroupList.srActivityPush || pushGroupList.srActivityPush == {})) return let BotidList = [] - for (let item in pushGroupList.gsActivityPush) { + let ActivityPushYaml = {...pushGroupList.gsActivityPush, ...pushGroupList.srActivityPush} + for (let item in ActivityPushYaml) { BotidList.push(item) } - let ActivityList = await this.getGsActivity() + let gsActivityList = await this.getGsActivity() + let srActivityList = await this.getSrActivity() + let ActivityList = [] + for (let item of srActivityList) { + ActivityList.push({ game: `sr`, subtitle: item.title, banner: item.img, title: item.title, end_time: item.end_time }) + } + for (let item of gsActivityList) { + ActivityList.push({ game: 'gs', subtitle: item.subtitle, banner: item.banner, title: item.title, end_time: item.end_time}) + } if(ActivityList.length === 0) return for (let item of BotidList) { let redisapgl = await redis.get(`Yz:apgl:${item}`) @@ -101,19 +111,28 @@ export class mysNews extends plugin { if(!redisapgl || redisapgl.date !== date) { redisapgl = { date, - GroupList: pushGroupList.gsActivityPush[item] + GroupList: ActivityPushYaml[item] } } - if(!Array.isArray(redisapgl.GroupList) || redisapgl.GroupList.length == 0) break - if(!Bot[item]) break + if(!Array.isArray(redisapgl.GroupList) || redisapgl.GroupList.length == 0) continue + if(!Bot[item]) { + redisapgl.GroupList.shift() + await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) + continue + } for (let a of ActivityList) { + if((!pushGroupList.srActivityPush || !pushGroupList.srActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `sr`) continue + if((!pushGroupList.gsActivityPush || !pushGroupList.gsActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `gs`) continue + let pushGame + if(a.game === `sr`) pushGame = `星铁` + if(a.game === `gs`) pushGame = `原神` let endDt = a.end_time endDt = endDt.replace(/\s/, `T`) let todayt = new Date() endDt = new Date(endDt) let sydate = await this.calculateRemainingTime(todayt, endDt) let msgList = [ - `【原神活动即将结束通知】`, + `【${pushGame}活动即将结束通知】`, `\n活动:${a.subtitle}`, segment.image(a.banner), `描述:${a.title}`, @@ -159,6 +178,29 @@ export class mysNews extends plugin { } return result } + async getSrActivity() { + let srhd + try { + srhd = await fetch(`https://hkrpg-api.mihoyo.com/common/hkrpg_cn/announcement/api/getAnnList?game=hkrpg&game_biz=hkrpg_cn&lang=zh-cn&auth_appid=announcement&authkey_ver=1&bundle_id=hkrpg_cn&channel_id=1&level=65&platform=pc®ion=prod_gf_cn&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid=100000000`) + srhd = await srhd.json() + } catch { + return [] + } + let hdlist = [] + let result = [] + for (let item of srhd.data.pic_list[0].type_list[0].list) { + if (item.title) hdlist.push(item) + } + for (let item of hdlist) { + let endDt = item.end_time + endDt = endDt.replace(/\s/, `T`) + let todayt = new Date() + endDt = new Date(endDt) + let sydate = await this.calculateRemainingTime(todayt, endDt) + if (sydate.days <= 1) result.push(item) + } + return result + } async calculateRemainingTime(startDate, endDate) { const difference = endDate - startDate; @@ -178,27 +220,36 @@ export class mysNews extends plugin { await this.reply('暂无权限,只有管理员才能操作', true) return true } + let typeName + let pushGame + if(this.e.msg.includes('星铁')) { + typeName = `srActivityPush` + pushGame = `星铁` + } else { + typeName = `gsActivityPush` + pushGame = `原神` + } let cfg = gsCfg.getConfig('mys', 'pushNews') - if(!cfg.gsActivityPush) cfg.gsActivityPush = {} - if(!Array.isArray(cfg.gsActivityPush[this.e.self_id])) cfg.gsActivityPush[this.e.self_id] = [] + if(!cfg[typeName]) cfg[typeName] = {} + if(!Array.isArray(cfg[typeName][this.e.self_id])) cfg[typeName][this.e.self_id] = [] let model - let msg = `原神活动到期预警推送已` + let msg = `${pushGame}活动到期预警推送已` if(this.e.msg.includes('开启')) { model = '开启' - cfg.gsActivityPush[this.e.self_id].push(this.e.group_id) - cfg.gsActivityPush[this.e.self_id] = lodash.uniq(cfg.gsActivityPush[this.e.self_id]) + cfg[typeName][this.e.self_id].push(this.e.group_id) + cfg[typeName][this.e.self_id] = lodash.uniq(cfg[typeName][this.e.self_id]) msg += `${model}\n如有即将到期的活动将自动推送至此` } else { model = '关闭' msg += model - cfg.gsActivityPush[this.e.self_id] = lodash.difference(cfg.gsActivityPush[this.e.self_id], [this.e.group_id]) - if (lodash.isEmpty(cfg.gsActivityPush[this.e.self_id])) - delete cfg.gsActivityPush[this.e.self_id] + cfg[typeName][this.e.self_id] = lodash.difference(cfg[typeName][this.e.self_id], [this.e.group_id]) + if (lodash.isEmpty(cfg[typeName][this.e.self_id])) + delete cfg[typeName][this.e.self_id] } let yaml = YAML.stringify(cfg) fs.writeFileSync(this.file, yaml, 'utf8') - logger.mark(`${this.e.logFnc} ${model}原神活动到期预警:${this.e.group_id}`) + logger.mark(`${this.e.logFnc} ${model}${pushGame}活动到期预警:${this.e.group_id}`) await this.reply(msg) } async mysSearch() { From faf2e05741f828cda6eaae703d6a938c108def5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Sat, 20 Apr 2024 01:27:24 +0800 Subject: [PATCH 7/9] chore: update pushNews.yaml --- plugins/genshin/defSet/mys/pushNews.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/genshin/defSet/mys/pushNews.yaml b/plugins/genshin/defSet/mys/pushNews.yaml index 304a973..9e54730 100644 --- a/plugins/genshin/defSet/mys/pushNews.yaml +++ b/plugins/genshin/defSet/mys/pushNews.yaml @@ -22,6 +22,9 @@ gsinfoGroup: {} #原神活动到期推送群 gsActivityPush: {} +#星铁活动到期推送群 +srActivityPush: {} + #崩坏星穹铁道公告推送群 srannounceGroup: {} From 68922b06ce333ea86cf2ee4a435adf87a9729d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Sat, 20 Apr 2024 13:54:15 +0800 Subject: [PATCH 8/9] fix: includes is undefined --- plugins/genshin/apps/mysNews.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/genshin/apps/mysNews.js b/plugins/genshin/apps/mysNews.js index eb22ed5..bb5b262 100644 --- a/plugins/genshin/apps/mysNews.js +++ b/plugins/genshin/apps/mysNews.js @@ -121,8 +121,8 @@ export class mysNews extends plugin { continue } for (let a of ActivityList) { - if((!pushGroupList.srActivityPush || !pushGroupList.srActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `sr`) continue - if((!pushGroupList.gsActivityPush || !pushGroupList.gsActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `gs`) continue + if((!pushGroupList.srActivityPush || !pushGroupList.srActivityPush[item] || !pushGroupList.srActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `sr`) continue + if((!pushGroupList.gsActivityPush || !pushGroupList.gsActivityPush[item] || !pushGroupList.gsActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `gs`) continue let pushGame if(a.game === `sr`) pushGame = `星铁` if(a.game === `gs`) pushGame = `原神` From 0cab6268c3767666a01752cb54f99da80d583f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Sun, 21 Apr 2024 14:01:46 +0800 Subject: [PATCH 9/9] chore: ActivityPush migration to model --- plugins/genshin/apps/mysNews.js | 136 +----------------------------- plugins/genshin/model/mysNews.js | 137 +++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 135 deletions(-) diff --git a/plugins/genshin/apps/mysNews.js b/plugins/genshin/apps/mysNews.js index bb5b262..60d79a7 100644 --- a/plugins/genshin/apps/mysNews.js +++ b/plugins/genshin/apps/mysNews.js @@ -74,143 +74,9 @@ export class mysNews extends plugin { async mysNewsTask() { let mysNews = new MysNews(this.e) - this.ActivityPush() + await mysNews.ActivityPush() await mysNews.mysNewsTask() } - async ActivityPush() { - let now = new Date() - now = now.getHours(); - if(now < 10) return - let pushGroupList - try { - pushGroupList = YAML.parse(fs.readFileSync(this.file, `utf8`)) - } catch (error) { - logger.error(`[米游社活动到期推送] 活动到期预警推送失败:无法获取配置文件信息\n${error}`) - return - } - if((!pushGroupList.gsActivityPush || pushGroupList.gsActivityPush == {}) && (!pushGroupList.srActivityPush || pushGroupList.srActivityPush == {})) return - let BotidList = [] - let ActivityPushYaml = {...pushGroupList.gsActivityPush, ...pushGroupList.srActivityPush} - for (let item in ActivityPushYaml) { - BotidList.push(item) - } - let gsActivityList = await this.getGsActivity() - let srActivityList = await this.getSrActivity() - let ActivityList = [] - for (let item of srActivityList) { - ActivityList.push({ game: `sr`, subtitle: item.title, banner: item.img, title: item.title, end_time: item.end_time }) - } - for (let item of gsActivityList) { - ActivityList.push({ game: 'gs', subtitle: item.subtitle, banner: item.banner, title: item.title, end_time: item.end_time}) - } - if(ActivityList.length === 0) return - for (let item of BotidList) { - let redisapgl = await redis.get(`Yz:apgl:${item}`) - let date = await this.getDate() - redisapgl = JSON.parse(redisapgl) - if(!redisapgl || redisapgl.date !== date) { - redisapgl = { - date, - GroupList: ActivityPushYaml[item] - } - } - if(!Array.isArray(redisapgl.GroupList) || redisapgl.GroupList.length == 0) continue - if(!Bot[item]) { - redisapgl.GroupList.shift() - await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) - continue - } - for (let a of ActivityList) { - if((!pushGroupList.srActivityPush || !pushGroupList.srActivityPush[item] || !pushGroupList.srActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `sr`) continue - if((!pushGroupList.gsActivityPush || !pushGroupList.gsActivityPush[item] || !pushGroupList.gsActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `gs`) continue - let pushGame - if(a.game === `sr`) pushGame = `星铁` - if(a.game === `gs`) pushGame = `原神` - let endDt = a.end_time - endDt = endDt.replace(/\s/, `T`) - let todayt = new Date() - endDt = new Date(endDt) - let sydate = await this.calculateRemainingTime(todayt, endDt) - let msgList = [ - `【${pushGame}活动即将结束通知】`, - `\n活动:${a.subtitle}`, - segment.image(a.banner), - `描述:${a.title}`, - `\n活动剩余时间:${sydate.days}天${sydate.hours}小时${sydate.minutes}分钟${sydate.seconds}秒`, - `\n活动结束时间:${a.end_time}` - ] - logger.mark(`[米游社活动到期推送] 开始推送 ${item}:${redisapgl.GroupList[0]} ${a.subtitle}`) - await common.sleep(5000) - Bot[item].pickGroup(redisapgl.GroupList[0]).sendMsg(msgList) - .then(() => {}).catch((err) => logger.error(`[米游社活动到期推送] ${item}:${redisapgl.GroupList[0]} 推送失败,错误信息${err}`)) - } - redisapgl.GroupList.shift() - await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) - } - } - async getDate() { - const currentDate = new Date(); - const year = currentDate.getFullYear(); - const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); - const day = currentDate.getDate().toString().padStart(2, '0'); - return `${year}-${month}-${day}` - } - async getGsActivity() { - let gshd - try { - gshd = await fetch(`https://hk4e-api.mihoyo.com/common/hk4e_cn/announcement/api/getAnnList?game=hk4e&game_biz=hk4e_cn&lang=zh-cn&bundle_id=hk4e_cn&platform=pc®ion=cn_gf01&level=55&uid=100000000`) - gshd = await gshd.json() - } catch { - return [] - } - let hdlist = [] - let result = [] - for (let item of gshd.data.list[1].list) { - if(item.tag_label.includes(`活动`) && !item.title.includes(`传说任务`) && !item.title.includes(`游戏公告`)) hdlist.push(item) - } - for (let item of hdlist) { - let endDt = item.end_time - endDt = endDt.replace(/\s/, `T`) - let todayt = new Date() - endDt = new Date(endDt) - let sydate = await this.calculateRemainingTime(todayt, endDt) - if(sydate.days <= 1) result.push(item) - } - return result - } - async getSrActivity() { - let srhd - try { - srhd = await fetch(`https://hkrpg-api.mihoyo.com/common/hkrpg_cn/announcement/api/getAnnList?game=hkrpg&game_biz=hkrpg_cn&lang=zh-cn&auth_appid=announcement&authkey_ver=1&bundle_id=hkrpg_cn&channel_id=1&level=65&platform=pc®ion=prod_gf_cn&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid=100000000`) - srhd = await srhd.json() - } catch { - return [] - } - let hdlist = [] - let result = [] - for (let item of srhd.data.pic_list[0].type_list[0].list) { - if (item.title) hdlist.push(item) - } - for (let item of hdlist) { - let endDt = item.end_time - endDt = endDt.replace(/\s/, `T`) - let todayt = new Date() - endDt = new Date(endDt) - let sydate = await this.calculateRemainingTime(todayt, endDt) - if (sydate.days <= 1) result.push(item) - } - return result - } - async calculateRemainingTime(startDate, endDate) { - const difference = endDate - startDate; - - const days = Math.floor(difference / (1000 * 60 * 60 * 24)); - const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); - const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); - const seconds = Math.floor((difference % (1000 * 60)) / 1000); - - return { days, hours, minutes, seconds }; - } async setActivityPush() { if (!this.e.isGroup) { await this.reply('推送请在群聊中设置') diff --git a/plugins/genshin/model/mysNews.js b/plugins/genshin/model/mysNews.js index 04320ea..b3d4e29 100644 --- a/plugins/genshin/model/mysNews.js +++ b/plugins/genshin/model/mysNews.js @@ -4,6 +4,8 @@ import lodash from 'lodash' import puppeteer from '../../../lib/puppeteer/puppeteer.js' import common from '../../../lib/common/common.js' import gsCfg from '../model/gsCfg.js' +import YAML from 'yaml' +import fs from 'fs' let emoticon @@ -324,6 +326,141 @@ export default class MysNews extends base { } } + async ActivityPush() { + let now = new Date() + now = now.getHours(); + if(now < 10) return + let pushGroupList + try { + pushGroupList = YAML.parse(fs.readFileSync(`./plugins/genshin/config/mys.pushNews.yaml`, `utf8`)) + } catch (error) { + logger.error(`[米游社活动到期推送] 活动到期预警推送失败:无法获取配置文件信息\n${error}`) + return + } + if((!pushGroupList.gsActivityPush || pushGroupList.gsActivityPush == {}) && (!pushGroupList.srActivityPush || pushGroupList.srActivityPush == {})) return + let BotidList = [] + let ActivityPushYaml = {...pushGroupList.gsActivityPush, ...pushGroupList.srActivityPush} + for (let item in ActivityPushYaml) { + BotidList.push(item) + } + let gsActivityList = await this.getGsActivity() + let srActivityList = await this.getSrActivity() + let ActivityList = [] + for (let item of srActivityList) { + ActivityList.push({ game: `sr`, subtitle: item.title, banner: item.img, title: item.title, end_time: item.end_time }) + } + for (let item of gsActivityList) { + ActivityList.push({ game: 'gs', subtitle: item.subtitle, banner: item.banner, title: item.title, end_time: item.end_time}) + } + if(ActivityList.length === 0) return + for (let item of BotidList) { + let redisapgl = await redis.get(`Yz:apgl:${item}`) + let date = await this.getDate() + redisapgl = JSON.parse(redisapgl) + if(!redisapgl || redisapgl.date !== date) { + redisapgl = { + date, + GroupList: ActivityPushYaml[item] + } + } + if(!Array.isArray(redisapgl.GroupList) || redisapgl.GroupList.length == 0) continue + if(!Bot[item]) { + redisapgl.GroupList.shift() + await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) + continue + } + for (let a of ActivityList) { + if((!pushGroupList.srActivityPush || !pushGroupList.srActivityPush[item] || !pushGroupList.srActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `sr`) continue + if((!pushGroupList.gsActivityPush || !pushGroupList.gsActivityPush[item] || !pushGroupList.gsActivityPush[item].includes(redisapgl.GroupList[0])) && a.game === `gs`) continue + let pushGame + if(a.game === `sr`) pushGame = `星铁` + if(a.game === `gs`) pushGame = `原神` + let endDt = a.end_time + endDt = endDt.replace(/\s/, `T`) + let todayt = new Date() + endDt = new Date(endDt) + let sydate = await this.calculateRemainingTime(todayt, endDt) + let msgList = [ + `【${pushGame}活动即将结束通知】`, + `\n活动:${a.subtitle}`, + segment.image(a.banner), + `描述:${a.title}`, + `\n活动剩余时间:${sydate.days}天${sydate.hours}小时${sydate.minutes}分钟${sydate.seconds}秒`, + `\n活动结束时间:${a.end_time}` + ] + logger.mark(`[米游社活动到期推送] 开始推送 ${item}:${redisapgl.GroupList[0]} ${a.subtitle}`) + await common.sleep(5000) + Bot[item].pickGroup(redisapgl.GroupList[0]).sendMsg(msgList) + .then(() => {}).catch((err) => logger.error(`[米游社活动到期推送] ${item}:${redisapgl.GroupList[0]} 推送失败,错误信息${err}`)) + } + redisapgl.GroupList.shift() + await redis.set(`Yz:apgl:${item}`, JSON.stringify(redisapgl)) + } + return + } + async getDate() { + const currentDate = new Date(); + const year = currentDate.getFullYear(); + const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); + const day = currentDate.getDate().toString().padStart(2, '0'); + return `${year}-${month}-${day}` + } + async getGsActivity() { + let gshd + try { + gshd = await fetch(`https://hk4e-api.mihoyo.com/common/hk4e_cn/announcement/api/getAnnList?game=hk4e&game_biz=hk4e_cn&lang=zh-cn&bundle_id=hk4e_cn&platform=pc®ion=cn_gf01&level=55&uid=100000000`) + gshd = await gshd.json() + } catch { + return [] + } + let hdlist = [] + let result = [] + for (let item of gshd.data.list[1].list) { + if(item.tag_label.includes(`活动`) && !item.title.includes(`传说任务`) && !item.title.includes(`游戏公告`)) hdlist.push(item) + } + for (let item of hdlist) { + let endDt = item.end_time + endDt = endDt.replace(/\s/, `T`) + let todayt = new Date() + endDt = new Date(endDt) + let sydate = await this.calculateRemainingTime(todayt, endDt) + if(sydate.days <= 1) result.push(item) + } + return result + } + async getSrActivity() { + let srhd + try { + srhd = await fetch(`https://hkrpg-api.mihoyo.com/common/hkrpg_cn/announcement/api/getAnnList?game=hkrpg&game_biz=hkrpg_cn&lang=zh-cn&auth_appid=announcement&authkey_ver=1&bundle_id=hkrpg_cn&channel_id=1&level=65&platform=pc®ion=prod_gf_cn&sdk_presentation_style=fullscreen&sdk_screen_transparent=true&sign_type=2&uid=100000000`) + srhd = await srhd.json() + } catch { + return [] + } + let hdlist = [] + let result = [] + for (let item of srhd.data.pic_list[0].type_list[0].list) { + if (item.title) hdlist.push(item) + } + for (let item of hdlist) { + let endDt = item.end_time + endDt = endDt.replace(/\s/, `T`) + let todayt = new Date() + endDt = new Date(endDt) + let sydate = await this.calculateRemainingTime(todayt, endDt) + if (sydate.days <= 1) result.push(item) + } + return result + } + async calculateRemainingTime(startDate, endDate) { + const difference = endDate - startDate; + + const days = Math.floor(difference / (1000 * 60 * 60 * 24)); + const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((difference % (1000 * 60)) / 1000); + + return { days, hours, minutes, seconds }; + } async sendNews(botId, groupId, typeName, postId, gid) { if (!this.pushGroup[groupId]) this.pushGroup[groupId] = 0 if (this.pushGroup[groupId] >= this.maxNum) return