UNPKG

koishi-plugin-kbot

Version:
105 lines (104 loc) 4.62 kB
"use strict"; /* * @Author: Kabuda-czh * @Date: 2023-01-29 14:43:47 * @LastEditors: Kabuda-czh * @LastEditTime: 2023-07-17 14:54:56 * @FilePath: \KBot-App\plugins\kbot\src\plugins\bilibili\dynamic\index.ts * @Description: * * Copyright (c) 2023 by Kabuda-czh, All Rights Reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.apply = exports.logger = exports.Config = void 0; const koishi_1 = require("koishi"); const utils_1 = require("../utils"); const dynamic_strategy_1 = require("./dynamic.strategy"); const listen_1 = require("./listen"); exports.Config = koishi_1.Schema.object({ interval: koishi_1.Schema.number() .description('请求之间的间隔 (秒) 注: 最低 10 秒!') .default(10) .min(10), device: koishi_1.Schema.union([ koishi_1.Schema.const('pc').description('电脑'), koishi_1.Schema.const('mobile').description('手机'), ]) .default('pc') .description('截图类型 (手机/电脑), 需要开启图片模式'), live: koishi_1.Schema.boolean().description('是否监控开始直播的动态').default(true), useImage: koishi_1.Schema.boolean().default(false).description('是否使用图片模式 (需要 puppeteer 支持!)'), authority: koishi_1.Schema.number() .default(2) .min(1) .description('设定指令的最低权限, 默认 2 级'), }); exports.logger = new koishi_1.Logger('KBot-bilibili-dynamic'); async function apply(ctx, config) { const channels = await ctx.database.get('channel', {}, [ 'id', 'guildId', 'platform', 'bilibili', ]); const list = channels .filter(channel => channel.bilibili.dynamic) .reduce((acc, x) => { x.bilibili.dynamic.forEach((notification) => { var _a; (acc[_a = notification.bilibiliId] || (acc[_a] = [])).push([x, notification]); }); return acc; }, {}); ctx .guild() .command('kbot/bilibili', 'b站相关') .channelFields(['id', 'guildId', 'platform', 'bilibili']) .before(checkDynamic) .usage(`最低权限: ${config.authority} 级`) .option('add', '-a <upInfo:string> 添加订阅, 请输入要添加的 up 主的 uid 或者 名字', { authority: config.authority, }) .option('batch', '-b [...upInfo:string] 批量添加订阅, 请输入要添加的 up 主的 uid 或者 名字, 以逗号分隔', { authority: config.authority, }) .option('remove', '-r <upInfo:string> 移除订阅, 请输入要移除的 up 主的 uid 或者 名字', { authority: config.authority, }) .option('search', '-s <upInfo:string> 查看最新动态, 请输入要查看动态的 up 主的 uid 或者 名字', { authority: config.authority }) .option('list', '-l 展示当前订阅 up 主列表', { authority: config.authority }) .option('vup', '-v <upInfo:string> 查成分, 请输入要查看成分的 up 主的 uid 或者 名字', { authority: config.authority }) .option('danmu', '-d <upInfo:string> 查弹幕, 请输入要查看弹幕的 up 主的 uid 或者 名字', { authority: config.authority }) .option('refresh', '--re 更新 vup', { authority: config.authority }) .option('cookie', '--ck <cookie:string> 更新 cookie', { authority: config.authority, }) .example('使用方法: bilibili -a 123456 或者 bilibili -a 名字') .action(async ({ session, options }) => { if (Object.keys(options).length > 1) return '请不要同时使用多个参数'; return (0, dynamic_strategy_1.dynamicStrategy)({ session, options }, list, ctx, config); }); const generator = (0, listen_1.listen)(list, request, ctx, config); ctx.setInterval(async () => { await generator.next(); }, config.interval * 1000); } exports.apply = apply; function checkDynamic({ session }) { var _a; (_a = session.channel.bilibili).dynamic || (_a.dynamic = []); } async function request(ctx, uid) { try { const res = await (0, utils_1.getDynamic)(ctx, uid, exports.logger); if (res.code !== 0) throw new Error(res.message + (res.code === -352 ? ' (可能为 cookie 失效)' : '')); return res.data.items.sort((a, b) => b.modules.module_author.pub_ts - a.modules.module_author.pub_ts); } catch (e) { if (['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED', 'ECONNABORTED', 'read ECONNRESET'].includes(e.code)) return []; throw e.message; } }