From d458ada051af95baffe617eaf214ada54d4323b2 Mon Sep 17 00:00:00 2001 From: Vraj Ved Date: Fri, 5 Dec 2025 18:09:24 +0530 Subject: [PATCH] Added Setbalance and balance check --- app/src/commands/cookies.ts | 10 ++++++++ app/src/commands/economy/balance.ts | 35 ++++++++++++++++++++++---- app/src/commands/economy/setbalance.ts | 26 +++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 app/src/commands/cookies.ts create mode 100644 app/src/commands/economy/setbalance.ts diff --git a/app/src/commands/cookies.ts b/app/src/commands/cookies.ts new file mode 100644 index 0000000..5fbc8d2 --- /dev/null +++ b/app/src/commands/cookies.ts @@ -0,0 +1,10 @@ +import { createCommand } from "@lib/utils"; +import { SlashCommandBuilder, EmbedBuilder } from "discord.js"; + +export const cookies = createCommand({ + data: new SlashCommandBuilder().setName("cookies").setDescription("Say sadhasj"), + execute: async (interaction) => { + const embed = new EmbedBuilder().setDescription("HELLO sandwich"); + await interaction.reply({ embeds: [embed] }); + } +}); diff --git a/app/src/commands/economy/balance.ts b/app/src/commands/economy/balance.ts index 2cd9ad1..56a77b2 100644 --- a/app/src/commands/economy/balance.ts +++ b/app/src/commands/economy/balance.ts @@ -1,13 +1,38 @@ import { createCommand } from "@lib/utils"; import { getUserBalance } from "@/modules/economy/economy.service"; -import { SlashCommandBuilder, EmbedBuilder } from "discord.js"; +import { createUser, getUserById } from "@/modules/users/users.service"; +import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from "discord.js"; export const balance = createCommand({ - data: new SlashCommandBuilder().setName("balance").setDescription("Check your balance"), + data: new SlashCommandBuilder() + .setName("balance") + .setDescription("Check your balance or another user's balance (admins only)") + .addUserOption(option => + option + .setName("user") + .setDescription("User to view balance of (admin only)") + .setRequired(false) + ), + execute: async (interaction) => { - const balance = await getUserBalance(interaction.user.id) || 0; - const embed = new EmbedBuilder().setDescription(`Your balance is ${balance}`); + const isAdmin = interaction.memberPermissions?.has(PermissionFlagsBits.Administrator) || false; + + const target = interaction.options.getUser("user"); + const finalTarget = (!isAdmin || !target) ? interaction.user : target; + + // Ensure user exists in DB + let dbUser = await getUserById(finalTarget.id); + if (!dbUser) { + await createUser(finalTarget.id); + } + + const balance = await getUserBalance(finalTarget.id); + + const embed = new EmbedBuilder() + .setTitle(`${finalTarget.username}'s Balance`) + .setDescription(`💰 **${balance} coins**`) + .setColor("Green"); + await interaction.reply({ embeds: [embed] }); } }); - diff --git a/app/src/commands/economy/setbalance.ts b/app/src/commands/economy/setbalance.ts new file mode 100644 index 0000000..0af7045 --- /dev/null +++ b/app/src/commands/economy/setbalance.ts @@ -0,0 +1,26 @@ +import { createCommand } from "@lib/utils"; +import { getUserBalance, setUserBalance } from "@/modules/economy/economy.service"; +import { createUser, getUserById } from "@/modules/users/users.service"; +import { SlashCommandBuilder, EmbedBuilder } from "discord.js"; + +export const setbalance = createCommand({ + data: new SlashCommandBuilder().setName("setbalance").setDescription("Set your balance").addUserOption(option => option.setName("user").setDescription("The user to set the balance for").setRequired(true)).addNumberOption(option => + option.setName("amount") + .setDescription("The amount to set your balance to") + .setRequired(true) + ), execute: async (interaction) => { + const user = await getUserById(interaction.options.getUser("user", true).id); + if (!user) { + await createUser(interaction.options.getUser("user", true).id); + } else { + console.log(`User ${interaction.options.getUser("user", true).id} already exists.`); + } + const amount = interaction.options.getNumber("amount", true); + const oldbalance = await getUserBalance(interaction.options.getUser("user", true).id); + await setUserBalance(interaction.options.getUser("user", true).id, amount); + const balance = await getUserBalance(interaction.options.getUser("user", true).id) ; + const embed = new EmbedBuilder().setDescription(`${interaction.options.getUser("user", true).username}'s old balance is: ${oldbalance}. ${interaction.options.getUser("user", true).username}'s amount is ${amount}. ${interaction.options.getUser("user", true).username}'s new balance is ${balance}.`); + await interaction.reply({ embeds: [embed] }); + } +}); +