feat(commands): improve error feedback for economy and admin commands
This commit is contained in:
@@ -10,7 +10,8 @@ import {
|
|||||||
MessageFlags
|
MessageFlags
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { inventoryService } from "@/modules/inventory/inventory.service";
|
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 { items } from "@/db/schema";
|
||||||
import { ilike, isNotNull, and } from "drizzle-orm";
|
import { ilike, isNotNull, and } from "drizzle-orm";
|
||||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||||
@@ -49,7 +50,7 @@ export const listing = createCommand({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!item.price) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,9 +74,13 @@ export const listing = createCommand({
|
|||||||
try {
|
try {
|
||||||
await targetChannel.send({ embeds: [embed], components: [actionRow] });
|
await targetChannel.send({ embeds: [embed], components: [actionRow] });
|
||||||
await interaction.editReply({ content: `✅ Listing for **${item.name}** posted in ${targetChannel}.` });
|
await interaction.editReply({ content: `✅ Listing for **${item.name}** posted in ${targetChannel}.` });
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error("Failed to send listing message:", error);
|
if (error instanceof UserError) {
|
||||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("Failed to post the listing.")] });
|
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) => {
|
autocomplete: async (interaction) => {
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
```typescript
|
||||||
import { createCommand } from "@/lib/utils";
|
import { createCommand } from "@/lib/utils";
|
||||||
import { SlashCommandBuilder, EmbedBuilder, MessageFlags } from "discord.js";
|
import { SlashCommandBuilder, EmbedBuilder, MessageFlags } from "discord.js";
|
||||||
import { economyService } from "@/modules/economy/economy.service";
|
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({
|
export const daily = createCommand({
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@@ -13,10 +15,10 @@ export const daily = createCommand({
|
|||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setTitle("💰 Daily Reward Claimed!")
|
.setTitle("💰 Daily Reward Claimed!")
|
||||||
.setDescription(`You claimed **${result.amount}** Astral Units!`)
|
.setDescription(`You claimed ** ${ result.amount }** Astral Units!`)
|
||||||
.addFields(
|
.addFields(
|
||||||
{ name: "Streak", value: `🔥 ${result.streak} days`, inline: true },
|
{ name: "Streak", value: `🔥 ${ result.streak } days`, inline: true },
|
||||||
{ name: "Next Reward", value: `<t:${Math.floor(result.nextReadyAt.getTime() / 1000)}:R>`, inline: true }
|
{ name: "Next Reward", value: `< t:${ Math.floor(result.nextReadyAt.getTime() / 1000) }: R > `, inline: true }
|
||||||
)
|
)
|
||||||
.setColor("Gold")
|
.setColor("Gold")
|
||||||
.setTimestamp();
|
.setTimestamp();
|
||||||
@@ -24,13 +26,13 @@ export const daily = createCommand({
|
|||||||
await interaction.reply({ embeds: [embed] });
|
await interaction.reply({ embeds: [embed] });
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.message.includes("Daily already claimed")) {
|
if (error instanceof UserError) {
|
||||||
await interaction.reply({ embeds: [createWarningEmbed(error.message, "Cooldown")], flags: MessageFlags.Ephemeral });
|
await interaction.reply({ embeds: [createErrorEmbed(error.message)], ephemeral: true });
|
||||||
return;
|
} 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 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
```
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { createCommand } from "@/lib/utils";
|
|||||||
import { SlashCommandBuilder } from "discord.js";
|
import { SlashCommandBuilder } from "discord.js";
|
||||||
import { userService } from "@/modules/user/user.service";
|
import { userService } from "@/modules/user/user.service";
|
||||||
import { createErrorEmbed, createSuccessEmbed } from "@lib/embeds";
|
import { createErrorEmbed, createSuccessEmbed } from "@lib/embeds";
|
||||||
|
import { UserError } from "@/lib/errors";
|
||||||
import { userTimers, users } from "@/db/schema";
|
import { userTimers, users } from "@/db/schema";
|
||||||
import { eq, and, sql } from "drizzle-orm";
|
import { eq, and, sql } from "drizzle-orm";
|
||||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||||
@@ -179,8 +180,12 @@ export const exam = createCommand({
|
|||||||
});
|
});
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Exam command error:", error);
|
if (error instanceof UserError) {
|
||||||
await interaction.editReply({ embeds: [createErrorEmbed("An error occurred while processing your exam.")] });
|
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 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
|
```typescript
|
||||||
import { createCommand } from "@/lib/utils";
|
import { createCommand } from "@/lib/utils";
|
||||||
import { SlashCommandBuilder, EmbedBuilder, MessageFlags } from "discord.js";
|
import { SlashCommandBuilder, EmbedBuilder, MessageFlags } from "discord.js";
|
||||||
import { economyService } from "@/modules/economy/economy.service";
|
import { economyService } from "@/modules/economy/economy.service";
|
||||||
import { userService } from "@/modules/user/user.service";
|
import { userService } from "@/modules/user/user.service";
|
||||||
import { config } from "@/lib/config";
|
import { config } from "@/lib/config";
|
||||||
import { createErrorEmbed, createWarningEmbed } from "@/lib/embeds";
|
import { createSuccessEmbed, createErrorEmbed } from "@lib/embeds";
|
||||||
import { UserError } from "@/lib/errors";
|
import { UserError } from "@/lib/errors";
|
||||||
|
|
||||||
export const pay = createCommand({
|
export const pay = createCommand({
|
||||||
@@ -26,7 +27,7 @@ export const pay = createCommand({
|
|||||||
const discordUser = interaction.options.getUser("user", true);
|
const discordUser = interaction.options.getUser("user", true);
|
||||||
|
|
||||||
if (discordUser.bot) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,33 +36,35 @@ export const pay = createCommand({
|
|||||||
const receiverId = targetUser.id;
|
const receiverId = targetUser.id;
|
||||||
|
|
||||||
if (amount < config.economy.transfers.minAmount) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (senderId === receiverId) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await interaction.deferReply({ ephemeral: true });
|
||||||
await economyService.transfer(senderId, receiverId, amount);
|
await economyService.transfer(senderId, receiverId, amount);
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setTitle("💸 Transfer Successful")
|
.setTitle("💸 Transfer Successful")
|
||||||
.setDescription(`Successfully sent **${amount}** Astral Units to <@${targetUser.id}>.`)
|
.setDescription(`Successfully sent ** ${ amount }** Astral Units to < @${ targetUser.id }>.`)
|
||||||
.setColor("Green")
|
.setColor("Green")
|
||||||
.setTimestamp();
|
.setTimestamp();
|
||||||
|
|
||||||
await interaction.reply({ embeds: [embed] });
|
await interaction.editReply({ embeds: [embed] });
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error instanceof UserError) {
|
if (error instanceof UserError) {
|
||||||
await interaction.reply({ embeds: [createWarningEmbed(error.message)], flags: MessageFlags.Ephemeral });
|
await interaction.editReply({ embeds: [createErrorEmbed(error.message)] });
|
||||||
return;
|
} 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 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user