feat(commands): improve error feedback for economy and admin commands

This commit is contained in:
syntaxbullet
2025-12-22 12:59:46 +01:00
parent fbcac51370
commit 0dea266a6d
4 changed files with 42 additions and 27 deletions

View File

@@ -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) => {

View File

@@ -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()
@@ -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 });
}
}
});
```

View File

@@ -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 });
}
}
}
});

View File

@@ -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,16 +36,17 @@ 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()
@@ -53,15 +55,16 @@ export const pay = createCommand({
.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 });
}
}
});
```