diff --git a/src/bot.ts b/src/bot.ts index 5c89764..e7a2402 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -17,12 +17,21 @@ import ListenerLoader from './lib/listener/loader.js' */ import { Client } from 'icqq' export class Yunzai extends Client { - // eslint-disable-next-line no-useless-constructor + /** + * + * @param conf + */ constructor(conf) { + /** + * + */ super(conf) } - /** 登录机器人 */ + /** + * 登录机器人 + * @returns + */ static async run() { const bot = new Yunzai(cfg.bot) /** 加载监听事件 */ @@ -40,7 +49,11 @@ export class Yunzai extends Client { return bot } - /** 跳过登录ICQQ */ + /** + * 跳过登录ICQQ + * @param bot + * @returns + */ static async skip_login(bot) { bot.uin = 88888 bot[bot.uin] = bot diff --git a/src/core/functional.ts b/src/core/functional.ts index 0d48261..97e2da2 100644 --- a/src/core/functional.ts +++ b/src/core/functional.ts @@ -1,7 +1,9 @@ import { MessageCallBackType } from './types.js' import { plugin } from './plugin.js' -// 插件super默认值 +/** + * 插件super默认值 + */ export const PluginSuperDefine = { name: 'group-app', dsc: 'group-dsc', @@ -9,13 +11,21 @@ export const PluginSuperDefine = { priority: 9999 } -// 消息 +/** + * 消息 + */ export class Messages { count = 0 rule: { reg: RegExp fnc: string }[] = [] + + /** + * + * @param reg + * @param fnc + */ response(reg: RegExp, fnc: MessageCallBackType) { this.count++ const propName = `prop_${this.count}` @@ -25,8 +35,10 @@ export class Messages { fnc: propName }) } + /** + * + */ get ok() { - // eslint-disable-next-line @typescript-eslint/no-this-alias const App = this class Children extends plugin { constructor() { @@ -49,14 +61,30 @@ export class Messages { * 事件 */ export class Events { + /** + * + */ count = 0 + + /** + * + */ data: { [key: string]: typeof plugin } = {} + + /** + * + * @param val + */ use(val: typeof plugin) { this.count++ this.data[this.count] = val } + + /** + * + */ get ok() { return this.data } diff --git a/src/core/local.js b/src/core/local.js deleted file mode 100644 index b7100fa..0000000 --- a/src/core/local.js +++ /dev/null @@ -1 +0,0 @@ -export * from '#miao' diff --git a/src/core/plugin.ts b/src/core/plugin.ts index 81793ea..ea2e530 100644 --- a/src/core/plugin.ts +++ b/src/core/plugin.ts @@ -1,4 +1,4 @@ -import { Common } from './local.js' +import { Common } from '../local.js' import { EventType } from './types.js' const stateArr = {} diff --git a/src/core/runtime.ts b/src/core/runtime.ts index 8fa1658..3b82cc0 100644 --- a/src/core/runtime.ts +++ b/src/core/runtime.ts @@ -22,10 +22,13 @@ export default class Runtime { _mysInfo = null handler = null + /** + * + * @param e + */ constructor(e) { this.e = e this._mysInfo = {} - this.handler = { has: Handler.has, call: Handler.call, @@ -33,26 +36,44 @@ export default class Runtime { } } + /** + * + */ get uid() { return this.user?.uid } + /** + * + */ get hasCk() { return this.user?.hasCk } + /** + * + */ get user() { return this.e.user } + /** + * + */ get cfg() { return cfg } + /** + * + */ get gsCfg() { return gsCfg } + /** + * + */ get common() { return common } @@ -64,14 +85,23 @@ export default class Runtime { return null } + /** + * + */ get MysInfo() { return MysInfo } + /** + * + */ get NoteUser() { return NoteUser } + /** + * + */ get MysUser() { return MysUser } diff --git a/src/core/types.ts b/src/core/types.ts index c624c13..bc737ce 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -1,18 +1,34 @@ import { type GroupMessage } from 'icqq' -// 机器人事件类型 +/** + * 机器人事件类型 + */ export interface EventType extends GroupMessage { + /** + * 是否是主人 + */ isMaster: boolean + /** + * 群聊 + */ group: { - // eslint-disable-next-line @typescript-eslint/no-explicit-any recallMsg: (...arg) => any } + /** + * 用户消息 + */ msg: string - // eslint-disable-next-line @typescript-eslint/no-explicit-any + /** + * 消息发送 + * @param arg + * @returns + */ reply: (...arg) => Promise } -// 函数式回调类型 +/** + * 函数式回调类型 + */ export type MessageCallBackType = ( e: EventType ) => Promise diff --git a/src/db/BaseModel.ts b/src/db/BaseModel.ts index 557c698..0a61221 100644 --- a/src/db/BaseModel.ts +++ b/src/db/BaseModel.ts @@ -1,18 +1,29 @@ import { Sequelize, DataTypes, Model } from 'sequelize' -import { Data } from './local.js' +import { Data } from '../local.js' +import { join } from 'path' +/** + * 创建路径 + */ Data.createDir('/data/db', 'root') -let dbPath = process.cwd() + '/data/db/data.db' -// TODO DB自定义 +/** + * DB自定义 + */ const sequelize = new Sequelize({ dialect: 'sqlite', - storage: dbPath, + storage: join(process.cwd(), '/data/db/data.db'), logging: false }) +/** + * 校验连接 + */ await sequelize.authenticate() +/** + * + */ export default class BaseModel extends Model { static Types = DataTypes @@ -23,4 +34,8 @@ export default class BaseModel extends Model { model.COLUMNS = columns } } + +/** + * + */ export { sequelize } diff --git a/src/db/MysUserDB.ts b/src/db/MysUserDB.ts index 43b14cd..7293d6d 100644 --- a/src/db/MysUserDB.ts +++ b/src/db/MysUserDB.ts @@ -2,6 +2,9 @@ import BaseModel from './BaseModel.js' const { Types } = BaseModel +/** + * + */ const COLUMNS = { // 用户ID,qq为数字 ltuid: { @@ -37,6 +40,9 @@ const COLUMNS = { } } +/** + * + */ class MysUserDB extends BaseModel { static async find(ltuid = '', create = false) { // DB查询 @@ -62,7 +68,17 @@ class MysUserDB extends BaseModel { } } +/** + * + */ BaseModel.initDB(MysUserDB, COLUMNS) + +/** + * + */ await MysUserDB.sync() +/** + * + */ export default MysUserDB diff --git a/src/db/UserDB.ts b/src/db/UserDB.ts index 7b20321..49d2603 100644 --- a/src/db/UserDB.ts +++ b/src/db/UserDB.ts @@ -2,8 +2,14 @@ import BaseModel from './BaseModel.js' import lodash from 'lodash' import MysUtil from '../mys/MysUtil.js' +/** + * + */ const { Types } = BaseModel +/** + * + */ const COLUMNS = { // 用户ID,qq为数字 id: { @@ -51,7 +57,16 @@ const COLUMNS = { data: Types.STRING } +/** + * + */ class UserDB extends BaseModel { + /** + * + * @param id + * @param type + * @returns + */ static async find(id, type = 'qq') { // user_id id = type === 'qq' ? '' + id : type + id @@ -66,6 +81,10 @@ class UserDB extends BaseModel { return user } + /** + * + * @param user + */ async saveDB(user) { let db = this let ltuids = [] @@ -93,7 +112,17 @@ class UserDB extends BaseModel { } } +/** + * + */ BaseModel.initDB(UserDB, COLUMNS) + +/** + * + */ await UserDB.sync() +/** + * + */ export default UserDB diff --git a/src/db/UserGameDB.ts b/src/db/UserGameDB.ts index fac8a98..9f7e700 100644 --- a/src/db/UserGameDB.ts +++ b/src/db/UserGameDB.ts @@ -1,8 +1,14 @@ import BaseModel from './BaseModel.js' import lodash from 'lodash' +/** + * + */ const { Types } = BaseModel +/** + * + */ const COLUMNS = { // 用户ID,qq为数字 userId: { @@ -33,9 +39,22 @@ const COLUMNS = { } } +/** + * + */ class UserGameDB extends BaseModel {} +/** + * + */ BaseModel.initDB(UserGameDB, COLUMNS) + +/** + * + */ await UserGameDB.sync() +/** + * + */ export default UserGameDB diff --git a/src/db/local.js b/src/db/local.js deleted file mode 100644 index b7100fa..0000000 --- a/src/db/local.js +++ /dev/null @@ -1 +0,0 @@ -export * from '#miao' diff --git a/src/lib/common/common.ts b/src/lib/common/common.ts index a61bbae..05fcaca 100644 --- a/src/lib/common/common.ts +++ b/src/lib/common/common.ts @@ -50,6 +50,11 @@ async function downFile (fileUrl, savePath, param = {}) { } } +/** + * + * @param dirname + * @returns + */ function mkdirs (dirname) { if (fs.existsSync(dirname)) { return true @@ -129,4 +134,7 @@ async function makeForwardMsg (e, msg = [], dec = '', msgsscr = false) { return forwardMsg } +/** + * + */ export default { sleep, relpyPrivate, downFile, makeForwardMsg } diff --git a/src/lib/config/check.ts b/src/lib/config/check.ts index 49931d6..074bc7a 100644 --- a/src/lib/config/check.ts +++ b/src/lib/config/check.ts @@ -1,9 +1,11 @@ import fs from 'fs' -import { createRequire } from 'module' -const require = createRequire(import.meta.url) -const { exec } = require('child_process') +import { exec } from 'child_process' -export async function checkRun () { +/** + * + * @returns + */ +export async function checkRun() { if (process.argv[1].includes('pm2')) return if (process.argv[1].includes('test')) return @@ -17,7 +19,12 @@ export async function checkRun () { } } -async function execSync (cmd) { +/** + * + * @param cmd + * @returns + */ +async function execSync(cmd) { return new Promise((resolve, reject) => { exec(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }) @@ -25,7 +32,11 @@ async function execSync (cmd) { }) } -function pm2Cfg () { +/** + * + * @returns + */ +function pm2Cfg() { let cfg = fs.readFileSync('./config/pm2/pm2.json') cfg = JSON.parse(cfg) return cfg diff --git a/src/lib/config/config.ts b/src/lib/config/config.ts index 3a2ea1f..a954606 100644 --- a/src/lib/config/config.ts +++ b/src/lib/config/config.ts @@ -2,18 +2,25 @@ import YAML from 'yaml' import fs from 'node:fs' import chokidar from 'chokidar' -/** 配置文件 */ +/** + * ******** + * 配置文件 + * ******** + */ class Cfg { + config = {} + + /** 监听文件 */ + watcher = { config: {}, defSet: {} } + constructor () { - this.config = {} - - /** 监听文件 */ - this.watcher = { config: {}, defSet: {} } - this.initCfg() } - /** 初始化配置 */ + + /** + * 初始化配置 + */ initCfg () { let path = 'config/config/' let pathDef = 'config/default_config/' @@ -27,17 +34,23 @@ class Cfg { if (!fs.existsSync("resources")) fs.mkdirSync("resources") } - /** 机器人qq号 */ + /** + * 机器人qq号 + */ get qq () { return Number(this.getConfig('qq').qq) } - /** 密码 */ + /** + * 密码 + */ get pwd () { return this.getConfig('qq').pwd } - /** icqq配置 */ + /** + * icqq配置 + */ get bot () { let bot = this.getConfig('bot') let defbot = this.getdefSet('bot') @@ -52,23 +65,37 @@ class Cfg { return bot } + /** + * + */ get other () { return this.getConfig('other') } + /** + * + */ get redis () { return this.getConfig('redis') } + /** + * + */ get renderer() { return this.getConfig('renderer'); } + /** + * + */ get notice() { return this.getConfig('notice'); } - /** 主人qq */ + /** + * 主人qq + */ get masterQQ () { let masterQQ = this.getConfig('other').masterQQ || [] @@ -80,7 +107,9 @@ class Cfg { return masterQQ } - /** package.json */ + /** + * package.json + */ get package () { if (this._package) return this._package @@ -88,7 +117,11 @@ class Cfg { return this._package } - /** 群配置 */ + /** + * 群配置 + * @param groupId + * @returns + */ getGroup (groupId = '') { let config = this.getConfig('group') let defCfg = this.getdefSet('group') @@ -98,14 +131,20 @@ class Cfg { return { ...defCfg.default, ...config.default } } - /** other配置 */ + /** + * other配置 + * @returns + */ getOther () { let def = this.getdefSet('other') let config = this.getConfig('other') return { ...def, ...config } } - /** notice配置 */ + /** + * notice配置 + * @returns + */ getNotice () { let def = this.getdefSet('notice') let config = this.getConfig('notice') @@ -113,14 +152,19 @@ class Cfg { } /** - * @param app 功能 + * * @param name 配置文件名称 + * @returns */ getdefSet (name) { return this.getYaml('default_config', name) } - /** 用户配置 */ + /** + * 用户配置 + * @param name + * @returns + */ getConfig (name) { return this.getYaml('config', name) } @@ -144,7 +188,13 @@ class Cfg { return this.config[key] } - /** 监听配置文件 */ + /** + * 监听配置文件 + * @param file + * @param name + * @param type + * @returns + */ watch (file, name, type = 'default_config') { let key = `${type}.${name}` @@ -163,16 +213,30 @@ class Cfg { this.watcher[key] = watcher } + /** + * + * @returns + */ change_qq () { if (process.argv.includes('login') || !this.qq) return logger.info('修改机器人QQ或密码,请手动重启') } + /** + * + */ async change_bot () { /** 修改日志等级 */ let log = await import('./log.js') log.default() } + + } +/** + * ********** + * + * *** + */ export default new Cfg() diff --git a/src/lib/config/init.ts b/src/lib/config/init.ts index d10fb2f..a445834 100644 --- a/src/lib/config/init.ts +++ b/src/lib/config/init.ts @@ -8,6 +8,9 @@ import yaml from "yaml" /** 设置标题 */ process.title = "Miao-Yunzai" +/** + * + */ async function UpdateTitle() { // 添加一些多余的标题内容 let title = "Miao-Yunzai" @@ -70,7 +73,9 @@ process.on("exit", async code => { await checkInit() -/** 初始化事件 */ +/** + * 初始化事件 + */ async function checkInit() { /** 检查node_modules */ if (!fs.existsSync("./node_modules") || !fs.existsSync("./node_modules/icqq")) { diff --git a/src/lib/config/log.ts b/src/lib/config/log.ts index 6d4833e..91a500b 100644 --- a/src/lib/config/log.ts +++ b/src/lib/config/log.ts @@ -87,6 +87,9 @@ export default function setLog () { logColor() } +/** + * + */ function logColor () { logger.chalk = chalk logger.red = chalk.red diff --git a/src/lib/config/qq.ts b/src/lib/config/qq.ts index 2e5621f..2e7cbb4 100644 --- a/src/lib/config/qq.ts +++ b/src/lib/config/qq.ts @@ -11,10 +11,24 @@ import chalk from 'chalk' export default async function createQQ () { /** 跳过登录ICQQ */ if(cfg.bot.skip_login) return + + /** + * + */ if (cfg.qq && !process.argv.includes('login')) { return } + + + /** + * + */ console.log(`欢迎使用${chalk.green('Miao-Yunzai v' + cfg.package.version)}\n请按提示输入完成QQ配置`) + + + /** + * + */ let propmtList = [ { type: 'Input', @@ -56,6 +70,9 @@ export default async function createQQ () { // } ] + /** + * + */ if (!process.argv.includes('login')) { propmtList.push({ type: 'Input', @@ -63,13 +80,24 @@ export default async function createQQ () { name: 'masterQQ' }) } + + /** + * + */ propmtList.push({ type: 'input', message: '请输入签名API地址(可留空):', name: 'signAPI' }) + + /** + * + */ const ret = await inquirer.prompt(propmtList) + /** + * + */ let file = './config/config/' let fileDef = './config/default_config/' @@ -83,12 +111,18 @@ export default async function createQQ () { let bot = fs.readFileSync(`${fileDef}bot.yaml`, 'utf8') // bot = bot.replace(/proxyAddress:/g, `proxyAddress: ${ret.proxyAddress}`) + /** + * + */ if (ret.masterQQ) { let other = fs.readFileSync(`${fileDef}other.yaml`, 'utf8') other = other.replace(/masterQQ:/g, `masterQQ:\n - ${ret.masterQQ}`) fs.writeFileSync(`${file}other.yaml`, other, 'utf8') } + /** + * + */ if (ret.signAPI) { bot = bot.replace(/sign_api_addr:/g, `sign_api_addr: ${ret.signAPI}`) } @@ -97,5 +131,8 @@ export default async function createQQ () { console.log(`\nQQ配置完成,正在登录\n后续修改账号可以运行命令: ${chalk.green('node app login')}\n`) + /** + * + */ await common.sleep(2000) } diff --git a/src/lib/config/redis.ts b/src/lib/config/redis.ts index e4b84c0..0ddb13f 100644 --- a/src/lib/config/redis.ts +++ b/src/lib/config/redis.ts @@ -3,8 +3,10 @@ import common from "../common/common.js" import { createClient } from "redis" import { exec } from "node:child_process" + /** * 初始化全局redis客户端 + * @returns */ export default async function redisInit() { const rc = cfg.redis @@ -49,6 +51,10 @@ export default async function redisInit() { return client } +/** + * + * @returns + */ async function aarch64() { if (process.platform == "win32") return "" @@ -67,6 +73,11 @@ async function aarch64() { return "" } +/** + * + * @param cmd + * @returns + */ function execSync (cmd) { return new Promise((resolve, reject) => { exec(cmd, (error, stdout, stderr) => { diff --git a/src/lib/events/login.ts b/src/lib/events/login.ts index db93a63..db45834 100644 --- a/src/lib/events/login.ts +++ b/src/lib/events/login.ts @@ -8,8 +8,20 @@ import fetch from 'node-fetch' * 监听上线事件 */ let inSlider = false + +/** + * + */ export default class loginEvent extends EventListener { + + /** + * + */ constructor () { + + /** + * + */ super({ prefix: 'system.login.', event: ['qrcode', 'slider', 'device', 'error'], @@ -17,10 +29,16 @@ export default class loginEvent extends EventListener { }) } - async execute (event) { - } + /** + * + * @param event + */ + async execute (event) {} - /** 扫码登录现在仅能在同一ip下进行 */ + /** + * 扫码登录现在仅能在同一ip下进行 + * @param event + */ async qrcode (event) { logger.mark(`请使用登录当前QQ的手机${logger.green('扫码')}完成登录,如果显示二维码过期,可以按${logger.green('回车键(Enter)')}刷新,重新输入密码请执行命令:${logger.green('node app login')}`) // logger.info('等待扫码中...') @@ -57,8 +75,10 @@ export default class loginEvent extends EventListener { }) } + /** * 收到滑动验证码提示后,必须使用手机拉动,PC浏览器已经无效 + * @param event */ async slider (event) { inSlider = true @@ -109,6 +129,11 @@ export default class loginEvent extends EventListener { this.client.submitSlider(ticket.trim()) } + /** + * + * @param url + * @returns + */ async getTicket (url) { let req = `https://hlhs-nb.cn/captcha/slider?key=${Bot.uin}` await fetch(req, { @@ -130,6 +155,11 @@ export default class loginEvent extends EventListener { } } + /** + * + * @param url + * @returns + */ async requestCode (url) { let txhelper = { url: url.replace('ssl.captcha.qq.com', 'txhelper.glitch.me') @@ -167,7 +197,11 @@ export default class loginEvent extends EventListener { return lodash.trim(txhelper.res) } - /** 设备锁 */ + + /** + * 设备锁 + * @param event + */ async device (event) { global.inputTicket = false console.log(`\n\n------------------${logger.green('↓↓设备锁验证↓↓')}----------------------\n`) @@ -197,7 +231,10 @@ export default class loginEvent extends EventListener { } } - /** 登录错误 */ + /** + * 登录错误 + * @param event + */ error (event) { if (Number(event.code) === 1) logger.error('QQ密码错误,运行命令重新登录:node app login') if (global.inputTicket && event.code == 237) { @@ -207,7 +244,6 @@ export default class loginEvent extends EventListener { } else { logger.error('登录错误,已停止运行') } - process.exit() } } diff --git a/src/lib/events/message.ts b/src/lib/events/message.ts index edcbcbb..48be8e6 100644 --- a/src/lib/events/message.ts +++ b/src/lib/events/message.ts @@ -1,13 +1,27 @@ import EventListener from '../listener/listener.js' + /** * 监听群聊消息 */ export default class messageEvent extends EventListener { + + + /** + * + */ constructor () { + + /** + * + */ super({ event: 'message' }) } + /** + * + * @param e + */ async execute (e) { this.plugins.deal(e) } diff --git a/src/lib/events/notice.ts b/src/lib/events/notice.ts index 48bc6db..36abd89 100644 --- a/src/lib/events/notice.ts +++ b/src/lib/events/notice.ts @@ -4,10 +4,22 @@ import EventListener from '../listener/listener.js' * 监听群聊消息 */ export default class noticeEvent extends EventListener { + + /** + * + */ constructor () { + + /** + * + */ super({ event: 'notice' }) } + /** + * + * @param e + */ async execute (e) { this.plugins.deal(e) } diff --git a/src/lib/events/offline.ts b/src/lib/events/offline.ts index d6c5f38..10d1ed3 100644 --- a/src/lib/events/offline.ts +++ b/src/lib/events/offline.ts @@ -6,11 +6,22 @@ import cfg from '../config/config.js' * 监听下线事件 */ export default class offlineEvent extends EventListener { + + /** + * + */ constructor () { + + /** + * + */ super({ event: 'system.offline' }) } - /** 默认方法 */ + /** + * 默认方法 + * @param e + */ async execute (e) { logger.mark('掉线了') let config = cfg.getConfig('notice') diff --git a/src/lib/events/online.ts b/src/lib/events/online.ts index 8d2533c..e4ffcd8 100644 --- a/src/lib/events/online.ts +++ b/src/lib/events/online.ts @@ -6,14 +6,25 @@ import common from '../common/common.js' * 监听上线事件 */ export default class onlineEvent extends EventListener { + + /** + * + */ constructor () { + + /** + * + */ super({ event: 'system.online', once: true }) } - /** 默认方法 */ + /** + * 默认方法 + * @param e + */ async execute (e) { logger.mark('----^_^----') logger.mark(logger.green(`Miao-Yunzai 上线成功 版本v${cfg.package.version}`)) @@ -26,6 +37,10 @@ export default class onlineEvent extends EventListener { this.loginMsg() } + /** + * + * @returns + */ async loginMsg () { if (!cfg.bot.online_msg) return if (!cfg.masterQQ || !cfg.masterQQ[0]) return diff --git a/src/lib/events/request.ts b/src/lib/events/request.ts index 335f145..29717aa 100644 --- a/src/lib/events/request.ts +++ b/src/lib/events/request.ts @@ -4,10 +4,22 @@ import EventListener from '../listener/listener.js' * 监听群聊消息 */ export default class requestEvent extends EventListener { + + /** + * + */ constructor () { + + /** + * + */ super({ event: 'request' }) } + /** + * + * @param e + */ async execute (e) { this.plugins.deal(e) } diff --git a/src/lib/listener/listener.ts b/src/lib/listener/listener.ts index f88b6f2..e2503f6 100644 --- a/src/lib/listener/listener.ts +++ b/src/lib/listener/listener.ts @@ -1,5 +1,8 @@ import PluginsLoader from '../plugins/loader.js' +/** + * + */ export default class EventListener { prefix = '' event = null diff --git a/src/lib/listener/loader.ts b/src/lib/listener/loader.ts index 0fdc074..89173de 100644 --- a/src/lib/listener/loader.ts +++ b/src/lib/listener/loader.ts @@ -41,4 +41,7 @@ class ListenerLoader { } } +/** + * + */ export default new ListenerLoader() diff --git a/src/lib/plugins/handler.ts b/src/lib/plugins/handler.ts index a96ebad..ce71c4e 100644 --- a/src/lib/plugins/handler.ts +++ b/src/lib/plugins/handler.ts @@ -1,8 +1,20 @@ import util from 'node:util' import lodash from 'lodash' -let events = {} -let Handler = { +/** + * + */ +const events = {} + +/** + * + */ +const Handler = { + /** + * + * @param cfg + * @returns + */ add (cfg) { let { ns, fn, self, property = 50 } = cfg let key = cfg.key || cfg.event @@ -21,6 +33,12 @@ let Handler = { }) events[key] = lodash.orderBy(events[key], ['priority'], ['asc']) }, + /** + * + * @param ns + * @param key + * @returns + */ del (ns, key = '') { if (!key) { for (let key in events) { @@ -39,10 +57,24 @@ let Handler = { } } }, + /** + * + * @param key + * @param e + * @param args + */ async callAll (key, e, args) { // 暂时屏蔽调用 // return Handler.call(key, e, args, true) }, + /** + * + * @param key + * @param e + * @param args + * @param allHandler + * @returns + */ async call (key, e, args, allHandler = false) { let ret for (let obj of events[key]) { @@ -65,9 +97,18 @@ let Handler = { } return ret }, + /** + * + * @param key + * @returns + */ has (key) { return !!events[key] } } + +/** + * + */ export default Handler diff --git a/src/lib/plugins/loader.ts b/src/lib/plugins/loader.ts index 6d68115..b88cf44 100644 --- a/src/lib/plugins/loader.ts +++ b/src/lib/plugins/loader.ts @@ -19,11 +19,15 @@ class PluginsLoader { task = [] dir = 'plugins' - /** 命令冷却cd */ + /** + * 命令冷却cd + */ groupGlobalCD = {} singleCD = {} - /** 插件监听 */ + /** + * 插件监听 + */ watcher = {} eventMap = { @@ -34,10 +38,16 @@ class PluginsLoader { msgThrottle = {} - /** 星铁命令前缀 */ + /** + * 星铁命令前缀 + */ srReg = /^#?(\*|星铁|星轨|穹轨|星穹|崩铁|星穹铁道|崩坏星穹铁道|铁道)+/ + /** + * + * @returns + */ async getPlugins() { const files = await fs.readdir(this.dir, { withFileTypes: true }) const ret = [] @@ -102,6 +112,11 @@ class PluginsLoader { this.priority = lodash.orderBy(this.priority, ['priority'], ['asc']) } + /** + * + * @param file + * @param packageErr + */ async importPlugin(file, packageErr) { try { let app = await import(file.path) @@ -125,6 +140,12 @@ class PluginsLoader { } } + /** + * + * @param file + * @param p + * @returns + */ async loadPlugin(file, p) { if (!p?.prototype) return this.pluginCount++ @@ -153,6 +174,11 @@ class PluginsLoader { } } + /** + * + * @param packageErr + * @returns + */ packageTips(packageErr) { if (!packageErr || packageErr.length <= 0) return logger.mark('--------插件载入错误--------') @@ -276,7 +302,12 @@ class PluginsLoader { } } - /** 过滤事件 */ + /** + * 过滤事件 + * @param e + * @param v + * @returns + */ filtEvent(e, v) { if (!v.event) return false const event = v.event.split(".") @@ -291,7 +322,13 @@ class PluginsLoader { return v.event == newEvent.join(".") } - /** 判断权限 */ + + /** + * 判断权限 + * @param e + * @param v + * @returns + */ filtPermission(e, v) { if (v.permission == 'all' || !v.permission) return true @@ -338,12 +375,10 @@ class PluginsLoader { * @param e.isMaster 是否管理员 * @param e.logText 日志用户字符串 * @param e.logFnc 日志方法字符串 - * 频道 * @param e.isGuild 是否频道 * @param e.at 支持频道 tiny_id * @param e.atBot 支持频道 - */ dealMsg(e) { if (e.message) { @@ -446,7 +481,10 @@ class PluginsLoader { } } - /** 处理回复,捕获发送失败异常 */ + /** + * 处理回复,捕获发送失败异常 + * @param e + */ reply(e) { if (e.reply) { e.replyNew = e.reply @@ -532,6 +570,11 @@ class PluginsLoader { } } + /** + * + * @param e + * @param msg + */ count(e, msg) { let screenshot = false if (msg && msg?.file && Buffer.isBuffer(msg?.file)) { @@ -547,6 +590,11 @@ class PluginsLoader { } } + /** + * + * @param type + * @param groupId + */ saveCount(type, groupId = '') { let key = 'Yz:count:' @@ -565,20 +613,28 @@ class PluginsLoader { redis.expire(monthKey, 3600 * 24 * 30) } + /** + * + */ delCount() { let key = 'Yz:count:' redis.set(`${key}sendMsg:total`, '0') redis.set(`${key}screenshot:total`, '0') } - /** 收集定时任务 */ + /** + * 收集定时任务 + * @param task + */ collectTask(task) { for (const i of Array.isArray(task) ? task : [task]) if (i.cron && i.name) this.task.push(i) } - /** 创建定时任务 */ + /** + * 创建定时任务 + */ createTask() { for (const i of this.task) i.job = schedule.scheduleJob(i.cron, async () => { @@ -595,7 +651,11 @@ class PluginsLoader { }) } - /** 检查命令冷却cd */ + /** + * 检查命令冷却cd + * @param e + * @returns + */ checkLimit(e) { /** 禁言中 */ if (e.isGroup && e?.group?.mute_left > 0) return false @@ -624,7 +684,11 @@ class PluginsLoader { return true } - /** 设置冷却cd */ + /** + * 设置冷却cd + * @param e + * @returns + */ setLimit(e) { if (!e.message || e.isPrivate) return let config = cfg.getGroup(e.group_id) @@ -644,7 +708,11 @@ class PluginsLoader { } } - /** 是否只关注主动at */ + /** + * 是否只关注主动at + * @param e + * @returns + */ onlyReplyAt(e) { if (!e.message || e.isPrivate) return true @@ -665,12 +733,20 @@ class PluginsLoader { return false } - /** 判断频道消息 */ + /** + * 判断频道消息 + * @param e + * @returns + */ checkGuildMsg(e) { return cfg.getOther().disableGuildMsg && e.detail_type == 'guild' } - /** 判断黑白名单 */ + /** + * 判断黑白名单 + * @param e + * @returns + */ checkBlack(e) { const other = cfg.getOther() @@ -698,7 +774,11 @@ class PluginsLoader { return true } - /** 判断是否启用功能 */ + /** + * 判断是否启用功能 + * @param p + * @returns + */ checkDisable(p) { const groupCfg = cfg.getGroup(p.e.group_id) if (groupCfg.disable?.length && groupCfg.disable.includes(p.name)) @@ -708,6 +788,10 @@ class PluginsLoader { return true } + /** + * + * @param key + */ async changePlugin(key) { try { let app = await import(`../../${this.dir}/${key}?${moment().format('x')}`) @@ -727,7 +811,12 @@ class PluginsLoader { } } - /** 监听热更新 */ + /** + * 监听热更新 + * @param dirName + * @param appName + * @returns + */ watch(dirName, appName) { this.watchDir(dirName) if (this.watcher[`${dirName}.${appName}`]) return @@ -757,7 +846,12 @@ class PluginsLoader { this.watcher[`${dirName}.${appName}`] = watcher } - /** 监听文件夹更新 */ + + /** + * 监听文件夹更新 + * @param dirName + * @returns + */ watchDir(dirName) { if (this.watcher[dirName]) return const watcher = chokidar.watch(`./${this.dir}/${dirName}/`) @@ -780,6 +874,11 @@ class PluginsLoader { }, 10000) this.watcher[dirName] = watcher } + + } +/** + * + */ export default new PluginsLoader() \ No newline at end of file diff --git a/src/mys/local.js b/src/local.js similarity index 100% rename from src/mys/local.js rename to src/local.js diff --git a/src/mys/BaseModel.ts b/src/mys/BaseModel.ts index 5e7720c..5e88bb5 100644 --- a/src/mys/BaseModel.ts +++ b/src/mys/BaseModel.ts @@ -11,7 +11,13 @@ export default class BaseModel { return this } - // 获取缓存 + /** + * 获取缓存 + * @param model + * @param id + * @param time + * @returns + */ _getThis(model, id = '', time = 10 * 60) { const uuid = `${model}:${id}` this._uuid = uuid @@ -20,7 +26,13 @@ export default class BaseModel { } } - // 设置缓存 + /** + * 设置缓存 + * @param model + * @param id + * @param time + * @returns + */ _cacheThis(model, id, time = 10 * 60) { const uuid = this._uuid || `${model}:${id}` this._uuid = uuid @@ -32,7 +44,11 @@ export default class BaseModel { return this } - // 设置超时时间 + /** + * 设置超时时间 + * @param time + * @returns + */ _expire(time = 10 * 60) { let id = this._uuid reFn[id] && clearTimeout(reFn[id]) @@ -48,6 +64,9 @@ export default class BaseModel { } } + /** + * + */ _delCache() { let id = this._uuid reFn[id] && clearTimeout(reFn[id]) @@ -55,14 +74,29 @@ export default class BaseModel { delete cacheMap[id] } + /** + * + * @param game + * @returns + */ gameKey(game = 'gs') { return MysUtil.getGameKey(game) } + /** + * + * @param game + * @returns + */ isGs(game = 'gs') { return this.gameKey(game) === 'gs' } + /** + * + * @param game + * @returns + */ isSr(game = 'gs') { return this.gameKey(game) === 'sr' } diff --git a/src/mys/DailyCache.ts b/src/mys/DailyCache.ts index 7e998f7..19d8739 100644 --- a/src/mys/DailyCache.ts +++ b/src/mys/DailyCache.ts @@ -7,7 +7,19 @@ const servs = ['mys', 'hoyolab'] const EX = 3600 * 24 const redisKeyRoot = 'Yz:cache:' +/** + * ************ + * tudo + * *********** + * + */ export default class DailyCache extends BaseModel { + /** + * + * @param uid + * @param game + * @returns + */ constructor(uid, game = 'config') { super() const storeKey = DailyCache.getStoreKey(uid, game) @@ -33,7 +45,12 @@ export default class DailyCache extends BaseModel { return new DailyCache(uid, game) } - // 内部方法:获取redis表前缀 + /** + * 内部方法:获取redis表前缀 + * @param uid + * @param game + * @returns + */ static getStoreKey(uid, game = 'config') { let key if (!uid || game === 'config') { @@ -78,7 +95,12 @@ export default class DailyCache extends BaseModel { } } - // 内部方法,用于decode value + /** + * 内部方法,用于decode value + * @param value + * @param decode + * @returns + */ static decodeValue(value, decode = false) { if (value && decode) { try { @@ -90,7 +112,11 @@ export default class DailyCache extends BaseModel { return value } - // 内部方法,用于encode value + /** + * 内部方法,用于encode value + * @param value + * @returns + */ static encodeValue(value) { if (typeof value === 'object') { return JSON.stringify(value) || '' @@ -102,7 +128,13 @@ export default class DailyCache extends BaseModel { } /** ---- 基础方法 ---- **/ - // 内部方法:获取redis表key键值 + + /** + * 内部方法:获取redis表key键值 + * @param key + * @param sub + * @returns + */ getTableKey(key, sub = '') { if (sub) { return `${this.keyPre}:${key}-${sub}` diff --git a/src/mys/MysUser.ts b/src/mys/MysUser.ts index 4416a54..ffdae52 100644 --- a/src/mys/MysUser.ts +++ b/src/mys/MysUser.ts @@ -15,7 +15,7 @@ import fetch from 'node-fetch' import { MysUserDB, UserDB } from '../db/index.js' -import { Data } from './local.js' +import { Data } from '../local.js' const tables = { // ltuid-uid 查询表 @@ -40,7 +40,17 @@ const tables = { del: 'del-detail' } +/** + * ********** + * tudo + * *********** + */ export default class MysUser extends BaseModel { + /** + * + * @param ltuid + * @returns + */ constructor(ltuid) { super() if (!ltuid) { @@ -55,13 +65,19 @@ export default class MysUser extends BaseModel { return self._cacheThis() } - // 可传入ltuid、cookie、ck对象来创建MysUser实例 - + /** + * 可传入ltuid、cookie、ck对象来创建MysUser实例 + */ get uid() { return this.uids?.gs?.[0] || '' } - // 在仅传入ltuid时,必须是之前传入过的才能被识别 + /** + * 在仅传入ltuid时,必须是之前传入过的才能被识别 + * @param ltuid + * @param db + * @returns + */ static async create(ltuid, db = false) { ltuid = MysUtil.getLtuid(ltuid) if (!ltuid) { @@ -72,6 +88,10 @@ export default class MysUser extends BaseModel { return mys } + /** + * + * @param fn + */ static async forEach(fn) { let dbs = await MysUserDB.findAll() await Data.forEach(dbs, async db => { @@ -80,7 +100,13 @@ export default class MysUser extends BaseModel { }) } - // 根据uid获取查询MysUser + /** + * 根据uid获取查询MysUser + * @param uid + * @param game + * @param onlySelfCk + * @returns + */ static async getByQueryUid(uid, game = 'gs', onlySelfCk = false) { let servCache = DailyCache.create(uid, game) // 查找已经查询过的ltuid || 分配最少查询的ltuid @@ -127,6 +153,10 @@ export default class MysUser extends BaseModel { return false } + /** + * + * @param fn + */ static async eachServ(fn) { await MysUtil.eachServ(async serv => { await MysUtil.eachGame(async game => { @@ -136,7 +166,9 @@ export default class MysUser extends BaseModel { }) } - // 清除当日缓存 + /** + * 清除当日缓存 + */ static async clearCache() { await MysUser.eachServ(async function (servCache) { await servCache.empty(tables.detail) @@ -147,7 +179,10 @@ export default class MysUser extends BaseModel { await cache.empty(tables.qq) } - // 获取用户统计数据 + /** + * 获取用户统计数据 + * @returns + */ static async getStatData() { let totalCount = {} let ret = { servs: {} } @@ -261,7 +296,11 @@ export default class MysUser extends BaseModel { } } - // 不建议使用,为了兼容老数据格式,后续废弃 + /** + * 不建议使用,为了兼容老数据格式,后续废弃 + * @param game + * @returns + */ getCkInfo(game = 'gs') { return { ck: this.ck, @@ -271,6 +310,12 @@ export default class MysUser extends BaseModel { } } + /** + * + * @param uid + * @param game + * @returns + */ getUidData(uid, game = 'gs') { game = this.gameKey(game) if (!this.hasUid(uid, game)) { @@ -284,20 +329,40 @@ export default class MysUser extends BaseModel { } } + /** + * + * @param uid + * @param game + * @returns + */ hasUid(uid, game = 'gs') { game = this.gameKey(game) return this.uids[game].includes(uid + '') } + /** + * + * @param game + * @returns + */ getUid(game = 'gs') { return this.getUids(game)[0] } + /** + * + * @param game + * @returns + */ getUids(game = 'gs') { let gameKey = this.gameKey(game) return this.uids[gameKey] || [] } + /** + * + * @returns + */ getUidInfo() { let ret = [] MysUtil.eachGame((game, gameDs) => { @@ -360,6 +425,11 @@ export default class MysUser extends BaseModel { return { status: 0, msg: '' } } + /** + * + * @param serv + * @returns + */ async getGameRole(serv = 'mys') { let ck = this.ck let url = { @@ -375,7 +445,11 @@ export default class MysUser extends BaseModel { return res } - // 获取米游社通行证id + /** + * 获取米游社通行证id + * @param serv + * @returns + */ async getUserFullInfo(serv = 'mys') { let ck = this.ck let url = { @@ -398,6 +472,11 @@ export default class MysUser extends BaseModel { return res } + /** + * + * @param game + * @returns + */ getCache(game = 'gs') { if (!this.cache) { this.cache = {} @@ -422,7 +501,10 @@ export default class MysUser extends BaseModel { this.setCkData(db) } - // 设置ck数据 + /** + * 设置ck数据 + * @param data + */ setCkData(data = {}) { this.ck = data.ck || this.ck || '' this.type = data.type || this.type || 'mys' @@ -434,11 +516,19 @@ export default class MysUser extends BaseModel { }) } + /** + * + */ async save() { await this.db.saveDB(this) } - // 为当前MysUser绑定uid + /** + * 为当前MysUser绑定uid + * @param uid + * @param game + * @returns + */ addUid(uid, game = 'gs') { if (lodash.isArray(uid)) { for (let u of uid) { @@ -457,12 +547,20 @@ export default class MysUser extends BaseModel { return true } + /** + * + * @param game + * @returns + */ hasGame(game = 'gs') { game = this.gameKey(game) return this.uids[game]?.length > 0 } - // 初始化当前MysUser缓存记录 + /** + * 初始化当前MysUser缓存记录 + * @returns + */ async initCache() { if (!this.ltuid || !this.ck) { return @@ -486,13 +584,16 @@ export default class MysUser extends BaseModel { return true } + /** + * + * @param game + */ async disable(game = 'gs') { let cache = this.getCache(game) await cache.zDel(tables.detail, this.ltuid) logger.mark(`[标记无效ck][game:${game}, ltuid:${this.ltuid}`) } - // /** * 删除缓存, 供User解绑CK时调用 * @returns {Promise} @@ -513,7 +614,10 @@ export default class MysUser extends BaseModel { logger.mark(`[删除失效ck][ltuid:${this.ltuid}]`) } - // 删除MysUser用户记录,会反向删除User中的记录及绑定关系 + /** + * 删除MysUser用户记录,会反向删除User中的记录及绑定关系 + * @param game + */ async delWithUser(game = 'gs') { // 查找用户 let cache = this.getCache(game) @@ -530,7 +634,12 @@ export default class MysUser extends BaseModel { await this.del() } - // 为当前用户添加uid查询记录 + /** + * 为当前用户添加uid查询记录 + * @param uid + * @param game + * @returns + */ async addQueryUid(uid, game = 'gs') { if (lodash.isArray(uid)) { for (let u of uid) { @@ -544,19 +653,33 @@ export default class MysUser extends BaseModel { } } - // 获取当前用户已查询uid列表 + /** + * 获取当前用户已查询uid列表 + * @param game + * @returns + */ async getQueryUids(game = 'gs') { let cache = this.getCache(game) return await cache.zList(tables.detail, this.ltuid) } - // 根据uid获取查询ltuid + /** + * 根据uid获取查询ltuid + * @param uid + * @param game + * @returns + */ async getQueryLtuid(uid, game = 'gs') { let cache = this.getCache(game) return await cache.zKey(tables.detail, uid) } - // 检查指定uid是否为当前MysUser所有 + /** + * 检查指定uid是否为当前MysUser所有 + * @param uid + * @param game + * @returns + */ ownUid(uid, game = 'gs') { if (!uid) { return false diff --git a/src/mys/MysUtil.ts b/src/mys/MysUtil.ts index 88adffe..0ec09bc 100644 --- a/src/mys/MysUtil.ts +++ b/src/mys/MysUtil.ts @@ -1,12 +1,22 @@ -import { Data } from './local.js' +import { Data } from '../local.js' +/** + * + */ const games = [ { key: 'gs', name: '原神' }, { key: 'sr', name: '星穹铁道' } ] +/** + * + */ const MysUtil = { - // 获取标准ltuid + /** + * 获取标准ltuid + * @param data + * @returns + */ getLtuid(data) { if (!data) { return false @@ -21,7 +31,11 @@ const MysUtil = { return false }, - // 获取标准gameKey + /** + * 获取标准gameKey + * @param game + * @returns + */ getGameKey(game) { // 兼容e的处理 if (game.user_id) { @@ -30,7 +44,10 @@ const MysUtil = { return ['sr', 'star'].includes(game) ? 'sr' : 'gs' }, - // 生成设备guid + /** + * 生成设备guid + * @returns + */ getDeviceGuid() { function S4() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) @@ -52,16 +69,26 @@ const MysUtil = { ) }, - // 循环game + /** + * 循环game + * @param fn + */ async eachGame(fn) { await Data.forEach(games, ds => { return fn(ds.key, ds) }) }, - // 循环server + /** + * 循环server + * @param fn + */ async eachServ(fn) { await Data.forEach(['mys', 'hoyolab'], fn) } } + +/** + * + */ export default MysUtil diff --git a/src/mys/NoteUser.ts b/src/mys/NoteUser.ts index ce78f91..952a628 100644 --- a/src/mys/NoteUser.ts +++ b/src/mys/NoteUser.ts @@ -1,3 +1,9 @@ +import BaseModel from './BaseModel.js' +import lodash from 'lodash' +import MysUser from './MysUser.js' +import MysUtil from './MysUtil.js' +import { UserDB } from '../db/index.js' +import { Data } from '../local.js' /** * Bot实际User用户类 * 主键QQ @@ -5,17 +11,12 @@ * User可以注册UID,通过 getRegUid / setRegUid * 一个User可以绑定多个MysUser CK,绑定MysUser */ -import BaseModel from './BaseModel.js' -import lodash from 'lodash' -import MysUser from './MysUser.js' -import MysUtil from './MysUtil.js' - -import { UserDB } from '../db/index.js' - -// -import { Data } from './local.js' - export default class NoteUser extends BaseModel { + /** + * + * @param qq + * @returns + */ constructor(qq) { super() // 检查实例缓存 @@ -109,6 +110,10 @@ export default class NoteUser extends BaseModel { return user } + /** + * + * @param fn + */ static async forEach(fn) { let dbs = await UserDB.findAll() await Data.forEach(dbs, async db => { @@ -117,7 +122,11 @@ export default class NoteUser extends BaseModel { }) } - // 初始化数据 + /** + * 初始化数据 + * @param db + * @returns + */ async initDB(db = false) { if (this.db && !db) { return @@ -132,7 +141,9 @@ export default class NoteUser extends BaseModel { await this.save() } - // 初始化MysUser对象 + /** + * 初始化MysUser对象 + */ async initMysUser() { let ltuids = this.db?.ltuids || '' this.mysUsers = {} @@ -144,10 +155,19 @@ export default class NoteUser extends BaseModel { } } + /** + * + */ async save() { await this.db.saveDB(this) } + /** + * + * @param game + * @param type + * @returns + */ getUidMapList(game = 'gs', type = 'all') { if (this._map?.[game]?.[type]) { return this._map[game][type] @@ -186,6 +206,12 @@ export default class NoteUser extends BaseModel { return this._map[game][type] } + /** + * + * @param uid + * @param game + * @returns + */ getUidData(uid = '', game = 'gs') { if (!uid) { uid = this.getUid(game) @@ -193,7 +219,12 @@ export default class NoteUser extends BaseModel { return this.getUidMapList(game, 'all').map[uid] } - /** 有Uid */ + /** + * 有Uid + * @param uid + * @param game + * @returns + */ hasUid(uid = '', game = '') { if (!uid) { return this.getUidMapList(game, 'all').list?.length > 0 @@ -201,19 +232,31 @@ export default class NoteUser extends BaseModel { return !!this.getUidData(uid, game) } - /** 获取CK-Uid */ + /** + * 获取CK-Uid + * @param game + * @returns + */ getCkUid(game = 'gs') { let uid = this.getUid(game) let { map, list } = this.getUidMapList(game, 'ck') return (map[uid] ? uid : list[0]?.uid) || '' } - /** 获取CK-Uid列表 */ + /** + * 获取CK-Uid列表 + * @param game + * @returns + */ getCkUidList(game = 'gs') { return this.getUidMapList(game, 'ck').list } - /** 获取当前UID */ + /** + * 获取当前UID + * @param game + * @returns + */ getUid(game = 'gs') { game = this.gameKey(game) // todo 刷新uid @@ -224,12 +267,20 @@ export default class NoteUser extends BaseModel { return ds.uid || '' } - /** 获取UID列表 */ + /** + * 获取UID列表 + * @param game + * @returns + */ getUidList(game = 'gs') { return this.getUidMapList(game, 'all').list } - /** 获取当前的MysUser对象 */ + /** + * 获取当前的MysUser对象 + * @param game + * @returns + */ getMysUser(game = 'gs') { if (lodash.isEmpty(this.mysUsers)) { return false @@ -242,7 +293,12 @@ export default class NoteUser extends BaseModel { return this.mysUsers[uidData.ltuid] } - // 添加UID + /** + * 添加UID + * @param uid + * @param game + * @param save + */ addRegUid(uid, game = 'gs', save = true) { game = this.gameKey(game) uid = uid + '' @@ -255,7 +311,11 @@ export default class NoteUser extends BaseModel { } } - // 删除UID + /** + * 删除UID + * @param uid + * @param game + */ delRegUid(uid, game = 'gs') { game = this.gameKey(game) let gameDs = this.getGameDs(game) @@ -269,6 +329,11 @@ export default class NoteUser extends BaseModel { this.save() } + /** + * + * @param game + * @returns + */ getGameDs(game = 'gs') { game = this.gameKey(game) if (!this._games[game]) { @@ -293,7 +358,13 @@ export default class NoteUser extends BaseModel { return uid } - // 切换绑定CK生效的UID + /** + * 切换绑定CK生效的UID + * @param uid + * @param game + * @param save + * @returns + */ setMainUid(uid = '', game = 'gs', save = true) { this._map = false game = this.gameKey(game) @@ -313,7 +384,10 @@ export default class NoteUser extends BaseModel { } } - // 添加MysUser + /** + * 添加MysUser + * @param mysUser + */ async addMysUser(mysUser) { this.mysUsers[mysUser.ltuid] = mysUser this._map = false @@ -326,12 +400,20 @@ export default class NoteUser extends BaseModel { this.save() } - // 删除当前用户绑定CK + /** + * 删除当前用户绑定CK + * @param ltuid + * @returns + */ async delCk(ltuid = '') { console.warn('delCk即将废弃') return await this.delMysUser(ltuid) } + /** + * + * @param mysUser + */ async delMysUser(mysUser = '') { let ltuid = mysUser.ltuid || mysUser if (ltuid && this.mysUsers[ltuid]) { @@ -344,6 +426,10 @@ export default class NoteUser extends BaseModel { await this.save() } + /** + * + * @param fn + */ async eachMysUser(fn) { await Data.forEach(this.mysUsers, async (mys, ltuid) => { if (!mys) { @@ -353,12 +439,18 @@ export default class NoteUser extends BaseModel { }) } + /** + * + * @param fn + * @returns + */ async eachAllMysUser(fn) { return MysUser.forEach(fn) } /** * 检查当前用户绑定的CK状态 + * @returns */ async checkCk() { // TODO:待完善文案 diff --git a/src/mys/apiTool.ts b/src/mys/apiTool.ts index d30f401..b1e787d 100644 --- a/src/mys/apiTool.ts +++ b/src/mys/apiTool.ts @@ -22,6 +22,11 @@ export default class apiTool { } } + /** + * + * @param data + * @returns + */ getUrlMap = (data = {}) => { let host, hostRecord, hostPublicData if ( diff --git a/src/mys/gsCfg.ts b/src/mys/gsCfg.ts index 9bb192c..be89546 100644 --- a/src/mys/gsCfg.ts +++ b/src/mys/gsCfg.ts @@ -4,25 +4,24 @@ import fs from 'node:fs' import lodash from 'lodash' import MysInfo from './mysInfo.js' import NoteUser from './NoteUser.js' -import { Character, Weapon } from './local.js' +import { Character, Weapon } from '../local.js' -/** 配置文件 */ +/** + * *********** + * 配置文件 + * *********** + */ class GsCfg { - constructor() { - this.isSr = false - /** 默认设置 */ - this.defSetPath = './plugins/genshin/defSet/' - this.defSet = {} - - /** 用户设置 */ - this.configPath = './plugins/genshin/config/' - this.config = {} - - /** 监听文件 */ - this.watcher = { config: {}, defSet: {} } - - this.ignore = ['mys.pubCk', 'gacha.set', 'bot.help', 'role.name'] - } + isSr = false + /** 默认设置 */ + defSetPath = './plugins/genshin/defSet/' + defSet = {} + /** 用户设置 */ + configPath = './plugins/genshin/config/' + config = {} + /** 监听文件 */ + watcher = { config: {}, defSet: {} } + ignore = ['mys.pubCk', 'gacha.set', 'bot.help', 'role.name'] get element() { return { @@ -39,7 +38,12 @@ class GsCfg { return this.getYaml(app, name, 'defSet') } - /** 用户配置 */ + /** + * 用户配置 + * @param app + * @param name + * @returns + */ getConfig(app, name) { if (this.ignore.includes(`${app}.${name}`)) { return this.getYaml(app, name, 'config') @@ -75,6 +79,13 @@ class GsCfg { return this[type][key] } + /** + * + * @param app s + * @param name + * @param type + * @returns + */ getFilePath(app, name, type) { if (type == 'defSet') { return `${this.defSetPath}${app}/${name}.yaml` @@ -83,7 +94,14 @@ class GsCfg { } } - /** 监听配置文件 */ + /** + * 监听配置文件 + * @param file + * @param app + * @param name + * @param type + * @returns + */ watch(file, app, name, type = 'defSet') { let key = `${app}.${name}` @@ -101,7 +119,11 @@ class GsCfg { this.watcher[type][key] = watcher } - /** 读取所有用户绑定的ck */ + /** + * 读取所有用户绑定的ck + * @param game + * @returns + */ async getBingCk(game = 'gs') { let ck = {} let ckQQ = {} @@ -129,13 +151,20 @@ class GsCfg { /** * 原神角色id转换角色名字 + * @param id + * @returns */ roleIdToName(id) { let char = Character.get(id) return char?.name || '' } - /** 原神角色别名转id */ + /** + * 原神角色别名转id + * @param keyword + * @param isSr + * @returns + */ roleNameToID(keyword, isSr) { let char = Character.get(keyword, isSr ? 'sr' : 'gs') return char?.id || false @@ -157,6 +186,11 @@ class GsCfg { await MysInfo.initPubCk() } + /** + * + * @param groupId + * @returns + */ getGachaSet(groupId = '') { let config = this.getYaml('gacha', 'set', 'config') let def = config.default @@ -166,6 +200,11 @@ class GsCfg { return def } + /** + * + * @param msg + * @returns + */ getMsgUid(msg) { let ret = /([1-9]|18)[0-9]{8}/g.exec(msg) if (!ret) return false @@ -206,6 +245,11 @@ class GsCfg { } } + /** + * + * @param app + * @param name + */ cpCfg(app, name) { if (!fs.existsSync('./plugins/genshin/config')) { fs.mkdirSync('./plugins/genshin/config') @@ -217,7 +261,10 @@ class GsCfg { } } - // 仅供内部调用 + /** + * 仅供内部调用 + * @returns + */ _getAbbr() { if (this[this.isSr ? 'sr_nameID' : 'nameID']) return @@ -250,7 +297,12 @@ class GsCfg { } } - // 仅供内部调用 + /** + * 仅供内部调用 + * @param keyword + * @param isSr + * @returns + */ _roleNameToID(keyword, isSr) { if (isSr) this.isSr = isSr if (!isNaN(keyword)) keyword = Number(keyword) @@ -259,7 +311,13 @@ class GsCfg { return roelId || false } - // 仅供内部调用 + /** + * 仅供内部调用 + * @param msg + * @param filterMsg + * @param isSr + * @returns + */ _getRole(msg, filterMsg = '', isSr = false) { let alias = msg.replace(/#|老婆|老公|([1-9]|18)[0-9]{8}/g, '').trim() if (filterMsg) { @@ -280,45 +338,97 @@ class GsCfg { } } + /** + * 仅供内部调用 + * @param hash + * @deprecated 已废弃 + * @returns + */ getWeaponDataByWeaponHash(hash) { console.log('gsCfg.getWeaponDataByWeaponHash() 已废弃') return {} } + /** + * + * @deprecated 已废弃 + * @returns + */ getAllAbbr() { console.log('gsCfg.getAllAbbr() 已废弃') return {} } + /** + * + * @deprecated 已废弃 + * @param userId + * @returns + */ getBingCkSingle(userId) { console.log('gsCfg.getBingCkSingle() 已废弃') return {} } + /** + * + * @deprecated 已废弃 + * @param userId + * @param data + */ saveBingCk(userId, data) { console.log('gsCfg.saveBingCk() 已废弃') } + /** + * + * @deprecated 已废弃 + * @param roleName + * @returns + */ getElementByRoleName(roleName) { console.log('gsCfg.getElementByRoleName() 已废弃') return '' } + /** + * + * @deprecated 已废弃 + * @param skillId + * @param roleName + * @returns + */ getSkillDataByskillId(skillId, roleName) { console.log('gsCfg.getSkillDataByskillId() 已废弃') return {} } + /** + * + * @deprecated 已废弃 + * @param propId + * @returns + */ fightPropIdToName(propId) { console.log('gsCfg.fightPropIdToName() 已废弃') return '' } + /** + * + * @deprecated 已废弃 + * @param talentId + * @returns + */ getRoleTalentByTalentId(talentId) { console.log('gsCfg.getRoleTalentByTalentId 已废弃') return {} } + /** + * + * @deprecated 已废弃 + */ getAbbr() { console.log('gsCfg.getAbbr() 已经废弃') } diff --git a/src/mys/mysApi.ts b/src/mys/mysApi.ts index 8580eb9..40cc577 100644 --- a/src/mys/mysApi.ts +++ b/src/mys/mysApi.ts @@ -35,6 +35,12 @@ export default class MysApi { return this._device } + /** + * + * @param type + * @param data + * @returns + */ getUrl(type, data = {}) { let urlMap = this.apiTool.getUrlMap({ ...data, deviceId: this.device }) if (!urlMap[type]) return false @@ -49,6 +55,10 @@ export default class MysApi { return { url, headers, body } } + /** + * + * @returns + */ getServer() { switch (String(this.uid).slice(0, -8)) { case '1': @@ -69,6 +79,13 @@ export default class MysApi { return this.isSr ? 'prod_gf_cn' : 'cn_gf01' } + /** + * + * @param type + * @param data + * @param cached + * @returns + */ async getData(type, data = {}, cached = false) { if ( !this._device_fp && @@ -147,6 +164,12 @@ export default class MysApi { return res } + /** + * + * @param query + * @param body + * @returns + */ getHeaders(query = '', body = '') { const cn = { app_version: '2.40.1', @@ -180,6 +203,12 @@ export default class MysApi { } } + /** + * + * @param q + * @param b + * @returns + */ getDs(q = '', b = '') { let n = '' if ( @@ -195,6 +224,10 @@ export default class MysApi { return `${t},${r},${DS}` } + /** + * + * @returns + */ getGuid() { function S4() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) @@ -216,15 +249,31 @@ export default class MysApi { ) } + /** + * + * @param type + * @param data + * @returns + */ cacheKey(type, data) { return 'Yz:genshin:mys:cache:' + md5(this.uid + type + JSON.stringify(data)) } + /** + * + * @param res + * @param cacheKey + * @returns + */ async cache(res, cacheKey) { if (!res || res.retcode !== 0) return redis.setEx(cacheKey, this.cacheCd, JSON.stringify(res)) } + /** + * + * @returns + */ async getAgent() { let proxyAddress = cfg.bot.proxyAddress if (!proxyAddress) return null @@ -249,6 +298,11 @@ export default class MysApi { return null } + /** + * + * @param length + * @returns + */ generateSeed(length = 16) { const characters = '0123456789abcdef' let result = '' @@ -259,6 +313,10 @@ export default class MysApi { } } +/** + * + * @returns + */ export function randomRange() { let randomStr = '' let charStr = 'abcdef0123456789' diff --git a/src/mys/mysInfo.ts b/src/mys/mysInfo.ts index 1ca79a8..554b1ba 100644 --- a/src/mys/mysInfo.ts +++ b/src/mys/mysInfo.ts @@ -8,9 +8,17 @@ import NoteUser from './NoteUser.js' import MysUser from './MysUser.js' import DailyCache from './DailyCache.js' +/** + * ************* + * tudo + * ********* + */ export default class MysInfo { static tips = '请先#绑定Cookie\n发送【Cookie帮助】查看配置教程' - + /** + * + * @param e + */ constructor(e) { if (e) { this.e = e @@ -51,6 +59,12 @@ export default class MysInfo { ]) } + /** + * + * @param e + * @param api + * @returns + */ static async init(e, api) { await MysInfo.initCache() @@ -328,12 +342,21 @@ export default class MysInfo { return true } + /** + * + * @returns + */ static async getBingCkUid() { let res = await GsCfg.getBingCk() return { ...res.ck } } - // 获取uid绑定的ck信息 + /** + * 获取uid绑定的ck信息 + * @param uid + * @param game + * @returns + */ static async checkUidBing(uid, game = 'gs') { let ckUser = await MysUser.getByQueryUid(uid, game, true) if (ckUser && ckUser.ck) { @@ -342,6 +365,10 @@ export default class MysInfo { return false } + /** + * + * @returns + */ static async delDisable() { return await MysUser.delDisable() } @@ -363,6 +390,10 @@ export default class MysInfo { return false } + /** + * + * @returns + */ async checkReply() { if (this.e.noTips === true) return @@ -413,6 +444,15 @@ export default class MysInfo { return this.ckUser?.ck } + /** + * + * @param res + * @param type + * @param mysApi + * @param data + * @param isTask + * @returns + */ async checkCode(res, type, mysApi = {}, data = {}, isTask = false) { if (!res) { if (!isTask) @@ -558,7 +598,10 @@ export default class MysInfo { return res } - /** 删除失效ck */ + /** + * 删除失效ck + * @returns + */ async delCk() { if (!this.ckUser) { return false @@ -568,7 +611,10 @@ export default class MysInfo { await ckUser.delWithUser() } - /** 查询次数满,今日内标记失效 */ + /** + * 查询次数满,今日内标记失效 + * @param game + */ async disableToday(game = 'gs') { /** 统计次数设为超限 */ await this.ckUser.disable(game) diff --git a/src/utils/puppeteer.ts b/src/utils/puppeteer.ts index 76df64f..21b8ac6 100644 --- a/src/utils/puppeteer.ts +++ b/src/utils/puppeteer.ts @@ -3,6 +3,9 @@ import puppeteer, { Browser } from 'puppeteer' import { ScreenshotFileOptions } from './types.js' import { BaseConfig } from './config.js' +/** + * + */ export const PuppeteerLunchConfig = new BaseConfig({ // 禁用超时 timeout: 0, //otocolTimeout: 0, @@ -26,6 +29,9 @@ export const PuppeteerLunchConfig = new BaseConfig({ // } }) +/** + * + */ export class Puppeteer { // 截图次数记录 #pic = 0 diff --git a/src/utils/types.ts b/src/utils/types.ts index 8838935..a63aae6 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1,7 +1,9 @@ import { type ScreenshotOptions, type PuppeteerLifeCycleEvent } from 'puppeteer' - import queryString from 'querystring' +/** + * + */ export interface ScreenshotFileOptions { SOptions?: { type: 'jpeg' | 'png' | 'webp' @@ -11,6 +13,9 @@ export interface ScreenshotFileOptions { timeout?: number } +/** + * + */ export interface ScreenshotUrlOptions { url: string time?: number