- Refactored user.service.ts to use withTransaction() helper - Added 14 comprehensive unit tests for user.service.ts - Removed duplicate user creation in interactionCreate.ts - Improved type safety in interaction.routes.ts
71 lines
2.6 KiB
TypeScript
71 lines
2.6 KiB
TypeScript
import { Events, MessageFlags } from "discord.js";
|
|
import { AuroraClient } 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()) {
|
|
const { interactionRoutes } = await import("@lib/interaction.routes");
|
|
|
|
for (const route of interactionRoutes) {
|
|
if (route.predicate(interaction)) {
|
|
const module = await route.handler();
|
|
const handlerMethod = module[route.method];
|
|
if (typeof handlerMethod === 'function') {
|
|
await handlerMethod(interaction);
|
|
return;
|
|
} else {
|
|
console.error(`Handler method ${route.method} not found in module`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (interaction.isAutocomplete()) {
|
|
const command = AuroraClient.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 = AuroraClient.commands.get(interaction.commandName);
|
|
|
|
if (!command) {
|
|
console.error(`No command matching ${interaction.commandName} was found.`);
|
|
return;
|
|
}
|
|
|
|
|
|
// Ensure user exists in database
|
|
try {
|
|
await userService.getOrCreateUser(interaction.user.id, interaction.user.username);
|
|
} catch (error) {
|
|
console.error("Failed to ensure user exists:", 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;
|