Added Setbalance and balance check
This commit is contained in:
10
app/src/commands/cookies.ts
Normal file
10
app/src/commands/cookies.ts
Normal 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] });
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -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] });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
26
app/src/commands/economy/setbalance.ts
Normal file
26
app/src/commands/economy/setbalance.ts
Normal 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] });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user