← back to browse
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 || "๐”๐ง๐ค๐ง๐จ๐ฐ๐ง ๐ž๐ซ๐ซ๐จ๐ซ"}`
      );
    }
  }
};

View raw file