Add currency commands and polish utilities
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const { connectToDatabase } = require('../../db.js');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('balance')
|
||||
.setDescription('Check your coin balance or another user\'s balance.')
|
||||
.addUserOption(option =>
|
||||
option
|
||||
.setName('user')
|
||||
.setDescription('User to check (defaults to you)')
|
||||
.setRequired(false)),
|
||||
async execute(interaction) {
|
||||
const dbClient = await connectToDatabase();
|
||||
const db = dbClient.db('discord');
|
||||
const collection = db.collection('currency');
|
||||
|
||||
const target = interaction.options.getUser('user') ?? interaction.user;
|
||||
const targetData = await collection.findOne({ userID: target.id });
|
||||
const balance = targetData?.balance ?? 0;
|
||||
|
||||
return interaction.reply({
|
||||
content: `${target.tag} has **${balance}** coins.`,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const { connectToDatabase } = require('../../db.js');
|
||||
|
||||
const REWARD_AMOUNT = 100;
|
||||
const COOLDOWN_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
const formatDuration = (ms) => {
|
||||
const totalSeconds = Math.ceil(ms / 1000);
|
||||
const hours = Math.floor(totalSeconds / 3600);
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
|
||||
const parts = [];
|
||||
if (hours) parts.push(`${hours}h`);
|
||||
if (minutes) parts.push(`${minutes}m`);
|
||||
if (!hours && !minutes) parts.push(`${seconds}s`);
|
||||
|
||||
return parts.join(' ');
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('earn')
|
||||
.setDescription('Claim your daily coin reward.'),
|
||||
async execute(interaction) {
|
||||
const dbClient = await connectToDatabase();
|
||||
const db = dbClient.db('discord');
|
||||
const collection = db.collection('currency');
|
||||
const now = new Date();
|
||||
|
||||
const userId = interaction.user.id;
|
||||
const userData = await collection.findOne({ userID: userId });
|
||||
const lastClaim = userData?.lastClaimedAt ? new Date(userData.lastClaimedAt) : null;
|
||||
|
||||
if (lastClaim) {
|
||||
const elapsed = now - lastClaim;
|
||||
if (elapsed < COOLDOWN_MS) {
|
||||
const remaining = COOLDOWN_MS - elapsed;
|
||||
return interaction.reply({
|
||||
content: `You already claimed your reward. Try again in **${formatDuration(remaining)}**.`,
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await collection.updateOne(
|
||||
{ userID: userId },
|
||||
{
|
||||
$set: { lastClaimedAt: now.toISOString() },
|
||||
$inc: { balance: REWARD_AMOUNT },
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
|
||||
return interaction.reply({
|
||||
content: `You earned **${REWARD_AMOUNT}** coins! Come back tomorrow for more.`,
|
||||
});
|
||||
}
|
||||
};
|
||||
+24
-16
@@ -1,37 +1,45 @@
|
||||
// exampleCommand.js
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
|
||||
const { connectToDatabase } = require('../../db.js');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('give')
|
||||
.setDescription('give somebody coins')
|
||||
.setDescription('Admin: grant coins to a user.')
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
|
||||
.addIntegerOption(option =>
|
||||
option
|
||||
.setName('amount')
|
||||
.setDescription('amount of the coins')
|
||||
.setDescription('Amount of coins to give')
|
||||
.setMinValue(1)
|
||||
.setRequired(true))
|
||||
.addUserOption(option =>
|
||||
option.setName('target')
|
||||
.setDescription('The user to give coins to')
|
||||
option
|
||||
.setName('target')
|
||||
.setDescription('User to receive coins')
|
||||
.setRequired(true)),
|
||||
async execute(interaction) {
|
||||
const dbClient = await connectToDatabase();
|
||||
const target = interaction.options.getUser('target');
|
||||
const amount = interaction.options.getInteger('amount');
|
||||
|
||||
// Choose a DB + Collection
|
||||
if (target.bot) {
|
||||
return interaction.reply({
|
||||
content: 'You cannot grant coins to bot accounts.',
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
|
||||
const db = dbClient.db('discord');
|
||||
const collection = db.collection('currency');
|
||||
|
||||
let targetData = await collection.findOne({ userID: target.id });
|
||||
if (!targetData) {
|
||||
await collection.insertOne({ userID: target.id, balance: 0 });
|
||||
}
|
||||
await collection.updateOne(
|
||||
{ userID: target.id },
|
||||
{ $inc: { balance: amount } },
|
||||
{ upsert: true }
|
||||
);
|
||||
|
||||
// Perform the transaction: subtract from sender and add to target.
|
||||
await collection.updateOne({ userID: target.id }, { $inc: { balance: amount } }, { upsert: true });
|
||||
|
||||
return interaction.reply({ content: `You have given ${amount} coins to ${target.tag}.` });
|
||||
return interaction.reply({
|
||||
content: `You granted **${amount}** coins to ${target.tag}.`,
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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}.`,
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user