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 \nāž¤ setbal \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 \nOR\nsetbal "); 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); } };