feat: add trading system with dedicated modules and centralize embed creation for commands
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
import { userService } from "@/modules/user/user.service";
|
||||
import { inventoryService } from "@/modules/inventory/inventory.service";
|
||||
import type { items } from "@db/schema";
|
||||
import { createErrorEmbed, createWarningEmbed } from "@lib/embeds";
|
||||
|
||||
export const sell = createCommand({
|
||||
data: new SlashCommandBuilder()
|
||||
@@ -36,18 +37,18 @@ export const sell = createCommand({
|
||||
const targetChannel = (interaction.options.getChannel("channel") as BaseGuildTextChannel) || interaction.channel as BaseGuildTextChannel;
|
||||
|
||||
if (!targetChannel || !targetChannel.isSendable()) {
|
||||
await interaction.editReply({ content: "Target channel is invalid or not sendable." });
|
||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("Target channel is invalid or not sendable.")] });
|
||||
return;
|
||||
}
|
||||
|
||||
const item = await inventoryService.getItem(itemId);
|
||||
if (!item) {
|
||||
await interaction.editReply({ content: `Item with ID ${itemId} not found.` });
|
||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed(`Item with ID ${itemId} not found.`)] });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!item.price) {
|
||||
await interaction.editReply({ content: `Item "${item.name}" is not for sale (no price set).` });
|
||||
await interaction.editReply({ content: "", embeds: [createWarningEmbed(`Item "${item.name}" is not for sale (no price set).`)] });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -82,7 +83,7 @@ export const sell = createCommand({
|
||||
|
||||
} catch (error) {
|
||||
console.error("Failed to send sell message:", error);
|
||||
await interaction.editReply({ content: "Failed to post the item for sale." });
|
||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("Failed to post the item for sale.")] });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -95,19 +96,19 @@ async function handleBuyInteraction(interaction: ButtonInteraction, item: typeof
|
||||
const user = await userService.getUserById(userId);
|
||||
|
||||
if (!user) {
|
||||
await interaction.editReply({ content: "User profile not found." });
|
||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("User profile not found.")] });
|
||||
return;
|
||||
}
|
||||
|
||||
if ((user.balance ?? 0n) < (item.price ?? 0n)) {
|
||||
await interaction.editReply({ content: `You don't have enough money! You need ${item.price} 🪙.` });
|
||||
await interaction.editReply({ content: "", embeds: [createWarningEmbed(`You don't have enough money! You need ${item.price} 🪙.`)] });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await inventoryService.buyItem(userId, item.id, 1n);
|
||||
|
||||
if (!result.success) {
|
||||
await interaction.editReply({ content: "Transaction failed. Please try again." });
|
||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("Transaction failed. Please try again.")] });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -115,9 +116,9 @@ async function handleBuyInteraction(interaction: ButtonInteraction, item: typeof
|
||||
} catch (error) {
|
||||
console.error("Error processing purchase:", error);
|
||||
if (interaction.deferred || interaction.replied) {
|
||||
await interaction.editReply({ content: "An error occurred while processing your purchase." });
|
||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("An error occurred while processing your purchase.")] });
|
||||
} else {
|
||||
await interaction.reply({ content: "An error occurred while processing your purchase.", ephemeral: true });
|
||||
await interaction.reply({ embeds: [createErrorEmbed("An error occurred while processing your purchase.")], ephemeral: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user