refactor: Extract UI component creation into new view files for lootdrop, trade, item wizard, and enrollment.

This commit is contained in:
syntaxbullet
2025-12-24 11:36:19 +01:00
parent eaf97572a4
commit 5c36b9be25
10 changed files with 341 additions and 244 deletions

View File

@@ -1,6 +1,6 @@
import { ButtonInteraction, MessageFlags } from "discord.js";
import { config } from "@/lib/config";
import { createErrorEmbed } from "@/lib/embeds";
import { getEnrollmentErrorEmbed, getEnrollmentSuccessMessage } from "./enrollment.view";
import { classService } from "@modules/class/class.service";
import { userService } from "@modules/user/user.service";
import { sendWebhookMessage } from "@/lib/webhookUtils";
@@ -15,7 +15,7 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
if (!studentRole || !visitorRole) {
await interaction.reply({
embeds: [createErrorEmbed("No student or visitor role configured for enrollment.", "Configuration Error")],
...getEnrollmentErrorEmbed("No student or visitor role configured for enrollment.", "Configuration Error"),
flags: MessageFlags.Ephemeral
});
return;
@@ -28,7 +28,7 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
// Check DB enrollment
if (user.class) {
await interaction.reply({
embeds: [createErrorEmbed("You are already enrolled in a class.", "Enrollment Failed")],
...getEnrollmentErrorEmbed("You are already enrolled in a class.", "Enrollment Failed"),
flags: MessageFlags.Ephemeral
});
return;
@@ -39,7 +39,7 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
// Check Discord role enrollment (Double safety)
if (member.roles.cache.has(studentRole)) {
await interaction.reply({
embeds: [createErrorEmbed("You already have the student role.", "Enrollment Failed")],
...getEnrollmentErrorEmbed("You already have the student role.", "Enrollment Failed"),
flags: MessageFlags.Ephemeral
});
return;
@@ -51,7 +51,7 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
if (validClasses.length === 0) {
await interaction.reply({
embeds: [createErrorEmbed("No classes with specified roles found in database.", "Configuration Error")],
...getEnrollmentErrorEmbed("No classes with specified roles found in database.", "Configuration Error"),
flags: MessageFlags.Ephemeral
});
return;
@@ -65,7 +65,7 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
const classRole = interaction.guild.roles.cache.get(classRoleId);
if (!classRole) {
await interaction.reply({
embeds: [createErrorEmbed(`The configured role ID \`${classRoleId}\` for class **${selectedClass.name}** does not exist in this server.`, "Configuration Error")],
...getEnrollmentErrorEmbed(`The configured role ID \`${classRoleId}\` for class **${selectedClass.name}** does not exist in this server.`, "Configuration Error"),
flags: MessageFlags.Ephemeral
});
return;
@@ -81,7 +81,7 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
await classService.assignClass(user.id.toString(), selectedClass.id);
await interaction.reply({
content: `🎉 You have been successfully enrolled! You received the **${classRole.name}** role.`,
...getEnrollmentSuccessMessage(classRole.name),
flags: MessageFlags.Ephemeral
});
@@ -113,8 +113,8 @@ export async function handleEnrollmentInteraction(interaction: ButtonInteraction
} catch (error) {
console.error("Enrollment error:", error);
await interaction.reply({
embeds: [createErrorEmbed("An unexpected error occurred during enrollment. Please contact an administrator.", "System Error")],
...getEnrollmentErrorEmbed("An unexpected error occurred during enrollment. Please contact an administrator.", "System Error"),
flags: MessageFlags.Ephemeral
});
}
}
}

View File

@@ -0,0 +1,12 @@
import { createErrorEmbed } from "@/lib/embeds";
export function getEnrollmentErrorEmbed(message: string, title: string = "Enrollment Failed") {
const embed = createErrorEmbed(message, title);
return { embeds: [embed] };
}
export function getEnrollmentSuccessMessage(roleName: string) {
return {
content: `🎉 You have been successfully enrolled! You received the **${roleName}** role.`
};
}