← back to browse
goat command

play

No description

0
views
0
likes
0
installs
raw source
const axios = require('axios');

const PRIMARY_API = "https://melissa-api-hub-1.onrender.com/Melissa-api-hub/badhon-00-api/apis/melissa/sing?q=";
const FALLBACK_API = "https://melissa-api-hub-1.onrender.com/api/sing/search?limit=5&q=";

module.exports = {
  config: {
    name: "play",
    author: "BADHON-BBZ",
    version: "3.0.0",
    role: 0,
    category: "tools",
    usage: "{pn} <query>"
  },

  react: async function ({ message, api, event }, emoji) {
    try {
      if (message && typeof message.react === "function") {
        await message.react(emoji);
        return;
      }
      if (api && typeof api.setMessageReaction === "function" && event) {
        await api.setMessageReaction(emoji, event.messageID, () => {}, true);
      }
    } catch (e) {
      console.log("[sing] reaction failed:", e.message);
    }
  },

  fetchAudioStream: async function (audioUrl, retries = 1) {
    let lastErr;
    for (let i = 0; i <= retries; i++) {
      try {
        return await axios.get(audioUrl, {
          responseType: 'stream',
          timeout: 30000
        });
      } catch (e) {
        lastErr = e;
        console.log(`[sing] audio fetch attempt ${i + 1} failed:`, e.message);
      }
    }
    throw lastErr;
  },

  onStart: async function ({ message, args, api, event }) {
    const query = args.join(" ");
    if (!query) return message.reply("Please provide a song name.");

    await module.exports.react({ message, api, event }, "⏳");

    try {
      let data;
      try {
        const res = await axios.get(PRIMARY_API + encodeURIComponent(query), { timeout: 10000 });
        data = res.data;
      } catch (e) {
        const res = await axios.get(FALLBACK_API + encodeURIComponent(query), { timeout: 10000 });
        data = res.data;
      }

      const audioUrl = data?.download_url || data?.audio_url || data?.result?.download_url;
      const title = data?.title || data?.result?.title || query;

      if (!audioUrl) {
        await module.exports.react({ message, api, event }, "❌");
        return message.reply("Couldn't find that song.");
      }

      let audioRes;
      try {
        audioRes = await module.exports.fetchAudioStream(audioUrl, 1);
      } catch (e) {
        await module.exports.react({ message, api, event }, "❌");
        return message.reply("The audio server is taking too long to respond. Please try again in a bit.");
      }

      await message.reply({
        body: `🎵 ${title}`,
        attachment: audioRes.data
      });

      await module.exports.react({ message, api, event }, "✅");

    } catch (err) {
      await module.exports.react({ message, api, event }, "❌");
      message.reply("Error: " + (err.response?.data?.error || err.message));
    }
  }
};

View raw file