goat
command
install
Install command via reply / code / url
0
views
0
likes
0
installs
raw source
const axios = require("axios");
const fs = require("fs-extra");
const path = require("path");
const cheerio = require("cheerio");
const { configCommands } = global.GoatBot;
const { log } = 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 {
return false;
}
}
function extractUrlFromText(text) {
const match = text.match(/https?:\/\/[^\s]+/i);
return match ? match[0] : null;
}
async function fetchCodeFromUrl(url) {
const domain = getDomain(url);
let fixedUrl = url;
if (domain === "pastebin.com" && !url.includes("/raw/")) {
fixedUrl = url.replace("pastebin.com/", "pastebin.com/raw/");
}
if (domain === "github.com" && url.includes("/blob/")) {
fixedUrl = url
.replace("github.com", "raw.githubusercontent.com")
.replace("/blob/", "/");
}
try {
const res = await axios.get(fixedUrl);
let code = res.data;
if (domain === "savetext.net") {
const $ = cheerio.load(code);
code = $("#content").text().trim();
}
return code;
} catch {
return null;
}
}
function extractCommandName(code) {
const nameMatch = code.match(/name\s*:\s*["']([^"']+)["']/);
return nameMatch ? nameMatch[1].trim() + ".js" : null;
}
module.exports = {
config: {
name: "install",
version: "1.0.0",
author: "๐๐๐๐ฎ๐
",
countDown: 3,
role: 2,
hasPrefix: false,
description: "Install command via reply / code / url",
category: "owner"
},
onStart: async function ({ args, message, event, api }) {
let rawCode = "";
let fileName = "";
if (event.messageReply?.body) {
const replyText = event.messageReply.body.trim();
const url = extractUrlFromText(replyText);
if (url) {
rawCode = await fetchCodeFromUrl(url);
if (!rawCode) return message.reply(
"โ ๐
๐๐ข๐ฅ๐๐ ๐ญ๐จ ๐๐๐ญ๐๐ก ๐๐จ๐๐ ๐๐ซ๐จ๐ฆ ๐๐๐."
);
} else {
rawCode = replyText;
}
fileName = extractCommandName(rawCode);
}
else if (args[0] && isURL(args[0])) {
rawCode = await fetchCodeFromUrl(args[0]);
if (!rawCode) return message.reply(
"โ ๐๐ง๐ฏ๐๐ฅ๐ข๐ ๐จ๐ซ ๐ฎ๐ง๐ซ๐๐๐๐ก๐๐๐ฅ๐ ๐๐๐."
);
fileName = extractCommandName(rawCode);
}
else if (args.length >= 2 && args[0].endsWith(".js")) {
fileName = args[0];
rawCode = args.slice(1).join(" ");
}
if (!rawCode)
return message.reply(
"โ ๏ธ ๐๐จ ๐๐จ๐๐ ๐ฉ๐ซ๐จ๐ฏ๐ข๐๐๐.\n\n" +
"๐๐๐ฉ๐ฅ๐ฒ ๐ญ๐จ ๐๐จ๐๐/๐๐๐, ๐ฎ๐ฌ๐ ๐ ๐๐๐, ๐จ๐ซ:\n" +
"๐ข๐ง๐ฌ๐ญ๐๐ฅ๐ฅ <๐ง๐๐ฆ๐.๐ฃ๐ฌ> <๐๐จ๐๐>"
);
if (!fileName)
return message.reply(
"โ ๐๐จ๐ฎ๐ฅ๐ ๐ง๐จ๐ญ ๐๐๐ญ๐๐๐ญ ๐๐จ๐ฆ๐ฆ๐๐ง๐ ๐ง๐๐ฆ๐.\n" +
"๐๐๐ค๐ ๐ฌ๐ฎ๐ซ๐ ๐ญ๐ก๐ ๐๐จ๐๐ ๐ก๐๐ฌ ๐ ๐ฏ๐๐ฅ๐ข๐ ๐ง๐๐ฆ๐ ๐๐ข๐๐ฅ๐."
);
const filePath = path.join(process.cwd(), "scripts", "cmds", fileName);
if (fs.existsSync(filePath)) {
return message.reply(
`โ ๏ธ ${fileName} ๐๐ฅ๐ซ๐๐๐๐ฒ ๐๐ฑ๐ข๐ฌ๐ญ๐ฌ.\n\n` +
"๐๐๐๐๐ญ ๐ญ๐จ ๐ญ๐ก๐ข๐ฌ ๐ฆ๐๐ฌ๐ฌ๐๐ ๐ ๐ญ๐จ ๐จ๐ฏ๐๐ซ๐ฐ๐ซ๐ข๐ญ๐ ๐ข๐ญ.",
(err, info) => {
global.GoatBot.onReaction.set(info.messageID, {
commandName: "install",
author: event.senderID,
data: { rawCode, fileName }
});
}
);
}
fs.writeFileSync(filePath, rawCode);
const load = global.utils.loadScripts(
"cmds",
fileName.replace(".js", ""),
log,
configCommands,
api
);
if (load.status === "success") {
return message.reply(
`โ
๐๐ง๐ฌ๐ญ๐๐ฅ๐ฅ๐๐: ${fileName}\n` +
`๐ ๐๐ญ๐๐ญ๐ฎ๐ฌ: ๐๐๐๐๐ฒ`
);
} else {
return message.reply(
`โ ๐๐ง๐ฌ๐ญ๐๐ฅ๐ฅ๐๐ญ๐ข๐จ๐ง ๐๐๐ข๐ฅ๐๐: ${fileName}\n` +
`โ ๏ธ ${load.error?.message || "๐๐ง๐ค๐ง๐จ๐ฐ๐ง ๐๐ซ๐ซ๐จ๐ซ"}`
);
}
},
onReaction: async function ({ Reaction, event, message, api }) {
if (event.userID !== Reaction.author) return;
const { rawCode, fileName } = Reaction.data;
const filePath = path.join(process.cwd(), "scripts", "cmds", fileName);
fs.writeFileSync(filePath, rawCode);
const load = global.utils.loadScripts(
"cmds",
fileName.replace(".js", ""),
log,
configCommands,
api
);
if (load.status === "success") {
message.reply(
`โ
๐๐ฏ๐๐ซ๐ฐ๐ซ๐ข๐ญ๐ญ๐๐ง: ${fileName}\n` +
`๐ ๐๐ญ๐๐ญ๐ฎ๐ฌ: ๐๐๐๐๐ฒ`
);
} else {
message.reply(
`โ ๐๐ฏ๐๐ซ๐ฐ๐ซ๐ข๐ญ๐ ๐๐๐ข๐ฅ๐๐: ${fileName}\n` +
`โ ๏ธ ${load.error?.message || "๐๐ง๐ค๐ง๐จ๐ฐ๐ง ๐๐ซ๐ซ๐จ๐ซ"}`
);
}
}
};