diff --git a/apps/bika.js b/apps/bika.js index e17b82b..6aaf04b 100644 --- a/apps/bika.js +++ b/apps/bika.js @@ -99,20 +99,14 @@ export class NewBika extends plugin { } if (!imageQualityType[quality] && !Object.values(imageQualityType).includes(quality)) return e.reply(`错误参数,支持的参数为${Object.keys(imageQualityType).join(',')}`) let type = imageQualityType[quality] ?? quality - await redis.set('yenai:bika:imageQuality', type) - Bika.imageQuality = type + Config.modify('bika', 'imageQuality', type) e.reply(`✅ 已将bika图片质量修改为${quality}(${type})`) } /** 图片直连 */ async directConnection (e) { if (!e.isMaster) return false - if (/开启/.test(e.msg)) { - await redis.set('yenai:bika:directConnection', '1') - } else { - await redis.del('yenai:bika:directConnection') - } - await Bika.init() + Config.modify('bika', 'bikaDirectConnection', /开启/.test(e.msg)) e.reply(`✅ 已${/开启/.test(e.msg) ? '开启' : '关闭'}哔咔直连`) } diff --git a/apps/handle.js b/apps/handle.js index 46b3157..8568426 100644 --- a/apps/handle.js +++ b/apps/handle.js @@ -126,7 +126,8 @@ export class NewHandle extends plugin { source = (await e.friend.getChatHistory(e.source.time, 1)).pop() } if (!source) return e.reply('❎ 获取消息失败') - let sourceMsg = source.source.message?.split('\n') + let sourceMsg = source.source?.message?.split('\n') + if (!sourceMsg) return e.reply('❎ 获取原消息失败,请使用同意xxx进行处理') if (e.isGroup) { if (!e.isMaster && !e.member.is_owner && !e.member.is_admin) { return e.reply('❎ 该命令仅限管理员可用', true) diff --git a/apps/pixiv.js b/apps/pixiv.js index 59b60a6..3518157 100644 --- a/apps/pixiv.js +++ b/apps/pixiv.js @@ -59,9 +59,14 @@ export class NewPixiv extends plugin { fnc: 'pximg' }, { - reg: '^#(p站|pixiv)((查看|更换)代理.*|(开启|关闭)直连)$', + reg: '^#(p站|pixiv)(查看|更换)代理.*$', fnc: 'setProxy', permission: 'master' + }, + { + reg: '^#(p站|pixiv)(开启|关闭)直连$', + fnc: 'directConnection', + permission: 'master' } ] }) @@ -225,13 +230,17 @@ export class NewPixiv extends plugin { ] if (/查看/.test(e.msg)) return e.reply(await redis.get('yenai:proxy')) let proxy = e.msg.replace(/#|(p站|pixiv)更换代理/g, '').trim() - if (/直连/.test(e.msg)) { - proxy = /开启/.test(e.msg) ? 'i.pximg.net' : proxydef[0] - } if (/^[1234]$/.test(proxy)) proxy = proxydef[proxy - 1] if (!/([\w\d]+\.){2}[\w\d]+/.test(proxy)) return e.reply('请检查代理地址是否正确') logger.mark(`${e.logFnc}切换为${proxy}`) - Pixiv.proxy = proxy + Config.modify('pixiv', 'pixivImageProxy', proxy) e.reply(`✅ 已经切换代理为「${proxy}」`) } + + /** 图片直连 */ + async directConnection (e) { + if (!e.isMaster) return false + Config.modify('pixiv', 'pixivDirectConnection', /开启/.test(e.msg)) + e.reply(`✅ 已${/开启/.test(e.msg) ? '开启' : '关闭'}Pixiv直连`) + } } diff --git a/apps/set.js b/apps/set.js index 61790e2..89bd239 100644 --- a/apps/set.js +++ b/apps/set.js @@ -216,7 +216,7 @@ export class NewConfig extends plugin { async switchProxy (e) { let is = /开启/.test(e.msg) - Config.modify('other', 'switchProxy', is) + Config.modify('proxy', 'switchProxy', is) e.reply(`✅ 已${is ? '开启' : '关闭'}代理`) } diff --git a/apps/state.js b/apps/state.js index e05b323..bdd5bdb 100644 --- a/apps/state.js +++ b/apps/state.js @@ -5,8 +5,6 @@ import { CPU, common, puppeteer } from '../model/index.js' import moment from 'moment' import lodash from 'lodash' -let si = await redis.get('yenai:node_modules') ? await import('systeminformation') : false - let interval = false export class State extends plugin { constructor () { @@ -27,11 +25,11 @@ export class State extends plugin { async state (e) { if (!/椰奶/.test(e.msg) && !Config.Notice.state) return false - if (!si) return e.reply('❎ 没有检测到systeminformation依赖,请运行:"pnpm add systeminformation -w"进行安装') + if (!CPU.si) return e.reply('❎ 没有检测到systeminformation依赖,请运行:"pnpm add systeminformation -w"进行安装') // 防止多次触发 if (interval) { return false } else interval = true // 系统 - let osinfo = await si.osInfo() + let osinfo = await CPU.si.osInfo() // 可视化数据 let visualData = lodash.compact([ // CPU板块 diff --git a/components/Config.js b/components/Config.js index 23e1873..f1d4cd1 100644 --- a/components/Config.js +++ b/components/Config.js @@ -71,9 +71,19 @@ class Config { return this.getDefOrConfig('groupAdd') } - /** 其他 */ - get other () { - return this.getDefOrConfig('other') + /** 代理 */ + get proxy () { + return this.getDefOrConfig('proxy') + } + + /** pixiv */ + get pixiv () { + return this.getDefOrConfig('pixiv') + } + + /** 哔咔 */ + get bika () { + return this.getDefOrConfig('bika') } /** 搜图配置 */ diff --git a/config/default_config/bika.yaml b/config/default_config/bika.yaml new file mode 100644 index 0000000..a49c969 --- /dev/null +++ b/config/default_config/bika.yaml @@ -0,0 +1,6 @@ +#哔咔图片直连,直接使用哔咔原图发送不使用图片反代,国内则需使用代理 +bikaDirectConnection: false +#哔咔图片反代 +bikaImageProxy: p.sesepic.top/static +#哔咔图片质量,可选值 ['low', 'medium', 'high', 'original'] 质量依次从低到高 +imageQuality: medium diff --git a/config/default_config/pixiv.yaml b/config/default_config/pixiv.yaml new file mode 100644 index 0000000..55b87dd --- /dev/null +++ b/config/default_config/pixiv.yaml @@ -0,0 +1,4 @@ +#pixiv图片直连,国内需配合代理使用 +pixivDirectConnection: false +#pixiv图片反代,开启直连后反代服务则无效 +pixivImageProxy: i.pixiv.re diff --git a/config/default_config/other.yaml b/config/default_config/proxy.yaml similarity index 71% rename from config/default_config/other.yaml rename to config/default_config/proxy.yaml index 110f276..f2a0fd8 100644 --- a/config/default_config/other.yaml +++ b/config/default_config/proxy.yaml @@ -1,4 +1,5 @@ -#代理用于Pixiv和哔咔图片 +#代理绝大部分请求 proxyAddress: http://127.0.0.1:7890 #开启或关闭使用代理 switchProxy: false + diff --git a/index.js b/index.js index 694df00..2489690 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ import fs from 'node:fs' import Ver from './components/Version.js' import chalk from 'chalk' - +import { CPU } from './model/index.js' const files = fs.readdirSync('./plugins/yenai-plugin/apps').filter(file => file.endsWith('.js')) let ret = [] @@ -11,8 +11,7 @@ logger.info(chalk.rgb(255, 207, 247)(`椰奶插件${Ver.ver}初始化~`)) logger.info(chalk.rgb(253, 235, 255)('-------------------------')) try { - await import('systeminformation') - if (!await redis.get('yenai:node_modules')) await redis.set('yenai:node_modules', '1') + CPU.si = await import('systeminformation') } catch (error) { if (error.stack?.includes('Cannot find package')) { logger.warn('--------椰奶依赖缺失--------') @@ -24,7 +23,6 @@ try { logger.error(`椰奶载入依赖错误:${logger.red('systeminformation')}`) logger.error(decodeURI(error.stack)) } - await redis.del('yenai:node_modules') } files.forEach((file) => { diff --git a/lib/request/request.js b/lib/request/request.js index 09e6143..3046847 100644 --- a/lib/request/request.js +++ b/lib/request/request.js @@ -62,8 +62,8 @@ export default new class { } async getAgent (cf) { - let { proxyAddress } = Config.other; let { cfTLSVersion } = Config.picSearch - return cf ? this.getTlsVersionAgent(proxyAddress, cfTLSVersion) : (Config.other.switchProxy ? new HttpsProxyAgent(proxyAddress) : false) + let { proxyAddress, switchProxy } = Config.proxy; let { cfTLSVersion } = Config.picSearch + return cf ? this.getTlsVersionAgent(proxyAddress, cfTLSVersion) : (switchProxy ? new HttpsProxyAgent(proxyAddress) : false) } /** diff --git a/model/Bika.js b/model/Bika.js index 8d3518e..44bb43c 100644 --- a/model/Bika.js +++ b/model/Bika.js @@ -1,29 +1,22 @@ import fetch from 'node-fetch' import lodash from 'lodash' import request from '../lib/request/request.js' +import { Config } from '../components/index.js' /** API请求错误文案 */ const API_ERROR = '❎ 出错辣,请稍后重试' export default new (class { constructor () { this.domain = 'http://api.liaobiao.top/api/bika' - this.imgproxy = null - this.imageQuality = 'medium' this.hearder = { headers: { - 'x-image-quality': this.imageQuality, - 'user-agent': 'Yenai-Plugin-bika' + 'x-image-quality': Config.bika.imageQuality, + 'User-Agent': 'Yenai-Plugin-bika' } } - this.init() } - async init () { - if (!await redis.get('yenai:bika:directConnection')) { - this.imgproxy = 'https://p.sesepic.top/static/' - } else { - this.imgproxy = undefined - } - this.imageQuality = await redis.get('yenai:bika:imageQuality') ?? 'medium' + get imgproxy () { + return Config.bika.bikaDirectConnection ? undefined : `https://${Config.bika.bikaImageProxy}/` } /** @@ -156,6 +149,7 @@ export default new (class { async requestBikaImg (fileServer, path) { fileServer = /static/.test(fileServer) ? fileServer : fileServer + '/static/' let url = (/picacomic.com/.test(fileServer) && this.imgproxy ? this.imgproxy : fileServer) + path + logger.debug(`Bika getImg URL: ${url}`) return request.proxyRequestImg(url) } })() diff --git a/model/CPU.js b/model/CPU.js index c280f66..1c185bf 100644 --- a/model/CPU.js +++ b/model/CPU.js @@ -3,7 +3,6 @@ import lodash from 'lodash' import fs from 'fs' import { common } from './index.js' import { Config } from '../components/index.js' -let si = await redis.get('yenai:node_modules') ? await import('systeminformation') : false export default new class OSUtils { constructor () { @@ -11,24 +10,25 @@ export default new class OSUtils { this.isGPU = false this.now_network = null this.fsStats = null + this.si = null this.init() } async init () { - if (!si) return + if (!this.si) return // 初始化GPU获取 - if ((await si.graphics()).controllers.find(item => item.memoryUsed && item.memoryFree && item.utilizationGpu)) { + if ((await this.si.graphics()).controllers.find(item => item.memoryUsed && item.memoryFree && item.utilizationGpu)) { this.isGPU = true } // 给有问题的用户关闭定时器 if (!Config.Notice.statusTask) return // 网速 let worktimer = setInterval(async () => { - this.now_network = await si.networkStats() + this.now_network = await this.si.networkStats() }, 5000) // 磁盘写入速度 let fsStatstimer = setInterval(async () => { - this.fsStats = await si.fsStats() + this.fsStats = await this.si.fsStats() }, 5000) // 一分钟后检测是否能获取不能则销毁定时器 setTimeout(() => { @@ -122,14 +122,14 @@ export default new class OSUtils { /** 获取CPU占用 */ async getCpuInfo (arch) { // cpu使用率 - let cpu_info = (await si.currentLoad())?.currentLoad + let cpu_info = (await this.si.currentLoad())?.currentLoad if (cpu_info == null || cpu_info == undefined) return false // 核心 let hx = os.cpus() // cpu制造者 let cpumodel = hx[0]?.model.slice(0, hx[0]?.model.indexOf(' ')) || '' // 最大MHZ - let maxspeed = await si.cpuCurrentSpeed() + let maxspeed = await this.si.cpuCurrentSpeed() return { ...this.Circle(cpu_info / 100), @@ -148,7 +148,7 @@ export default new class OSUtils { async getGPU () { if (!this.isGPU) return false try { - let graphics = (await si.graphics()).controllers.find(item => item.memoryUsed && item.memoryFree && item.utilizationGpu) + let graphics = (await this.si.graphics()).controllers.find(item => item.memoryUsed && item.memoryFree && item.utilizationGpu) let { vendor, temperatureGpu, utilizationGpu, memoryTotal, memoryUsed, powerDraw } = graphics temperatureGpu = temperatureGpu ? temperatureGpu + '℃' : '' powerDraw = powerDraw ? powerDraw + 'W' : '' @@ -174,7 +174,7 @@ export default new class OSUtils { */ async getfsSize () { // 去重 - let HardDisk = lodash.uniqWith(await si.fsSize(), + let HardDisk = lodash.uniqWith(await this.si.fsSize(), (a, b) => a.used === b.used && a.size === b.size && a.use === b.use && a.available === b.available) // 过滤 HardDisk = HardDisk.filter(item => item.size && item.used && item.available && item.use) diff --git a/model/Pixiv.js b/model/Pixiv.js index 99ddeaa..aa37b33 100644 --- a/model/Pixiv.js +++ b/model/Pixiv.js @@ -4,27 +4,18 @@ import { segment } from 'oicq' import moment from 'moment' import { rankType, MSG } from '../tools/pixiv.js' import request from '../lib/request/request.js' +import { Config } from '../components/index.js' /** API请求错误文案 */ const API_ERROR = '❎ 出错辣,请稍后重试' export default new class Pixiv { constructor () { - this._proxy = '' this.ranktype = rankType this.domain = 'http://api.liaobiao.top/api/pixiv' - this.init() - } - - async init () { - this._proxy = await redis.get('yenai:proxy') - if (!this.proxy) { - await redis.set('yenai:proxy', 'i.pixiv.re') - this._proxy = 'i.pixiv.re' - } } get headers () { - if (this.proxy == 'i.pximg.net') { + if (Config.pixiv.pixivDirectConnection) { return { Host: 'i.pximg.net', Referer: 'https://www.pixiv.net/', @@ -36,12 +27,7 @@ export default new class Pixiv { } get proxy () { - return this._proxy - } - - set proxy (value) { - redis.set('yenai:proxy', value) - this._proxy = value + return Config.pixiv.pixivDirectConnection ? 'i.pximg.net' : Config.pixiv.pixivImageProxy } /** 开始执行文案 */ @@ -262,7 +248,7 @@ export default new class Pixiv { let list = [] for (let i of res.trend_tags) { let { tag, translated_name } = i - let url = i.illust.image_urls.large.replace('i.pximg.net', this.proxy) + let url = i.illust.image_urls.large list.push( [ `Tag:${tag}\n`, @@ -457,6 +443,8 @@ export default new class Pixiv { */ async requestPixivImg (url) { url = url.replace('i.pximg.net', this.proxy) + console.log(this.proxy) + logger.debug(`pixiv getImg URL: ${url}`) return request.proxyRequestImg(url, { headers: this.headers }) }