Added Setbalance and balance check

This commit is contained in:
2025-12-05 18:09:24 +05:30
parent 1cc2759511
commit d458ada051
3 changed files with 66 additions and 5 deletions

View File

@@ -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] });
}
});

View File

@@ -1,13 +1,38 @@
import { createCommand } from "@lib/utils"; import { createCommand } from "@lib/utils";
import { getUserBalance } from "@/modules/economy/economy.service"; 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({ 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) => { execute: async (interaction) => {
const balance = await getUserBalance(interaction.user.id) || 0; const isAdmin = interaction.memberPermissions?.has(PermissionFlagsBits.Administrator) || false;
const embed = new EmbedBuilder().setDescription(`Your balance is ${balance}`);
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] }); await interaction.reply({ embeds: [embed] });
} }
}); });

View File

@@ -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] });
}
});