54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { Events, MessageFlags } from "discord.js";
|
|
import { KyokoClient } from "@/lib/BotClient";
|
|
import { userService } from "@/modules/user/user.service";
|
|
import { createErrorEmbed } from "@lib/embeds";
|
|
import type { Event } from "@lib/types";
|
|
|
|
const event: Event<Events.InteractionCreate> = {
|
|
name: Events.InteractionCreate,
|
|
execute: async (interaction) => {
|
|
// Handle Trade Interactions
|
|
if (interaction.isButton() || interaction.isStringSelectMenu() || interaction.isModalSubmit()) {
|
|
if (interaction.customId.startsWith("trade_") || interaction.customId === "amount") {
|
|
await import("@/modules/trade/trade.interaction").then(m => m.handleTradeInteraction(interaction));
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!interaction.isChatInputCommand()) return;
|
|
|
|
const command = KyokoClient.commands.get(interaction.commandName);
|
|
|
|
if (!command) {
|
|
console.error(`No command matching ${interaction.commandName} was found.`);
|
|
return;
|
|
}
|
|
|
|
|
|
// Ensure user exists in database
|
|
try {
|
|
const user = await userService.getUserById(interaction.user.id);
|
|
if (!user) {
|
|
console.log(`🆕 Creating new user entry for ${interaction.user.tag}`);
|
|
await userService.createUser(interaction.user.id, interaction.user.username);
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to check/create user:", error);
|
|
}
|
|
|
|
try {
|
|
await command.execute(interaction);
|
|
} catch (error) {
|
|
console.error(error);
|
|
const errorEmbed = createErrorEmbed('There was an error while executing this command!');
|
|
if (interaction.replied || interaction.deferred) {
|
|
await interaction.followUp({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral });
|
|
} else {
|
|
await interaction.reply({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral });
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
export default event;
|