72 lines
3.1 KiB
TypeScript
72 lines
3.1 KiB
TypeScript
import type { Client, 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: Parameters<Parameters<Client["on"]>[1]>[0], 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); });
|
|
};
|