forked from syntaxbullet/AuroraBot-discord
61 lines
2.3 KiB
TypeScript
61 lines
2.3 KiB
TypeScript
import { createCommand } from "@lib/utils";
|
|
import { getUserBalance, setUserBalance } from "@/modules/economy/economy.service";
|
|
import { createUser, getUserById } from "@/modules/users/users.service";
|
|
import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from "discord.js";
|
|
|
|
export const pay = createCommand({
|
|
data: new SlashCommandBuilder()
|
|
.setName("pay")
|
|
.setDescription("Send balance to another user")
|
|
.addUserOption(option =>
|
|
option.setName('recipient')
|
|
.setDescription('The user to send balance to')
|
|
.setRequired(true))
|
|
.addIntegerOption(option =>
|
|
option.setName('amount')
|
|
.setDescription('The amount of balance to send')
|
|
.setRequired(true))
|
|
|
|
|
|
, execute: async (interaction) => {
|
|
const user = interaction.user;
|
|
// Ensure if your user exists in DB
|
|
let dbUser = await getUserById(user.id);
|
|
if (!dbUser) {
|
|
await createUser(user.id);
|
|
}
|
|
|
|
const balance = await getUserBalance(user.id);
|
|
const recipient = interaction.options.getUser('recipient');
|
|
const amount = interaction.options.getInteger('amount');
|
|
|
|
if (amount! <= 0) {
|
|
await interaction.reply({ content: "❌ Amount must be greater than zero.", ephemeral: true });
|
|
return;
|
|
}
|
|
if (amount! > balance) {
|
|
await interaction.reply({ content: "❌ You do not have enough coins to complete this transaction.", ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
if (recipient!.id === user.id) {
|
|
await interaction.reply({ content: "❌ You cannot send coins to yourself.", ephemeral: true });
|
|
return;
|
|
}
|
|
|
|
// Ensure recipient exists in DB
|
|
let dbRecipient = await getUserById(recipient!.id);
|
|
if (!dbRecipient) {
|
|
dbRecipient = await createUser(recipient!.id);
|
|
}
|
|
|
|
await setUserBalance(user.id, balance - amount!); // Deduct from sender
|
|
await setUserBalance(recipient!.id, (await getUserBalance(recipient!.id)) + amount!); // Add to recipient
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setDescription(`sent **${amount} coins** to ${recipient!.username}`)
|
|
.setColor("Green");
|
|
|
|
await interaction.reply({ embeds: [embed] });
|
|
}
|
|
}); |