Files
discord-rpg-concept/src/commands/economy/pay.ts

61 lines
2.2 KiB
TypeScript

import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { economyService } from "@/modules/economy/economy.service";
import { userService } from "@/modules/user/user.service";
export const pay = createCommand({
data: new SlashCommandBuilder()
.setName("pay")
.setDescription("Transfer coins to another user")
.addUserOption(option =>
option.setName("user")
.setDescription("The user to pay")
.setRequired(true)
)
.addIntegerOption(option =>
option.setName("amount")
.setDescription("Amount to transfer")
.setMinValue(1)
.setRequired(true)
),
execute: async (interaction) => {
await interaction.deferReply();
const targetUser = interaction.options.getUser("user", true);
const amount = BigInt(interaction.options.getInteger("amount", true));
const senderId = interaction.user.id;
const receiverId = targetUser.id;
if (senderId === receiverId) {
await interaction.editReply({ content: "❌ You cannot pay yourself." });
return;
}
// Ensure receiver exists
let receiver = await userService.getUserById(receiverId);
if (!receiver) {
receiver = await userService.createUser(receiverId, targetUser.username, undefined);
}
try {
await economyService.transfer(senderId, receiverId, amount);
const embed = new EmbedBuilder()
.setTitle("💸 Transfer Successful")
.setDescription(`Successfully sent **${amount}** coins to ${targetUser}.`)
.setColor("Green")
.setTimestamp();
await interaction.editReply({ embeds: [embed] });
} catch (error: any) {
if (error.message.includes("Insufficient funds")) {
await interaction.editReply({ content: "❌ Insufficient funds." });
return;
}
console.error(error);
await interaction.editReply({ content: "❌ Transfer failed." });
}
}
});