Skip to content

Commit

Permalink
fix:优化插件整体性能
Browse files Browse the repository at this point in the history
  • Loading branch information
CikeyQi committed Nov 28, 2024
1 parent 471d48d commit 510dbb7
Show file tree
Hide file tree
Showing 31 changed files with 485 additions and 450 deletions.
10 changes: 5 additions & 5 deletions apps/Bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ export class Bind extends plugin {
return await e.reply(`登录失败!原因:${gameData.msg}\n使用[~登录帮助]查看登录方法!`);
}

const userConfig = Config.getUserConfig(e.user_id);
const userConfig = Config.getUserData(e.user_id);
const userData = { token, userId: gameData.data.userId, serverId: gameData.data.serverId, roleId: gameData.data.roleId };
const userIndex = userConfig.findIndex(item => item.userId === gameData.data.userId);
userIndex !== -1 ? (userConfig[userIndex] = userData) : userConfig.push(userData);
await redis.set(`Yunzai:waves:bind:${e.user_id}`, gameData.data.roleId);

Config.setUserConfig(e.user_id, userConfig);
Config.setUserData(e.user_id, userConfig);
return await e.reply(`${gameData.data.roleName}(${gameData.data.roleId}) 登录成功!`, true);
}
async unLogin(e) {
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserConfig(e.user_id);
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserData(e.user_id);

if (!accountList || !accountList.length) {
return await e.reply('当前没有登录任何账号,请使用[~登录]进行登录');
Expand All @@ -112,13 +112,13 @@ export class Bind extends plugin {
let index = accountList.findIndex(item => item.roleId == roleId);
accountList.splice(index, 1);
await e.reply(`已删除账号 ${roleId}`);
Config.setUserConfig(e.user_id, accountList);
Config.setUserData(e.user_id, accountList);
}
return true;
}

async getToken(e) {
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserConfig(e.user_id);
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserData(e.user_id);

if (!accountList || !accountList.length) {
return await e.reply('当前没有登录任何账号,请使用[~登录]进行登录');
Expand Down
4 changes: 2 additions & 2 deletions apps/Calabash.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Calabash extends plugin {
async calabash(e) {

if (e.at) e.user_id = e.at;
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserConfig(e.user_id);
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserData(e.user_id);
const waves = new Waves();

const [, roleId] = e.msg.match(this.rule[0].reg);
Expand Down Expand Up @@ -89,7 +89,7 @@ export class Calabash extends plugin {

if (deleteroleId.length) {
const newAccountList = accountList.filter(account => !deleteroleId.includes(account.roleId));
Config.setUserConfig(e.user_id, newAccountList);
Config.setUserData(e.user_id, newAccountList);
}

if (data.length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions apps/Challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Challenge extends plugin {
async challenge(e) {

if (e.at) e.user_id = e.at;
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserConfig(e.user_id);
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserData(e.user_id);
const waves = new Waves();

const [, roleId] = e.msg.match(this.rule[0].reg);
Expand Down Expand Up @@ -103,7 +103,7 @@ export class Challenge extends plugin {

if (deleteroleId.length) {
const newAccountList = accountList.filter(account => !deleteroleId.includes(account.roleId));
Config.setUserConfig(e.user_id, newAccountList);
Config.setUserData(e.user_id, newAccountList);
}

if (data.length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions apps/Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Character extends plugin {
async character(e) {

if (e.at) e.user_id = e.at;
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserConfig(e.user_id);
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserData(e.user_id);
const waves = new Waves();

const [, message, roleId] = e.msg.match(this.rule[0].reg);
Expand Down Expand Up @@ -135,7 +135,7 @@ export class Character extends plugin {

if (deleteroleId.length) {
const newAccountList = accountList.filter(account => !deleteroleId.includes(account.roleId));
Config.setUserConfig(e.user_id, newAccountList);
Config.setUserData(e.user_id, newAccountList);
}

imgList = [...new Set(imgList)];
Expand Down
4 changes: 2 additions & 2 deletions apps/Explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Explore extends plugin {
async explore(e) {

if (e.at) e.user_id = e.at;
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserConfig(e.user_id);
let accountList = JSON.parse(await redis.get(`Yunzai:waves:users:${e.user_id}`)) || await Config.getUserData(e.user_id);
const waves = new Waves();

const [, roleId] = e.msg.match(this.rule[0].reg);
Expand Down Expand Up @@ -83,7 +83,7 @@ export class Explore extends plugin {

if (deleteroleId.length) {
let newAccountList = accountList.filter(account => !deleteroleId.includes(account.roleId));
Config.setUserConfig(e.user_id, newAccountList);
Config.setUserData(e.user_id, newAccountList);
}

if (data.length === 1) {
Expand Down
10 changes: 5 additions & 5 deletions apps/Gacha.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class Gacha extends plugin {
}

fs.writeFileSync(`${_path}/data/wavesGacha/${jsonData.playerId}_Export.json`, JSON.stringify(json, null, 2));
logger.info(`[Waves-Plugin] 已写入本地文件 ${jsonData.playerId}_Export.json`)
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.green(`已写入本地文件 ${jsonData.playerId}_Export.json`));

return true;
}
Expand Down Expand Up @@ -249,9 +249,9 @@ export class Gacha extends plugin {

try {
await Bot.download(fileUrl, path);
} catch (err) {
logger.error(`文件下载错误:${logger.red(err.stack)}`);
await this.reply(`导入抽卡记录失败:${err.message || '未知错误'}`, true);
} catch (error) {
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.red(`文件下载错误:${error.stack}`));
await this.reply(`导入抽卡记录失败:${error.message || '未知错误'}`, true);
return true;
}

Expand Down Expand Up @@ -289,7 +289,7 @@ export class Gacha extends plugin {

await this.e.reply(`导入UID为 ${uid} 的抽卡记录成功,共计${json.list.length}条记录`);
await redis.set(`Yunzai:waves:gachaHistory:${this.e.user_id}`, uid);
logger.info(`[Waves-Plugin] 已写入本地文件 ${uid}_Export.json`);
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.green(`已写入本地文件 ${uid}_Export.json`));

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/Guide.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class Guide extends plugin {

if (!entryData.status) {
if (/^(~|~|鸣潮)/.test(e.msg)) {
logger.info(`[Waves-Plugin] 尝试搜索图鉴:${message}`)
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.yellow(`尝试搜索图鉴:${message}`));
let result = await wiki.search(message)
if (!result.status) {
logger.warn(`[Waves-Plugin] 未能搜索到图鉴内容:${message}`)
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.red(`未能搜索到图鉴内容:${message}`));
await e.reply(`未能获取到${message}的图鉴,请检查输入是否正确`)
return false
} else {
Expand Down
16 changes: 10 additions & 6 deletions apps/Manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Waves from "../components/Code.js";
import { _path } from '../model/path.js';
import Render from '../components/Render.js';
import fs from 'fs/promises';
import pLimit from 'p-limit';
import YAML from 'yaml';

export class Manage extends plugin {
Expand Down Expand Up @@ -40,7 +41,10 @@ export class Manage extends plugin {
return YAML.parse(content);
}));

const results = await Promise.all(userCounts.flat().map(user => waves.isAvailable(user.token, true)));
const limit = pLimit(Config.getConfig().limit);
const results = await Promise.all(
userCounts.flat().map(user => limit(() => waves.isAvailable(user.token, true)))
)

const available = results.filter(Boolean).length;
const expired = results.length - available;
Expand All @@ -60,8 +64,8 @@ export class Manage extends plugin {
}, { e, retType: 'base64' });

await e.reply(imageCard);
} catch (err) {
logger.error('计算总用户数时出现错误', err);
} catch (error) {
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.red(`计算总用户数时出现错误:\n${error}`));
await e.reply('[Waves-Plugin] 账号总数\n计算总用户数时出现错误,请检查日志');
}
}
Expand All @@ -86,12 +90,12 @@ export class Manage extends plugin {
return valid ? user : null;
}));

Config.setUserConfig(file.slice(0, -5), validUsers.filter(Boolean));
Config.setUserData(file.slice(0, -5), validUsers.filter(Boolean));
}));

await e.reply(`[Waves-Plugin] 删除失效账号\n删除了 ${deleted} 个失效账号`);
} catch (err) {
logger.error('删除失效账号时出现错误', err);
} catch (error) {
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.red(`删除失效账号时出现错误:\n${error}`));
await e.reply('[Waves-Plugin] 删除失效账号\n删除失效账号时出现错误,请检查日志');
}
return true;
Expand Down
47 changes: 25 additions & 22 deletions apps/News.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import plugin from '../../../lib/plugins/plugin.js'
import Waves from "../components/Code.js";
import Config from "../components/Config.js";
import pLimit from 'p-limit';

export class News extends plugin {
constructor() {
Expand Down Expand Up @@ -43,7 +44,7 @@ export class News extends plugin {
}

async autoNews() {
const { waves_auto_news_list: autoPushList } = Config.getConfig();
const { waves_auto_news_list: autoPushList } = Config.getUserConfig();

if (!autoPushList.length) {
return true;
Expand All @@ -53,36 +54,38 @@ export class News extends plugin {
const newsData = await waves.getEventList();

if (!newsData.status) {
logger.info(newsData.msg);
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.red(newsData.msg));
return true;
}

const postId = newsData.data.list[0].postId;
if (postId != await redis.get(`Yunzai:waves:news`)) {
await Promise.all(autoPushList.map(async (user) => {
const { botId, groupId, userId } = user;
const limit = pLimit(Config.getConfig().limit);
await Promise.all(autoPushList.map(user =>
limit(async () => {
const { botId, groupId, userId } = user;

let isGroup = !!groupId;
let id = isGroup ? groupId : userId;
let isGroup = !!groupId;
let id = isGroup ? groupId : userId;

if (isGroup) {
await Bot[botId]?.pickGroup(id).sendMsg([
segment.image(newsData.data.list[0].coverUrl),
`${newsData.data.list[0].postTitle}\nhttps://www.kurobbs.com/mc/post/${newsData.data.list[0].postId}\n\n${new Date(newsData.data.list[0].publishTime).toLocaleString()}`
]);
} else {
await Bot[botId]?.pickUser(id).sendMsg([
segment.image(newsData.data.list[0].coverUrl),
`${newsData.data.list[0].postTitle}\nhttps://www.kurobbs.com/mc/post/${newsData.data.list[0].postId}\n\n${new Date(newsData.data.list[0].publishTime).toLocaleString()}`
]);
}
if (isGroup) {
await Bot[botId]?.pickGroup(id).sendMsg([
segment.image(newsData.data.list[0].coverUrl),
`${newsData.data.list[0].postTitle}\nhttps://www.kurobbs.com/mc/post/${newsData.data.list[0].postId}\n\n${new Date(newsData.data.list[0].publishTime).toLocaleString()}`
]);
} else {
await Bot[botId]?.pickUser(id).sendMsg([
segment.image(newsData.data.list[0].coverUrl),
`${newsData.data.list[0].postTitle}\nhttps://www.kurobbs.com/mc/post/${newsData.data.list[0].postId}\n\n${new Date(newsData.data.list[0].publishTime).toLocaleString()}`
]);
}

redis.set(`Yunzai:waves:news`, postId);
logger.info(`[Waves-Plugin] 新闻推送: 已推送最新公告`);
return true;
}));
redis.set(`Yunzai:waves:news`, postId);
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.green(`已推送最新公告`));
return true;
})));
} else {
logger.info(`[Waves-Plugin] 新闻推送: 未获取到新公告`);
logger.mark(logger.blue('[WAVES PLUGIN]'), logger.yellow(`未获取到新公告`));
return true;
}
}
Expand Down
Loading

0 comments on commit 510dbb7

Please sign in to comment.