Files
lbf-bot/apps/discord-bot/src/modes/bot.ts
Pihkaal 4059ea1ddf
All checks were successful
Build and Deploy / typecheck (push) Successful in 27s
Build and Deploy / build (push) Successful in 28s
Build and Deploy / deploy (push) Successful in 2s
chore(discord-bot): remove all migration related code
2026-05-30 22:13:13 +02:00

68 lines
3.1 KiB
TypeScript

import type { Client, Interaction, OmitPartialGroupDMChannel, Message } from "discord.js";
import { logger } from "@lbf-bot/utils";
import { env } from "~/env";
import { questCheckCron } from "~/quests";
import { trackingCron } from "~/tracking";
import { commands } from "~/commands";
import { handleReportButton, handleReportModal, handleEditButton, handleDeleteButton, handleEditModal, REPORT_BUTTON_ID, REPORT_MODAL_ID, REPORT_EDIT_BUTTON_PREFIX, REPORT_DELETE_BUTTON_PREFIX, REPORT_EDIT_MODAL_PREFIX } from "~/reporting";
const onReady = async (client: Client<true>) => {
logger.info(`Client ready`);
logger.info(`Connected as @${client.user.username}`);
await questCheckCron(client);
setInterval(() => void questCheckCron(client), env.WOV_FETCH_INTERVAL);
await trackingCron(client);
setInterval(() => void trackingCron(client), env.WOV_TRACKING_INTERVAL);
};
const onMessage = async (message: OmitPartialGroupDMChannel<Message>) => {
if (message.author.bot) return;
const parts = message.content.trim().split(/\s+/);
if (parts[0]?.toLowerCase() === "lbf") {
const [commandName, ...args] = parts.slice(1);
const command = commands[commandName];
if (!command) return;
const child = logger.child(`cmd:${commandName}${args.length > 0 ? " " : ""}${args.join(" ")}`);
try {
const start = Date.now();
await command.handler(message, args);
child.info(`Done in ${(Date.now() - start).toFixed(2)}ms`);
} catch (error: unknown) {
child.error("Failed:", error);
}
}
};
const onInteraction = async (interaction: Interaction, client: Client) => {
if (interaction.isButton()) {
if (interaction.customId === REPORT_BUTTON_ID) {
await handleReportButton(interaction);
} else if (interaction.customId.startsWith(`${REPORT_EDIT_BUTTON_PREFIX}:`)) {
const reportId = interaction.customId.slice(REPORT_EDIT_BUTTON_PREFIX.length + 1);
await handleEditButton(interaction, reportId);
} else if (interaction.customId.startsWith(`${REPORT_DELETE_BUTTON_PREFIX}:`)) {
const reportId = interaction.customId.slice(REPORT_DELETE_BUTTON_PREFIX.length + 1);
await handleDeleteButton(interaction, reportId);
}
} else if (interaction.isModalSubmit()) {
if (interaction.customId === REPORT_MODAL_ID) {
await handleReportModal(interaction, client);
} else if (interaction.customId.startsWith(`${REPORT_EDIT_MODAL_PREFIX}:`)) {
const rest = interaction.customId.slice(REPORT_EDIT_MODAL_PREFIX.length + 1);
const [reportId, channelId, messageId] = rest.split(":");
await handleEditModal(interaction, client, reportId, channelId, messageId);
}
}
};
export const setupBotMode = (client: Client) => {
client.on("clientReady", (client) => { void onReady(client); });
client.on("messageCreate", (message) => { void onMessage(message); });
client.on("interactionCreate", (interaction) => { void onInteraction(interaction, client); });
};