Skip to content

Commit

Permalink
improvement: 米游社公告代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowwarmth committed Jan 17, 2025
1 parent e841869 commit 02baa65
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 106 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ config/pm2
config/config.json
data
pnpm-lock.yaml
dist
dist
logs
86 changes: 37 additions & 49 deletions plugins/mihoyo/apps/mysNew.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ function getTypeAndName(content) {
}
export async function handleNewsContent(msg, gid, type, page) {
const pagesData = await miGetNewsList(gid, type);
if (!pagesData)
return;
if (!pagesData)return;
if (page <= 0 || page > pagesData.list.length) {
msg.sendMsgEx({ content: "目前只查前10条最新的公告,请输入1-10之间的整数。" });
return true;
}
const postFull = await miGetPostFull(gid, pagesData.list[page - 1].post.post_id);
if (!postFull)
return;
if (!postFull)return;
const data = await detalData(postFull.post);
render({
const savePath = await render({
app: "mys",
type: "mysNew",
imgType: "jpeg",
Expand All @@ -71,25 +69,22 @@ export async function handleNewsContent(msg, gid, type, page) {
dataConent: data.post.content,
data,
}
}).then((savePath) => {
if (savePath)
msg.sendMsgEx({ content: data.post.subject, imagePath: savePath });
logger.mark(chalk.blueBright(`[${gameIds[gid]}公告] newsContentBBS/mysNew.js`));
}).catch((err) => {
logger.error(err);
});
}

if (savePath) {
msg.sendMsgEx({ content: data.post.subject, imagePath: savePath });
}
logger.mark(chalk.blueBright(`[${gameIds[gid]}公告] newsContentBBS/mysNew.js`));
}
export async function handleNewsList(msg, gid, gameName, type, typeName) {
const data = await miGetNewsList(gid, type);
if (!data)
return;
if (!data)return;
const datas = data.list;
if (datas.length === 0)
return true;
if (datas.length === 0)return true;
datas.forEach((element) => {
element.post.created_at = new Date(element.post.created_at * 1000).toLocaleString();
});
await render({
const savePath = await render({
app: "mys",
type: "mysNewList",
imgType: "jpeg",
Expand All @@ -99,13 +94,12 @@ export async function handleNewsList(msg, gid, gameName, type, typeName) {
typeName,
gameName
}
}).then((savePath) => {
if (savePath)
msg.sendMsgEx({ imagePath: savePath });
logger.mark(chalk.blueBright(`[${gameName}${typeName}列表] newListBBS/mysNew.js`));
}).catch((err) => {
logger.error(err);
});
})

if (savePath){
msg.sendMsgEx({ imagePath: savePath });
}
logger.mark(chalk.blueBright(`[${gameName}${typeName}列表] newListBBS/mysNew.js`));
}
export async function newsContentBBS(msg) {
logger.mark(`[${msg.guild_name}-${msg.channel_name}(${msg.guild_id}-${msg.channel_id}), ${msg.author.username}(${msg.author.id})][${msg.content}]`);
Expand Down Expand Up @@ -145,18 +139,15 @@ export async function changePushTask(msg) {
export async function taskPushNews(gamePrefix, getNewsList, getPostFull, logMessage) {
const ignoreReg = getIgnoreReg(gamePrefix);
const msgId = await redis.get("lastestMsgId");
if (!msgId)
return;
if (!msgId)return;
const sendChannels = [];
const _newsPushChannels = await redis.hGetAll(`config:${gamePrefix}newsPush`).catch(err => { logger.error(err); });
if (!_newsPushChannels)
return;
if (!_newsPushChannels)return;
for (const channel in _newsPushChannels) {
if (_newsPushChannels[channel] == "true")
sendChannels.push(channel);
}
if (sendChannels.length == 0)
return;
if (sendChannels.length == 0)return;
logger.debug(logMessage);
const pagesData = [{ type: "公告", list: (await getNewsList(1))?.list }, { type: "资讯", list: (await getNewsList(3))?.list }];
const postIds = [];
Expand All @@ -179,7 +170,7 @@ export async function taskPushNews(gamePrefix, getNewsList, getPostFull, logMess
if (!postFull)
return;
const data = await detalData(postFull.post);
await render({
const savePath = await render({
app: "mys",
type: "mysNew",
imgType: "jpeg",
Expand All @@ -188,27 +179,24 @@ export async function taskPushNews(gamePrefix, getNewsList, getPostFull, logMess
dataConent: data.post.content,
data,
}
}).then(savePath => {
if (savePath) {
const _sendQueue = [];
for (const sendChannel of sendChannels) {
_sendQueue.push(sendImage({
msgId,
content: data.post.subject,
imagePath: savePath,
channelId: sendChannel,
messageType: "GUILD"
}));
}
logger.mark(chalk.blueBright(`[${logMessage}] taskPushNews/mysNew.js`));
return Promise.all(_sendQueue).catch(err => {
logger.error(err);
});
}
}).catch(err => {
})
if (savePath) {
const _sendQueue = [];
for (const sendChannel of sendChannels) {
_sendQueue.push(sendImage({
msgId,
content: data.post.subject,
imagePath: savePath,
channelId: sendChannel,
messageType: "GUILD"
}));
}
logger.mark(chalk.blueBright(`[${logMessage}] taskPushNews/mysNew.js`));
return Promise.all(_sendQueue).catch(err => {
logger.error(err);
});
}
}
logger.debug(`${logMessage}检查完成`);
}
export async function bbbtaskPushNews() {
Expand Down
21 changes: 9 additions & 12 deletions plugins/mihoyo/models/mysNew.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ const headers = {
Referer: 'https://www.miyoushe.com',
origin: 'https://www.miyoushe.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
};
}

async function fetchMihoyoAPI(url) {
try {
const res = await fetch(url, { method: "GET", headers });
const json = await res.json();
if (json.data)
return json.data;
else
throw new Error("not found data");
}
catch (err) {
logger.error(err);
return null;
const res = await fetch(url, { method: "GET", headers });
const json = await res.json();
if (json.data){
return json.data;
} else {
throw new Error("not found data");
}
}

async function miGetNewsList(gid, type, pageSize = 10) {
const url = `https://bbs-api-static.miyoushe.com/painter/wapi/getNewsList?gids=${gid}&page_size=${pageSize}&type=${type}`;
return fetchMihoyoAPI(url);
Expand Down
44 changes: 0 additions & 44 deletions tsconfig.json

This file was deleted.

0 comments on commit 02baa65

Please sign in to comment.