refactor: streamline API request handling and enhance message processing

This commit is contained in:
fzzin
2025-04-08 18:48:16 +02:00
parent 1e8f195714
commit 79750ed5cc
4 changed files with 157 additions and 110 deletions

View File

@@ -1,69 +1,22 @@
import { Bot, Message } from "npm:discordeno@18.0.1";
import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api/api.ts";
import { Bot, DiscordEmbed, Embed, Message } from "npm:discordeno@18.0.1";
import { defaultString } from "@root/defaultString.ts";
import { logMessage } from "@root/logging.ts";
import { drop, requestWorker } from "@root/plugins/yandere/plugin.ts";
export async function yandereMessageHandler(bot: Bot, message: Message) {
const command = message.content.split(" ").slice(1).join(" ");
const args = message.content.split(" ").slice(1);
switch (command) {
case `drop`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
await refresh();
bot.helpers.sendMessage(message.channelId, {
content: defaultString(await dropYandere()),
});
}
break;
case `drop 5`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
await refresh();
bot.helpers.sendMessage(message.channelId, {
content: defaultString(await dropYandere5()),
});
}
break;
case `page`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
if (args[0] === undefined) {
bot.helpers.sendMessage(message.channelId, {
content: "Please provide a page number",
});
} else if (isNaN(parseInt(args[0]))) {
bot.helpers.sendMessage(message.channelId, {
content: "Please provide a valid number",
});
} else {
await setPage(parseInt(args[0]));
bot.helpers.sendMessage(message.channelId, {
content: "Page set to " + args[0],
});
}
}
break;
case `getpage`:
logMessage(message);
if (
message.channelId === 754338073101205524n ||
message.guildId === undefined
) {
bot.helpers.sendMessage(message.channelId, {
content: "Page is " + getPage(),
});
}
break;
if (command === "drop") {
bot.helpers.sendMessage(message.channelId, {
embeds: [await drop()]
})
} else if (command.startsWith("drop [") && command.endsWith("]")) {
logMessage(message);
const generatedEmbeds: Embed[] = await requestWorker(command);
for (const embed of generatedEmbeds) {
bot.helpers.sendMessage(message.channelId, {
embeds: [embed],
});
}
}
}