← back to browse
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);
 }
};

View raw file