Add currency commands and polish utilities

This commit is contained in:
2026-01-26 12:14:18 +01:00
parent 60d52b220a
commit 0c36960645
7 changed files with 213 additions and 37 deletions
+68
View File
@@ -0,0 +1,68 @@
const { SlashCommandBuilder } = require('discord.js');
const { connectToDatabase } = require('../../db.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('transfer')
.setDescription('Send coins to another user.')
.addUserOption(option =>
option
.setName('target')
.setDescription('User to send coins to')
.setRequired(true))
.addIntegerOption(option =>
option
.setName('amount')
.setDescription('Amount of coins to send')
.setMinValue(1)
.setRequired(true)),
async execute(interaction) {
const dbClient = await connectToDatabase();
const db = dbClient.db('discord');
const collection = db.collection('currency');
const target = interaction.options.getUser('target');
const amount = interaction.options.getInteger('amount');
if (target.bot) {
return interaction.reply({
content: 'You cannot transfer coins to a bot account.',
ephemeral: true,
});
}
if (target.id === interaction.user.id) {
return interaction.reply({
content: 'You cannot transfer coins to yourself.',
ephemeral: true,
});
}
const senderId = interaction.user.id;
const senderData = await collection.findOne({ userID: senderId });
const senderBalance = senderData?.balance ?? 0;
if (senderBalance < amount) {
return interaction.reply({
content: `You only have **${senderBalance}** coins available.`,
ephemeral: true,
});
}
await collection.updateOne(
{ userID: senderId },
{ $inc: { balance: -amount } },
{ upsert: true }
);
await collection.updateOne(
{ userID: target.id },
{ $inc: { balance: amount } },
{ upsert: true }
);
return interaction.reply({
content: `You sent **${amount}** coins to ${target.tag}.`,
});
}
};