Skip to content

Commit

Permalink
refactor: 优化ws和加载配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowwarmth committed Dec 31, 2024
1 parent 6ca5a39 commit a2955ef
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,48 @@ import { _path, redis } from "@src/lib/global/global";
export async function initialize() {
await init();

ws.on('READY', (data: any) => logger.info('[READY] 事件接收 :', data));
ws.on('ERROR', (data: any) => logger.error('[ERROR] 事件接收 :', data));
ws.on('READY', (data) => logger.info('[READY] 事件接收 :', data));
ws.on('ERROR', (data) => logger.error('[ERROR] 事件接收 :', data));
ws.on('GUILDS', reloadGuildTree);
ws.on('GUILD_MESSAGES', (data: IntentMessage) => handleMessage(data, "GUILD"));
ws.on('DIRECT_MESSAGE', (data: IntentMessage) => handleMessage(data, "DIRECT"));
}

ws.on('GUILD_MESSAGES', async (data: IntentMessage) => {
if (data.eventType === "MESSAGE_CREATE") {
const msg = new IMessageEx(data.msg, "GUILD");
await execute(msg);
}
});
async function reloadGuildTree() {
try {
logger.info(`重新加载频道树中`);
await loadGuildTree();
logger.info(`频道树加载完毕`);
} catch (err) {
logger.error(`频道树加载失败`, err);
}
}

ws.on("DIRECT_MESSAGE", async (data: IntentMessage) => {
if (data.eventType === 'DIRECT_MESSAGE_CREATE') {
const msg = new IMessageEx(data.msg, "DIRECT");
async function handleMessage(data: IntentMessage, messageType: "DIRECT" | "GUILD") {
if ((messageType === "GUILD" && data.eventType === "MESSAGE_CREATE") ||
(messageType === "DIRECT" && data.eventType === 'DIRECT_MESSAGE_CREATE')) {

const msg = new IMessageEx(data.msg, messageType);

if (messageType === "DIRECT") {
await redis.hSet(`genshin:config:${msg.author.id}`, "guildId", msg.guild_id);
await execute(msg);
}
});

ws.on("GUILDS", async () => {
logger.info(`重新加载频道树中`);
try {
await loadGuildTree();
logger.info(`频道树加载完毕`);
} catch (err) {
logger.error(`频道树加载失败`, err);
}
});

await execute(msg);
}
}

async function execute(msg: IMessageEx) {
try {
await redis.set("lastestMsgId", msg.id, { EX: 4 * 60 });

if (!msg?.content) {
logger.error('检查消息为空,可能是图片和GIF导致的');
if (!msg.content) {
logger.error('消息为空,可能是图片和GIF导致的');
return;
}

msg.content = msg.content.trim().replace(/^\//, "#");

const opt = await findOpts(msg);
if (!opt || opt.directory === "err") return;

Expand All @@ -59,16 +62,16 @@ async function execute(msg: IMessageEx) {

logger.debug(`插件路径: ${pluginURL}`);

try {
const plugin = await import(pluginURL);
if (typeof plugin[opt.fnc] === "function") {
await plugin[opt.fnc](msg);
} else {
await import(pluginURL)
.then((plugin) => {
if (typeof plugin[opt.fnc] === "function") {
return plugin[opt.fnc](msg);
}
logger.error(`未找到函数 ${opt.fnc}() 在 "${pluginURL}"`);
}
} catch (importErr) {
logger.error('插件导入失败:', importErr);
}
})
.catch((importErr) => {
logger.error('插件导入失败:', importErr);
});
} catch (err) {
logger.error('执行过程中发生错误:', err);
}
Expand Down

0 comments on commit a2955ef

Please sign in to comment.