// code from goatstore // api: https://store.agi.bd const fs = require("fs"); const path = require("path"); const vm = require("vm"); // কোড চালায় না, শুধু syntax পরীক্ষা করে function checkCode(code, filename = "code.js") { const wrapped = "(async function(require,module,exports,__dirname,__filename){" + code + "\n})"; try { new vm.Script(wrapped, { filename }); return { ok: true }; } catch (err) { const stack = String(err.stack || ""); const m = stack.match(/:(\d+)\n([^\n]*)\n/); const line = m ? Number(m[1]) : null; const lineText = line ? (code.split("\n")[line - 1] || "").trim() : ""; return { ok: false, message: err.message, line, lineText }; } } function stripFences(text) { const fence = text.match(/```(?:js|javascript)?\n?([\s\S]*?)```/i); return (fence ? fence[1] : text).trim(); } function listJsFiles(dir) { try { return fs.readdirSync(dir).filter(f => f.endsWith(".js")).map(f => path.join(dir, f)); } catch (e) { return []; } } module.exports = { config: { name: "syntaxcheck", aliases: ["syntax", "jscheck"], version: "1.0", author: "Zihad", countDown: 5, role: 0, description: { en: "Check JavaScript syntax errors (no code is executed)" }, category: "tools", guide: { en: " {pn} : check typed code" + "\n Reply to a message with code + {pn}: check that code" + "\n {pn} all: scan all command & event files (admin only)" + "\n {pn} : check one command file (admin only)" } }, onStart: async function ({ api, event, args, message }) { const { threadID, messageID, senderID } = event; const adminList = (global.GoatBot && global.GoatBot.config && global.GoatBot.config.adminBot) || []; const isAdmin = adminList.includes(String(senderID)); const root = process.cwd(); const dirs = [path.join(root, "scripts", "cmds"), path.join(root, "scripts", "events")]; const first = (args[0] || "").toLowerCase(); // ---------- সব ফাইল স্ক্যান ---------- if (first === "all") { if (!isAdmin) return message.reply("⛔ Only bot admins can scan files."); const files = dirs.flatMap(listJsFiles); if (!files.length) return message.reply("❌ No command files found."); const bad = []; for (const file of files) { const res = checkCode(fs.readFileSync(file, "utf-8"), file); if (!res.ok) bad.push(`❌ ${path.basename(file)} — line ${res.line || "?"}: ${res.message}`); } const head = `╭───「 SYNTAX SCAN 」\n│ 📂 Files : ${files.length}\n│ ✅ OK : ${files.length - bad.length}\n│ ❌ Error : ${bad.length}\n╰────────────`; return message.reply(bad.length ? head + "\n\n" + bad.join("\n") : head + "\n\n🎉 All files are clean!"); } // ---------- একটা ফাইল ---------- if (args.length === 1 && /^[\w.\-]+$/.test(args[0]) && !/[;(){}=]/.test(args[0])) { const name = args[0].endsWith(".js") ? args[0] : args[0] + ".js"; const found = dirs.map(d => path.join(d, name)).find(p => fs.existsSync(p)); if (found) { if (!isAdmin) return message.reply("⛔ Only bot admins can check bot files."); const res = checkCode(fs.readFileSync(found, "utf-8"), found); if (res.ok) return message.reply(`✅ ${name}\nNo syntax errors found.`); return message.reply(`❌ ${name}\n📍 Line ${res.line || "?"}\n💬 ${res.message}${res.lineText ? `\n🔎 ${res.lineText.slice(0, 120)}` : ""}`); } } // ---------- কোড (reply বা লেখা) ---------- let code = ""; if (event.messageReply && event.messageReply.body) code = event.messageReply.body; else code = args.join(" "); code = stripFences(code); if (!code) return message.reply("⚠️ Reply to a message with code, or type: syntaxcheck "); const res = checkCode(code); if (res.ok) return message.reply("✅ No syntax errors found."); return message.reply( `╭───「 SYNTAX ERROR 」\n│ 📍 Line : ${res.line || "?"}\n│ 💬 ${res.message}\n${res.lineText ? `│ 🔎 ${res.lineText.slice(0, 120)}\n` : ""}╰────────────` ); } };