UNPKG

koishi-plugin-freegames-subscribe

Version:

【订阅功能需要自建Rsshub,没有只能手动查询!】订阅Epic喜+1、PS+每月会免、XGP入库提醒,任何有关游戏的news!

217 lines (190 loc) 8.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apply = exports.Config = exports.name = void 0; const koishi_1 = require("koishi"); exports.name = 'freegames-subscribe'; const axios = require('axios'); exports.Config = koishi_1.Schema.intersect([ koishi_1.Schema.object({ steaminfo: koishi_1.Schema.boolean().description('是否与Steaminfo插件展开联动?你可以使“epic”命令测试插件是否有效。注:使用本插件时可能需要科学上网环境,你可以访问以下链接测试https://rsshub.app/epicgames/freegames/zh-CN.json').default(false), }).description('基础设置'), koishi_1.Schema.object({ epic_subscribe: koishi_1.Schema.boolean().description('启用EPIC喜+1上新订阅?当内容更新时,会自动推送至指定群组。请使用无缓存的自建的rsshub地址!').default(false), debug_mode: koishi_1.Schema.boolean().description('开启后若rsshub无响应则会报错。').default(false), epic_rss: koishi_1.Schema.string().description('epic的rss地址'), news_subscribe: koishi_1.Schema.boolean().description('启用自定义新闻订阅?注:启用新闻订阅需要填写筛选关键词!当内容更新时,会自动推送至指定群组。请使用无缓存的自建的rsshub地址!').default(false), news_rss: koishi_1.Schema.string().description('二柄的rss地址'), keywords: koishi_1.Schema.array(String).description('筛选订阅的关键词,订阅的原理是从新闻条目中筛选包含指定关键词的条目,你可以查询“二柄APP”中想要订阅的关键词,例如添加“会免游戏公布”以订阅PS+每月会免;添加“旬XGP/PGP新增”以订阅XGP入库通知。'), authors: koishi_1.Schema.array(String).description('筛选你想订阅的作者,例如“涟漪”'), botplatform: koishi_1.Schema.string().description('平台名称。').required(), guildId: koishi_1.Schema.array(String).description('群组 ID。'), selfId: koishi_1.Schema.string().description('机器人 ID。').required(), }).description('订阅设置'), koishi_1.Schema.object({ interval: koishi_1.Schema.number().default(3600000).description('轮询间隔 (毫秒)。如果不了解它们的含义,请使用默认值!'), filter_time: koishi_1.Schema.number().default(3600000).description('过滤间隔,和轮询间隔填相同数值'), }).description('高级设置'), ]) const axiosRetry = require('axios-retry'); // Enable axios-retry axiosRetry(axios, { retries: 3 }); async function getmessage(url,config) { try { const response = await axios.get(url, { headers: { 'Accept-Language': 'zh-CN,zh;q=0.9', }, cache: 'no-cache' }); const data = response.data; const items = data.items; if (items.length === 0) { return; } else { let message = ''; items.forEach(item => { const currentTime = new Date(); const currentTimestamp = currentTime.getTime(); const datePublished = new Date(item.date_published); const datePublishedTimestamp = datePublished.getTime(); const timeDifference = currentTimestamp - datePublishedTimestamp; if (timeDifference < config.filter_time) { message += `游戏名称:${item.title}\n`; message += `介绍:${item.content_html.replace(/<img[^>]*>/g, '')}\n`; message += `领取链接:${item.url}\n`; if(config.steaminfo){ message += `\n使用以下指令获取详细信息:查询 ${item.title.replace(/[^a-zA-Z0-9]/g, ' ')}\n`; } // 提取 description 中的图片链接 const imageUrl = extractImageUrl(item.content_html); if (imageUrl) { // 使用 koishi_1.segment.image 方法拼接图片 message += koishi_1.segment.image(imageUrl); } message += '\n'; } }); return message; } } catch (error) { // 处理错误 console.error(error); if (config.debug_mode){ return '请求出现错误,无法获取数据,请检查bot所处网络环境是否可以访问rsshub'; } } } async function getnews(url, config) { try { const response = await axios.get(url, { headers: { 'Accept-Language': 'zh-CN,zh;q=0.9' }, cache: 'no-cache' }); const data = response.data; const items = data.items; if (items.length === 0) { return; } else { let message = ''; items.forEach(item => { const currentTime = new Date(); const currentTimestamp = currentTime.getTime(); const datePublished = new Date(item.date_published); const datePublishedTimestamp = datePublished.getTime(); const timeDifference = currentTimestamp - datePublishedTimestamp; if ((config.authors.length === 0 || config.authors.some(author => item.authors[0].name.includes(author))) && (config.keywords.length === 0 || config.keywords.some(keyword => item.content_html.includes(keyword))) && timeDifference < config.filter_time) { message += `订阅提醒:${item.content_html.replace(/<img[^>]*>/g, '').replace(/<br\/?>/g, '\n')}\n`; message += `作者:${item.authors[0].name}\n`; // 提取 description 中的图片链接 const imageUrl = extractImageUrl(item.content_html); if (imageUrl) { // 使用 koishi_1.segment.image 方法拼接图片 message += koishi_1.segment.image(imageUrl); } message += '\n'; } }); return message; } } catch (error) { // 处理错误 console.error(error); if (config.debug_mode){ return '请求出现错误,无法获取数据,请检查bot所处网络环境是否可以访问rsshub'; } } } function apply(ctx, config) { const currentDate = new Date(); const currentHour = currentDate.getHours(); const currentMinute = currentDate.getMinutes(); ctx.on('ready', async () => { ctx.setInterval(async () => { const message = await getmessage(`${config.epic_rss}`,config); const news = await getnews(`${config.news_rss}`,config); const bot = ctx.bots[`${config.botplatform}:${config.selfId}`]; if(message && config.epic_subscribe){ config.guildId.forEach((guildId, index) => { setTimeout(() => { bot.sendMessage(`${guildId}`, `Epic喜+1上新!\n\n${message}`) }, index * 1000) }); } if(news && config.news_subscribe){ config.guildId.forEach((guildId, index) => { setTimeout(() => { bot.sendMessage(`${guildId}`, `${news}`) }, index * 1000) }); } }, config.interval); }); ctx.command('epic', "查询epic免费游戏") .action(async ({ session, options }) => { try { const response = await axios.get(`https://rsshub.app/epicgames/freegames/zh-CN.json`, { headers: { 'Accept-Language': 'zh-CN,zh;q=0.9', }, cache: 'no-cache' }); const data = response.data; const items = data.items; let message = ''; const now = new Date(); const timeStr = now.toLocaleString(); message += `\n当前时间:${timeStr}\n`; items.forEach(item => { message += `游戏名称:${item.title}\n`; message += `介绍:${item.content_html.replace(/<img[^>]*>/g, '')}\n`; message += `领取链接:${item.url}\n`; if(config.steaminfo){ message += `\n使用以下指令获取详细信息:查询 ${item.title.replace(/[^a-zA-Z0-9]/g, ' ')}\n`; } // 提取 description 中的图片链接 const imageUrl = extractImageUrl(item.content_html); if (imageUrl) { // 使用 koishi_1.segment.image 方法拼接图片 message += koishi_1.segment.image(imageUrl); } message += '\n'; }); session.send(message); } catch (error) { console.error(error); if (config.debug_mode){ session.send('出错了!'); } } }); } function extractImageUrl(description) { // 此处假设图片链接位于 description 中的第一个 img 标签中 const imgTag = description.match(/<img[^>]*>/g)[0]; const srcAttr = imgTag.match(/src="[^"]*"/g)[0]; return srcAttr.slice(5, -1); } exports.apply = apply;