goat
command
setbal
๐ฐ Set or add balance
0
views
0
likes
0
installs
Aliases: setmoney, setbalance
raw source
const { evaluate } = require("mathjs");
function parseAmount(input) {
const suffixes = { k: 1e3, m: 1e6, b: 1e9, t: 1e12, q: 1e15 };
input = input.toLowerCase().replace(/,/g, "");
const match = input.match(/^([\d.+\-*/^()]+)([kmbtq]?)$/);
if (!match) throw new Error("Invalid input");
const base = evaluate(match[1]);
const suffix = match[2];
return base * (suffixes[suffix] || 1);
}
module.exports = {
config: {
name: "setbal",
aliases: ["setmoney", "setbalance"],
version: "1.3",
author: "BaYjid",
countDown: 3,
role: 3,
shortDescription: {
en: "๐ฐ Set or add balance"
},
longDescription: {
en: "Set or add balance to user (supports math & short forms)"
},
category: "๐ผ Economy",
guide: {
en: "โค +setbal <uid/@mention> <amount>\nโค setbal <amount>\n\nโ
Supports math & short forms:\n5m = 5 million\n9999^2 = math\n\nExamples:\n+setbal 10008133 5b\n+setbal @John 1.2t\nsetbal 1000^4"
},
usePrefix: true,
useChat: true
},
onStart: async function ({ api, event, args, usersData, message }) {
if (args.length < 1)
return message.reply("โ Usage:\n+setbal <uid/@mention> <amount>\nOR\nsetbal <amount>");
let targetID;
let amountInput;
// Check for mention
if (event.mentions && Object.keys(event.mentions).length > 0) {
targetID = Object.keys(event.mentions)[0];
amountInput = args.slice(1).join("");
}
// Check for UID
else if (/^\d{5,20}$/.test(args[0]) && args.length >= 2) {
targetID = args[0];
amountInput = args.slice(1).join("");
}
// Else treat as self-add
else {
targetID = event.senderID;
amountInput = args.join("");
}
let amount;
try {
amount = parseAmount(amountInput);
if (isNaN(amount) || !isFinite(amount)) throw new Error();
} catch {
return message.reply("โ Invalid amount.\nExamples: `99999^2`, `5m`, `2.5t`, etc.");
}
if (amount < 0) return message.reply("๐ซ Balance cannot be negative!");
const oldBalance = (await usersData.get(targetID, "money")) || 0;
const newBalance = targetID === event.senderID && args.length === 1
? oldBalance + amount
: amount;
await usersData.set(targetID, newBalance, "money");
const name = await usersData.getName(targetID);
const displayAmount = Number(newBalance).toLocaleString();
const msg = targetID === event.senderID && args.length === 1
? `๐ธ ๐๐ฎ๐น๐ฎ๐ป๐ฐ๐ฒ ๐๐ฑ๐ฑ๐ฒ๐ฑ!\n๐ค You (${name})\nโ Added: ${amount.toLocaleString()}\n๐ฐ Total: $${displayAmount}`
: `๐ ๐๐ฎ๐น๐ฎ๐ป๐ฐ๐ฒ ๐ฆ๐ฒ๐!\n๐ค ${name} (${targetID})\n๐ฐ ๐ก๐ฒ๐ ๐๐ฎ๐น๐ฎ๐ป๐ฐ๐ฒ: $${displayAmount}`;
return message.reply(msg);
},
onChat: async function ({ event, message }) {
const body = event.body?.toLowerCase();
if (!body.startsWith("setbal")) return;
const args = body.split(" ").slice(1);
message.body = "+setbal " + args.join(" ");
return this.onStart(...arguments);
}
};