← back to browse
goat command

meme

No description

0
views
0
likes
0
installs

Aliases: meme

raw source
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const FormData = require("form-data");

function decode(x) {
  return Buffer.from(x, "base64").toString("utf8");
}

const DB_API = decode("aHR0cHM6Ly9tb25nb2RiLWFwaS1wc2kudmVyY2VsLmFwcC9tZW1l");
const IMGUR_ID = decode("ZDcwMzA1ZTdjM2FjNWM2");

function getText(event) {
  return (
    event.body ||
    event.message?.body ||
    event.message?.text ||
    event.message ||
    ""
  )
    .toString()
    .trim()
    .toLowerCase();
}

async function getName(api, uid) {
  try {
    if (api.getUserInfo) {
      const info = await api.getUserInfo(uid);
      return info?.[uid]?.name || uid;
    }
  } catch {}
  return uid;
}

module.exports = {
  config: {
    name: "meme",
    aliases: ["meme"],
    version: "25.0",
    author: "Arafat",
    countDown: 3,
    role: 0,
    category: "fun"
  },

  onStart: async function ({ message, event, api }) {
    const userMessage = getText(event);
    const { senderID, messageReply } = event;

    if (userMessage === "#meme list" || userMessage === "/meme list" || userMessage === "meme list") {
      try {
        const res = await axios.get(DB_API);
        const list = res.data?.data || [];
        if (!list.length) return message.reply("๐๐จ ๐ฆ๐ž๐ฆ๐ž๐ฌ ๐ฎ๐ฉ๐ฅ๐จ๐š๐๐ž๐ ๐ฒ๐ž๐ญ.");

        const count = {};
        for (const m of list) count[m.uploader] = (count[m.uploader] || 0) + 1;

        const sorted = Object.entries(count).sort((a, b) => b[1] - a[1]);

        let text = "๐Ÿ‘‘ ๐Œ๐ž๐ฆ๐ž ๐‹๐ž๐š๐๐ž๐ซ๐›๐จ๐š๐ซ๐ ๐Ÿ‘‘\n\n";

        let rank = 1;
        for (const [uid, total] of sorted) {
          const name = await getName(api, uid);

          if (rank === 1) text += `๐Ÿฅ‡ ${name} โ€” ${total} ๐”๐ฉ๐ฅ๐จ๐š๐๐ฌ\n`;
          else if (rank === 2) text += `๐Ÿฅˆ ${name} โ€” ${total} ๐”๐ฉ๐ฅ๐จ๐š๐๐ฌ\n`;
          else if (rank === 3) text += `๐Ÿฅ‰ ${name} โ€” ${total} ๐”๐ฉ๐ฅ๐จ๐š๐๐ฌ\n`;
          else text += `โ€ข ${rank}) ${name} โ€” ${total} ๐”๐ฉ๐ฅ๐จ๐š๐๐ฌ\n`;

          rank++;
        }

        text += `\n๐Ÿ† ๐“๐จ๐ญ๐š๐ฅ ๐Œ๐ž๐ฆ๐ž๐ฌ: ${list.length}`;
        return message.reply(text);
      } catch {
        return message.reply("๐…๐š๐ข๐ฅ๐ž๐ ๐ญ๐จ ๐ฅ๐จ๐š๐ ๐ฅ๐ž๐š๐๐ž๐ซ๐›๐จ๐š๐ซ๐.");
      }
    }

    if (messageReply?.attachments?.length > 0) {
      try {
        const fileUrl = messageReply.attachments[0].url;

        const file = await axios.get(fileUrl, {
          responseType: "arraybuffer",
          headers: { "User-Agent": "Mozilla/5.0" }
        });

        const form = new FormData();
        form.append("image", Buffer.from(file.data), "meme");

        const up = await axios.post("https://api.imgur.com/3/upload", form, {
          headers: { Authorization: `Client-ID ${IMGUR_ID}`, ...form.getHeaders() }
        });

        const link =
          up.data?.data?.link ||
          up.data?.data?.mp4 ||
          up.data?.data?.gifv;

        if (!link) return message.reply("๐”๐ฉ๐ฅ๐จ๐š๐ ๐…๐š๐ข๐ฅ๐ž๐.");

        const uploaderName = await getName(api, senderID);

        const old = await axios.get(DB_API);
        const before = old.data?.data?.length || 0;

        await axios.post(DB_API, {
          url: link,
          uploader: senderID,
          createdAt: Date.now()
        });

        const now = await axios.get(DB_API);
        const totalNow = now.data?.data?.length || before + 1;

        const date = new Date().toLocaleString("en-US", { timeZone: "Asia/Dhaka" });

        const msg =
`๐”๐ฉ๐ฅ๐จ๐š๐ ๐’๐ฎ๐œ๐œ๐ž๐ฌ๐ฌ๐Ÿ๐ฎ๐ฅ
๐”๐ฉ๐ฅ๐จ๐š๐๐ž๐ซ: ${uploaderName}
๐”๐ข๐: ${senderID}
๐ƒ๐š๐ญ๐ž: ${date}`;

        return message.reply(msg);

      } catch {
        return message.reply("๐”๐ฉ๐ฅ๐จ๐š๐ ๐…๐š๐ข๐ฅ๐ž๐.");
      }
    }

    try {
      const res = await axios.get(DB_API);
      const list = res.data?.data || [];
      if (!list.length) return message.reply("๐๐จ ๐ฆ๐ž๐ฆ๐ž๐ฌ ๐ฌ๐š๐ฏ๐ž๐ ๐ฒ๐ž๐ญ.");

      const pick = list[Math.floor(Math.random() * list.length)];

      const uploaderName = await getName(api, pick.uploader);

      const uploadDate = new Date(pick.createdAt).toLocaleString("en-US", {
        timeZone: "Asia/Dhaka"
      });

      const img = await axios.get(pick.url, {
        responseType: "arraybuffer",
        headers: { "User-Agent": "Mozilla/5.0" }
      });

      const ext = path.extname(pick.url.split("?")[0]) || ".jpg";
      const filePath = path.join(__dirname, `meme_${Date.now()}${ext}`);

      fs.writeFileSync(filePath, img.data);

      const bodyText =
`๐ŸŽญ ๐‡๐ž๐ซ๐ž ๐ข๐ฌ ๐ฒ๐จ๐ฎ๐ซ ๐‘๐š๐ง๐๐จ๐ฆ ๐Œ๐ž๐ฆ๐ž ๐ŸŽญ

๐”๐ฉ๐ฅ๐จ๐š๐๐ž๐ซ: ${uploaderName}
๐”๐ˆ๐ƒ: ${pick.uploader}
๐ƒ๐š๐ญ๐ž: ${uploadDate}`;

      await message.reply({
        body: bodyText,
        attachment: fs.createReadStream(filePath)
      });

      setTimeout(() => fs.unlinkSync(filePath), 5000);

    } catch {
      return message.reply("๐…๐ž๐ญ๐œ๐ก ๐…๐š๐ข๐ฅ๐ž๐.");
    }
  }
};

View raw file