Skip to content

Commit

Permalink
More economy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMonDon committed Jul 17, 2024
1 parent 4855c61 commit 9a386eb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
17 changes: 10 additions & 7 deletions commands/Economy/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ class Balance extends Command {
if (!mem) return this.client.util.errorEmbed(msg, msg.settings.prefix + this.help.usage, 'Invalid Member');

const cash = BigInt(
(await db.get(`servers.${msg.guild.id}.users.${mem.id}.economy.cash`)) ||
(await db.get(`servers.${msg.guild.id}.economy.startBalance`)) ||
0,
parseInt(
(await db.get(`servers.${msg.guild.id}.users.${mem.id}.economy.cash`)) ||
(await db.get(`servers.${msg.guild.id}.economy.startBalance`)) ||
0,
10,
),
);
const bank = BigInt((await db.get(`servers.${msg.guild.id}.users.${mem.id}.economy.bank`)) || 0);
const bank = BigInt(parseInt((await db.get(`servers.${msg.guild.id}.users.${mem.id}.economy.bank`)) || 0, 10));
const netWorth = cash + bank;

const currencySymbol = (await db.get(`servers.${msg.guild.id}.economy.symbol`)) || '$';
Expand Down Expand Up @@ -77,8 +80,8 @@ class Balance extends Command {
try {
const user = await this.client.users.cache.get(userId);
if (user) {
const userCash = BigInt(usersData[userId].economy.cash || 0);
const userBank = BigInt(usersData[userId].economy.bank || 0);
const userCash = BigInt(parseInt(usersData[userId].economy.cash || 0, 10));
const userBank = BigInt(parseInt(usersData[userId].economy.bank || 0, 10));
const userMoney = userCash + userBank;
leaderboard.push({ user: user.tag, userId: user.id, money: userMoney });
}
Expand All @@ -88,7 +91,7 @@ class Balance extends Command {
}

// Sort the leaderboard
const sortedLeaderboard = leaderboard.sort((a, b) => (BigInt(b.money) > BigInt(a.money) ? 1 : -1));
const sortedLeaderboard = leaderboard.sort((a, b) => (b.money > a.money ? 1 : -1));

// Find the user's rank
const userRank = sortedLeaderboard.findIndex((entry) => entry.userId === mem.id) + 1;
Expand Down
2 changes: 1 addition & 1 deletion slash_commands/Economy/add-money.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports.run = async (interaction) => {
if (!target.user) return interaction.client.util.errorEmbed(interaction, 'Invalid Member');
if (target.user.bot) return interaction.client.util.errorEmbed(interaction, "You can't add money to a bot.");

amount = BigInt(amount);
amount = BigInt(parseInt(amount));
if (destination === 'bank') {
const bank = BigInt((await db.get(`servers.${interaction.guild.id}.users.${target.user.id}.economy.bank`)) || 0);
const newAmount = bank + amount;
Expand Down
47 changes: 42 additions & 5 deletions slash_commands/Economy/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ exports.run = async (interaction) => {
let mem = interaction.options?.get('user')?.member || interaction.options?.get('user')?.user;
if (!mem) mem = interaction.user;

const cash = parseFloat(
(await db.get(`servers.${interaction.guildId}.users.${mem.id}.economy.cash`)) ||
(await db.get(`servers.${interaction.guildId}.economy.startBalance`)) ||
0,
const cash = BigInt(
parseInt(
(await db.get(`servers.${interaction.guildId}.users.${mem.id}.economy.cash`)) ||
(await db.get(`servers.${interaction.guildId}.economy.startBalance`)) ||
0,
10,
),
);
const bank = parseFloat((await db.get(`servers.${interaction.guildId}.users.${mem.id}.economy.bank`)) || 0);
const bank = BigInt(parseInt((await db.get(`servers.${interaction.guildId}.users.${mem.id}.economy.bank`)) || 0, 10));
const netWorth = cash + bank;

const currencySymbol = (await db.get(`servers.${interaction.guildId}.economy.symbol`)) || '$';
Expand All @@ -34,6 +37,12 @@ exports.run = async (interaction) => {
return symbol + amount.toLocaleString();
}

function getOrdinalSuffix(n) {
const s = ['th', 'st', 'nd', 'rd'];
const v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
}

let csCashAmount = formatCurrency(cash, currencySymbol);
csCashAmount = csCashAmount.length > 1024 ? `${csCashAmount.slice(0, 1021) + '...'}` : csCashAmount;

Expand All @@ -43,14 +52,42 @@ exports.run = async (interaction) => {
let csNetWorthAmount = formatCurrency(netWorth, currencySymbol);
csNetWorthAmount = csNetWorthAmount.length > 1024 ? `${csNetWorthAmount.slice(0, 1021) + '...'}` : csNetWorthAmount;

// Fetch all users data to find the rank
const usersData = (await db.get(`servers.${interaction.guildId}.users`)) || {};
const leaderboard = [];

// Cache users and add them to the leaderboard
for (const userId in usersData) {
try {
const user = await interaction.client.users.cache.get(userId);
if (user) {
const userCash = BigInt(parseInt(usersData[userId].economy.cash || 0, 10));
const userBank = BigInt(parseInt(usersData[userId].economy.bank || 0, 10));
const userMoney = userCash + userBank;
leaderboard.push({ user: user.tag, userId: user.id, money: userMoney });
}
} catch (err) {
console.error(`Leaderboard: ${err}`);
}
}

// Sort the leaderboard
const sortedLeaderboard = leaderboard.sort((a, b) => (b.money > a.money ? 1 : -1));

// Find the user's rank
const userRank = sortedLeaderboard.findIndex((entry) => entry.userId === mem.id) + 1;
const userRankDisplay = userRank > 0 ? `Leaderboard Rank: ${getOrdinalSuffix(userRank)}` : 'Not on Leaderboard';

const embed = new EmbedBuilder()
.setAuthor({ name: mem.user?.tag || mem.username, iconURL: mem.user?.displayAvatarURL() || mem.displayAvatarURL() })
.setColor(interaction.settings.embedColor)
.setDescription(userRankDisplay)
.addFields([
{ name: 'Cash:', value: csCashAmount },
{ name: 'Bank:', value: csBankAmount },
{ name: 'Net Worth:', value: csNetWorthAmount },
])
.setTimestamp();

return interaction.editReply({ embeds: [embed] });
};
2 changes: 1 addition & 1 deletion slash_commands/Economy/remove-money.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports.run = async (interaction) => {
if (!target.user) return interaction.client.util.errorEmbed(interaction, 'Invalid Member');
if (target.user.bot) return interaction.client.util.errorEmbed(interaction, "You can't remove money from a bot.");

amount = BigInt(amount);
amount = BigInt(parseInt(amount));
if (destination === 'bank') {
const bank = BigInt((await db.get(`servers.${interaction.guild.id}.users.${target.user.id}.economy.bank`)) || 0);
const newAmount = bank - amount;
Expand Down

0 comments on commit 9a386eb

Please sign in to comment.