Files
discord-rpg-concept/src/events/interactionCreate.ts

73 lines
2.9 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.customId.startsWith("shop_buy_") && interaction.isButton()) {
await import("@/modules/economy/shop.interaction").then(m => m.handleShopInteraction(interaction));
return;
}
if (interaction.customId.startsWith("lootdrop_") && interaction.isButton()) {
await import("@/modules/economy/lootdrop.interaction").then(m => m.handleLootdropInteraction(interaction));
return;
}
}
if (interaction.isAutocomplete()) {
const command = KyokoClient.commands.get(interaction.commandName);
if (!command || !command.autocomplete) return;
try {
await command.autocomplete(interaction);
} catch (error) {
console.error(`Error handling autocomplete for ${interaction.commandName}:`, error);
}
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;