From 550f7e0d9916ef783600bbf70c054d40db5060cd Mon Sep 17 00:00:00 2001 From: "yysforget@163.com" Date: Tue, 9 Jul 2024 17:01:42 +0800 Subject: [PATCH] =?UTF-8?q?add:=E7=BB=9D=E5=8C=BA=E9=9B=B6=E6=94=BB?= =?UTF-8?q?=E7=95=A5=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/guide.js | 110 ++++++++++++++++++++++++++++++++++++++++++ lib/convert/char.js | 8 +-- lib/convert/weapon.js | 2 +- 3 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 apps/guide.js diff --git a/apps/guide.js b/apps/guide.js new file mode 100644 index 0000000..242f952 --- /dev/null +++ b/apps/guide.js @@ -0,0 +1,110 @@ +import {ZZZPlugin} from '../lib/plugin.js'; +import {rulePrefix} from '../lib/common.js'; +import {atlasToName} from '../lib/convert/char.js' +import path from 'path'; +import fs from 'fs'; +import {imageResourcesPath} from '../lib/path.js' +import fetch from 'node-fetch' +import lodash from 'lodash' + +const ZZZ_GUIDES_PATH = path.join(imageResourcesPath, 'guides'); + +export class Guide extends ZZZPlugin { + constructor() { + super({ + name: '[ZZZ-Plugin]Guide', + dsc: '#zzz角色攻略', + event: 'message', + priority: 100, + rule: [ + { + reg: `${rulePrefix}角色攻略\\S+$`, + fnc: 'Guide', + }, + ], + }); + + this.url = 'https://bbs-api.mihoyo.com/post/wapi/getPostFullInCollection?&gids=8&collection_id=' + this.collection_id = [ + [], + // 来源:新艾利都快讯 + [2712859], + ] + } + async Guide() { + let reg = new RegExp(`${rulePrefix}角色攻略(\\S+)$`); + let [,,,, name] = this.e.msg.match(reg); + console.log(name) + let id = atlasToName(name); + if (!id) { + await this.reply('该角色不存在'); + return; + } + const filename = `role_guide_${name}.png`; + const guidePath = path.join(ZZZ_GUIDES_PATH, filename); + if (fs.existsSync(guidePath)) { + await this.e.reply(segment.image(`file://${guidePath}`)); + return; + } + //目前攻略较少,暂为1 + if (await this.getImg(name, 1)) { + await this.e.reply(segment.image(`file://${guidePath}`)) + } + + } + + /** 下载攻略图 */ + async getImg (name, group) { + let msyRes = [] + this.collection_id[group].forEach((id) => msyRes.push(this.getData(this.url + id))) + + try { + msyRes = await Promise.all(msyRes) + } catch (error) { + this.e.reply('暂无攻略数据,请稍后再试') + console.log(`米游社接口报错:${error}}`) + return false + } + + let posts = lodash.flatten(lodash.map(msyRes, (item) => item.data.posts)) + let url + for (let val of posts) { + if (val.post.subject.includes(name)) { + let max = 0 + val.image_list.forEach((v, i) => { + if (Number(v.size) >= Number(val.image_list[max].size)) max = i + }) + url = val.image_list[max].url + break + } + } + if (!url) { + this.e.reply(`暂无${name}攻略(${this.source[group - 1]})\n请尝试其他的攻略来源查询`) + return false + } + console.log(`${this.e.logFnc} 下载${name}攻略图`) + + const download = await fetch(url); + const arrayBuffer = await download.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + const filename = `role_guide_${name}.png`; + const avatarPath = path.join(ZZZ_GUIDES_PATH, filename); + if (!fs.existsSync(ZZZ_GUIDES_PATH)) { + fs.mkdirSync(ZZZ_GUIDES_PATH, { recursive: true }); + } + fs.writeFileSync(avatarPath , buffer); + + console.log(`${this.e.logFnc} 下载${name}攻略成功`) + + return true + } + + /** 获取数据 */ + async getData (url) { + let response = await fetch(url, { method: 'get' }) + if (!response.ok) { + return false + } + return await response.json() + } +} diff --git a/lib/convert/char.js b/lib/convert/char.js index f7bbb80..fd40a1d 100644 --- a/lib/convert/char.js +++ b/lib/convert/char.js @@ -1,5 +1,5 @@ import settings from '../settings.js'; -import PartnerId2SpriteId from '../../resources/map/PartnerId2SpriteId.json?json'; +import PartnerId2SpriteId from '../../resources/map/PartnerId2SpriteId.json' assert { type: "json" }; /** * @@ -63,13 +63,13 @@ export const atlasToName = _atlas => { }; /** - * @param {string} atlas + * @param {string} _atlas * @returns string | null */ -export const atlasToSprite = atlas => { +export const atlasToSprite = _atlas => { const atlas = settings.getConfig('atlas'); for (const [_id, data] of Object.entries(atlas)) { - if (data.includes(atlas)) return data['sprite']; + if (data.includes(_atlas)) return data['sprite']; } return null; }; diff --git a/lib/convert/weapon.js b/lib/convert/weapon.js index 22b026c..49fab8b 100644 --- a/lib/convert/weapon.js +++ b/lib/convert/weapon.js @@ -1,4 +1,4 @@ -import WeaponId2Sprite from '../../resources/map/WeaponId2Sprite.json?json'; +import WeaponId2Sprite from '../../resources/map/WeaponId2Sprite.json' assert { type: "json" }; /** * @param {string} id