← back to browse
goat command

autotimer

Automatic Islamic timer

0
views
0
likes
0
installs
raw source
const axios = require("axios");
const fs = require("fs-extra");
const path = require("path");
const moment = require("moment-timezone");

const CACHE_DIR = path.join(__dirname, "cache", "autotimer");
fs.ensureDirSync(CACHE_DIR);

const schedules = [
  ["12:00 AM", "https://files.catbox.moe/7ch5ym.mp4"],
  ["01:00 AM", "https://files.catbox.moe/rqnlpt.mp4"],
  ["02:00 AM", "https://files.catbox.moe/ev7guv.mp4"],
  ["03:00 AM", "https://files.catbox.moe/rijx8m.mp4"],
  ["04:30 AM", "https://files.catbox.moe/ee9khu.mp4"],
  ["06:00 AM", "https://files.catbox.moe/otdztt.mp4"],
  ["07:00 AM", "https://files.catbox.moe/q6b7fo.mp4"],
  ["08:00 AM", "https://files.catbox.moe/jxa3ka.mp4"],
  ["09:00 AM", "https://files.catbox.moe/wtu9vw.mp4"],
  ["10:00 AM", "https://files.catbox.moe/guv0tc.mp4"],
  ["11:00 AM", "https://files.catbox.moe/aecxiz.mp4"],
  ["12:00 PM", "https://files.catbox.moe/wrc15v.mp4"],
  ["01:00 PM", "https://files.catbox.moe/c5qbek.mp4"],
  ["02:00 PM", "https://files.catbox.moe/vgecfk.mp4"],
  ["03:00 PM", "https://files.catbox.moe/iddna6.mp4"],
  ["04:30 PM", "https://files.catbox.moe/vcgbxq.mp4"],
  ["06:30 PM", "https://files.catbox.moe/y8pnz7.mp4"],
  ["08:00 PM", "https://files.catbox.moe/rpnut9.mp4"],
  ["09:00 PM", "https://files.catbox.moe/fpac7y.mp4"],
  ["10:00 PM", "https://files.catbox.moe/e7v8en.mp4"],
  ["11:00 PM", "https://files.catbox.moe/7bas7j.mp4"]
];

let timer = null;
let running = false;
let lastSent = "";

async function downloadVideo(url) {
  const response = await axios.get(url, {
    responseType: "arraybuffer",
    timeout: 120000,
    maxContentLength: 100 * 1024 * 1024,
    maxBodyLength: 100 * 1024 * 1024
  });

  return Buffer.from(response.data);
}

async function sendToGroups(api, videoBuffer, time) {
  const threadList = await api.getThreadList(
    1000,
    null,
    ["INBOX"]
  );

  const groups = threadList.filter(
    thread =>
      thread &&
      thread.isGroup &&
      thread.threadID
  );

  const message = {
    body:
      `☪️ Mehedi-Bot\n\n` +
      `🕐 সময়: ${time}\n` +
      `📖 ইসলামিক রিমাইন্ডার\n\n` +
      `🤲 আল্লাহ আমাদের সবাইকে হেদায়েত দান করুন।`
  };

  for (const group of groups) {
    try {
      await api.sendMessage(
        {
          ...message,
          attachment: videoBuffer
        },
        group.threadID
      );

      await new Promise(resolve => setTimeout(resolve, 1000));
    } catch (err) {
      console.error(
        `Send failed: ${group.threadID}`,
        err.message
      );
    }
  }
}

async function checkAndSend(api) {
  if (!running) return;

  const now = moment().tz("Asia/Dhaka");
  const currentTime = now.format("hh:mm A");
  const currentDate = now.format("YYYY-MM-DD");

  const uniqueKey = `${currentDate}_${currentTime}`;

  if (uniqueKey === lastSent) return;

  const schedule = schedules.find(
    item => item[0] === currentTime
  );

  if (!schedule) return;

  lastSent = uniqueKey;

  const [time, videoUrl] = schedule;

  console.log(`⏰ Autotimer: ${time}`);

  try {
    const videoBuffer = await downloadVideo(videoUrl);

    await sendToGroups(
      api,
      videoBuffer,
      time
    );

    console.log(`✅ Video sent: ${time}`);
  } catch (error) {
    console.error(
      `❌ Autotimer error at ${time}:`,
      error.message
    );
  }
}

module.exports = {
  config: {
    name: "autotimer",
    version: "4.0.0",
    author: "Mehedi",
    countDown: 5,
    role: 2,
    shortDescription: "Automatic Islamic timer",
    longDescription: "Sends one scheduled Islamic video to all groups.",
    category: "system",
    guide: "{pn}"
  },

  onStart: async function ({ api, event }) {
    if (running) {
      return api.sendMessage(
        "✅ Autotimer already running.",
        event.threadID
      );
    }

    running = true;

    await checkAndSend(api);

    timer = setInterval(() => {
      checkAndSend(api).catch(err => {
        console.error("Autotimer:", err.message);
      });
    }, 30000);

    return api.sendMessage(
      "✅ Mehedi-Bot Autotimer started!\n\n" +
      "🇧🇩 Bangladesh Time: Asia/Dhaka\n" +
      "🎬 প্রতি scheduled time-এ ১টি video পাঠানো হবে।",
      event.threadID
    );
  }
};

View raw file