From 0dea266a6d97dded311a8a19a1ea26b5ec455d12 Mon Sep 17 00:00:00 2001 From: syntaxbullet Date: Mon, 22 Dec 2025 12:59:46 +0100 Subject: [PATCH] feat(commands): improve error feedback for economy and admin commands --- src/commands/admin/listing.ts | 15 ++++++++++----- src/commands/economy/daily.ts | 22 ++++++++++++---------- src/commands/economy/exam.ts | 9 +++++++-- src/commands/economy/pay.ts | 23 +++++++++++++---------- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/commands/admin/listing.ts b/src/commands/admin/listing.ts index 361f5b9..b3bd82a 100644 --- a/src/commands/admin/listing.ts +++ b/src/commands/admin/listing.ts @@ -10,7 +10,8 @@ import { MessageFlags } from "discord.js"; import { inventoryService } from "@/modules/inventory/inventory.service"; -import { createErrorEmbed, createWarningEmbed } from "@lib/embeds"; +import { createSuccessEmbed, createErrorEmbed } from "@lib/embeds"; +import { UserError } from "@/lib/errors"; import { items } from "@/db/schema"; import { ilike, isNotNull, and } from "drizzle-orm"; import { DrizzleClient } from "@/lib/DrizzleClient"; @@ -49,7 +50,7 @@ export const listing = createCommand({ } if (!item.price) { - await interaction.editReply({ content: "", embeds: [createWarningEmbed(`Item "${item.name}" is not for sale (no price set).`)] }); + await interaction.editReply({ content: "", embeds: [createErrorEmbed(`Item "${item.name}" is not for sale (no price set).`)] }); return; } @@ -73,9 +74,13 @@ export const listing = createCommand({ try { await targetChannel.send({ embeds: [embed], components: [actionRow] }); await interaction.editReply({ content: `✅ Listing for **${item.name}** posted in ${targetChannel}.` }); - } catch (error) { - console.error("Failed to send listing message:", error); - await interaction.editReply({ content: "", embeds: [createErrorEmbed("Failed to post the listing.")] }); + } catch (error: any) { + if (error instanceof UserError) { + await interaction.reply({ embeds: [createErrorEmbed(error.message)], ephemeral: true }); + } else { + console.error("Error creating listing:", error); + await interaction.reply({ embeds: [createErrorEmbed("An unexpected error occurred.")], ephemeral: true }); + } } }, autocomplete: async (interaction) => { diff --git a/src/commands/economy/daily.ts b/src/commands/economy/daily.ts index a8da200..ee2f6f2 100644 --- a/src/commands/economy/daily.ts +++ b/src/commands/economy/daily.ts @@ -1,7 +1,9 @@ +```typescript import { createCommand } from "@/lib/utils"; import { SlashCommandBuilder, EmbedBuilder, MessageFlags } from "discord.js"; import { economyService } from "@/modules/economy/economy.service"; -import { createErrorEmbed, createWarningEmbed } from "@lib/embeds"; +import { createSuccessEmbed, createErrorEmbed } from "@lib/embeds"; +import { UserError } from "@/lib/errors"; export const daily = createCommand({ data: new SlashCommandBuilder() @@ -13,10 +15,10 @@ export const daily = createCommand({ const embed = new EmbedBuilder() .setTitle("💰 Daily Reward Claimed!") - .setDescription(`You claimed **${result.amount}** Astral Units!`) + .setDescription(`You claimed ** ${ result.amount }** Astral Units!`) .addFields( - { name: "Streak", value: `🔥 ${result.streak} days`, inline: true }, - { name: "Next Reward", value: ``, inline: true } + { name: "Streak", value: `🔥 ${ result.streak } days`, inline: true }, + { name: "Next Reward", value: `< t:${ Math.floor(result.nextReadyAt.getTime() / 1000) }: R > `, inline: true } ) .setColor("Gold") .setTimestamp(); @@ -24,13 +26,13 @@ export const daily = createCommand({ await interaction.reply({ embeds: [embed] }); } catch (error: any) { - if (error.message.includes("Daily already claimed")) { - await interaction.reply({ embeds: [createWarningEmbed(error.message, "Cooldown")], flags: MessageFlags.Ephemeral }); - return; + if (error instanceof UserError) { + await interaction.reply({ embeds: [createErrorEmbed(error.message)], ephemeral: true }); + } else { + console.error("Error claiming daily:", error); + await interaction.reply({ embeds: [createErrorEmbed("An unexpected error occurred.")], ephemeral: true }); } - - console.error(error); - await interaction.reply({ embeds: [createErrorEmbed("An error occurred while claiming your daily reward.")], flags: MessageFlags.Ephemeral }); } } }); +``` diff --git a/src/commands/economy/exam.ts b/src/commands/economy/exam.ts index 7e09d02..d7a3159 100644 --- a/src/commands/economy/exam.ts +++ b/src/commands/economy/exam.ts @@ -2,6 +2,7 @@ import { createCommand } from "@/lib/utils"; import { SlashCommandBuilder } from "discord.js"; import { userService } from "@/modules/user/user.service"; import { createErrorEmbed, createSuccessEmbed } from "@lib/embeds"; +import { UserError } from "@/lib/errors"; import { userTimers, users } from "@/db/schema"; import { eq, and, sql } from "drizzle-orm"; import { DrizzleClient } from "@/lib/DrizzleClient"; @@ -179,8 +180,12 @@ export const exam = createCommand({ }); } catch (error: any) { - console.error("Exam command error:", error); - await interaction.editReply({ embeds: [createErrorEmbed("An error occurred while processing your exam.")] }); + if (error instanceof UserError) { + await interaction.reply({ embeds: [createErrorEmbed(error.message)], ephemeral: true }); + } else { + console.error("Error in exam command:", error); + await interaction.reply({ embeds: [createErrorEmbed("An unexpected error occurred.")], ephemeral: true }); + } } } }); diff --git a/src/commands/economy/pay.ts b/src/commands/economy/pay.ts index e2f7a9c..d4f78b9 100644 --- a/src/commands/economy/pay.ts +++ b/src/commands/economy/pay.ts @@ -1,9 +1,10 @@ +```typescript import { createCommand } from "@/lib/utils"; import { SlashCommandBuilder, EmbedBuilder, MessageFlags } from "discord.js"; import { economyService } from "@/modules/economy/economy.service"; import { userService } from "@/modules/user/user.service"; import { config } from "@/lib/config"; -import { createErrorEmbed, createWarningEmbed } from "@/lib/embeds"; +import { createSuccessEmbed, createErrorEmbed } from "@lib/embeds"; import { UserError } from "@/lib/errors"; export const pay = createCommand({ @@ -26,7 +27,7 @@ export const pay = createCommand({ const discordUser = interaction.options.getUser("user", true); if (discordUser.bot) { - await interaction.reply({ embeds: [createWarningEmbed("You cannot send money to bots.")], flags: MessageFlags.Ephemeral }); + await interaction.reply({ embeds: [createErrorEmbed("You cannot send money to bots.")], flags: MessageFlags.Ephemeral }); return; } @@ -35,33 +36,35 @@ export const pay = createCommand({ const receiverId = targetUser.id; if (amount < config.economy.transfers.minAmount) { - await interaction.reply({ embeds: [createWarningEmbed(`Amount must be at least ${config.economy.transfers.minAmount}.`)], flags: MessageFlags.Ephemeral }); + await interaction.reply({ embeds: [createErrorEmbed(`Amount must be at least ${ config.economy.transfers.minAmount }.`)], flags: MessageFlags.Ephemeral }); return; } if (senderId === receiverId) { - await interaction.reply({ embeds: [createWarningEmbed("You cannot pay yourself.")], flags: MessageFlags.Ephemeral }); + await interaction.reply({ embeds: [createErrorEmbed("You cannot pay yourself.")], flags: MessageFlags.Ephemeral }); return; } try { + await interaction.deferReply({ ephemeral: true }); await economyService.transfer(senderId, receiverId, amount); const embed = new EmbedBuilder() .setTitle("💸 Transfer Successful") - .setDescription(`Successfully sent **${amount}** Astral Units to <@${targetUser.id}>.`) + .setDescription(`Successfully sent ** ${ amount }** Astral Units to < @${ targetUser.id }>.`) .setColor("Green") .setTimestamp(); - await interaction.reply({ embeds: [embed] }); + await interaction.editReply({ embeds: [embed] }); } catch (error: any) { if (error instanceof UserError) { - await interaction.reply({ embeds: [createWarningEmbed(error.message)], flags: MessageFlags.Ephemeral }); - return; + await interaction.editReply({ embeds: [createErrorEmbed(error.message)] }); + } else { + console.error("Error sending payment:", error); + await interaction.editReply({ embeds: [createErrorEmbed("An unexpected error occurred.")] }); } - console.error(error); - await interaction.reply({ embeds: [createErrorEmbed("Transfer failed due to an unexpected error.")], flags: MessageFlags.Ephemeral }); } } }); +```