← back to browse
goat command

ai-song

Generate AI songs

0
views
0
likes
0
installs

Aliases: aisong, aimusic

raw source
const axios = require("axios");
const fs = require("fs-extra");
const path = require("path");

const GENRES = {
  1: "Phonk",
  2: "Lo-fi",
  3: "EDM",
  4: "Hip Hop",
  5: "Pop",
  6: "Rock",
  7: "Jazz",
  8: "Trap",
  9: "R&B",
  10: "Classical",
  11: "Cinematic",
  12: "Experimental"
};

const STYLES = {
  1: "Aggressive energy",
  2: "Deep 808 bass",
  3: "Powerful vocals",
  4: "Chill vibe",
  5: "Emotional",
  6: "Epic",
  7: "Sad",
  8: "Romantic",
  9: "Happy",
  10: "Dreamy",
  11: "Energetic"
};

const DEFAULT_GENRE = "Phonk";
const DEFAULT_STYLE = "Energetic";
const DEFAULT_DURATION = 120;

module.exports = {
  config: {
    name: "ai-song",
    aliases: ["aisong", "aimusic"],
    version: "3.0",
    author: "๐’๐ˆ๐€๐Œ ๐€๐‡๐Œ๐„๐ƒ ๐’๐€๐€๐",
    countDown: 20,
    role: 0,
    shortDescription: "Generate AI songs",
    longDescription: "Generate custom AI songs using prompt, genre, style and duration.",
    category: "AI-MUSIC",

    guide: {
      en: `
โ•ญโ”โ”โ”ใ€” ๐ŸŽต AI SONG GENERATOR ใ€•โ”โ”โ”โ•ฎ

๐Ÿ“ ๐—•๐—ฎ๐˜€๐—ถ๐—ฐ:
{pn} <prompt>
โ€บ {pn} A romantic song about Bangladesh

๐ŸŽ›๏ธ ๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐—ฐ๐—ฒ๐—ฑ:
{pn} <prompt> --g <1-12> --s <1-11> --d <6-120>
โ€บ {pn} My Song --g 1 --s 2 --d 60

๐ŸŽผ ๐—š๐—ฒ๐—ป๐—ฟ๐—ฒ ๐—œ๐——๐˜€:
 1. Phonk         7. Jazz
 2. Lo-fi         8. Trap
 3. EDM           9. R&B
 4. Hip Hop      10. Classical
 5. Pop          11. Cinematic
 6. Rock         12. Experimental

๐ŸŽจ ๐—ฆ๐˜๐˜†๐—น๐—ฒ ๐—œ๐——๐˜€:
 1. Aggressive energy    7. Sad
 2. Deep 808 bass        8. Romantic
 3. Powerful vocals      9. Happy
 4. Chill vibe          10. Dreamy
 5. Emotional           11. Energetic
 6. Epic

โฑ๏ธ ๐——๐˜‚๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: 6 โ€“ 120s (Default: 120)

๐Ÿ’ก Missing any flag โ†’ default is used.

โ•ฐโ”โ”โ”ใ€” โšก Powered by ๐’๐ˆ๐€๐Œ ๐€๐‡๐Œ๐„๐ƒ ๐’๐€๐€๐  ใ€•โ”โ”โ”โ•ฏ
`
    }
  },

  onStart: async function ({ api, event, args }) {
    const { threadID, messageID } = event;
    const input = args.join(" ").trim();

    if (!input) {
      return api.sendMessage(
        `๐ŸŽต ๐—”๐—œ ๐—ฆ๐—ข๐—ก๐—š ๐—š๐—˜๐—ก๐—˜๐—ฅ๐—”๐—ง๐—ข๐—ฅ

๐Ÿ“ Please provide a song prompt.

โ€บ Basic:  {pn} My Bangladesh Song
โ€บ Custom: {pn} My Song --g 1 --s 2 --d 60

Type {pn} help for full guide.`,
        threadID,
        messageID
      );
    }

    if (input.toLowerCase() === "help" || input.toLowerCase() === "-h") {
      return api.sendMessage(
        this.config.guide.en.replace(/\{pn\}/g, "ai-song"),
        threadID,
        messageID
      );
    }


    let genre = DEFAULT_GENRE;
    let style = DEFAULT_STYLE;
    let duration = DEFAULT_DURATION;

    const gMatch = input.match(/--g\s+(\d+)/i);
    const sMatch = input.match(/--s\s+(\d+)/i);
    const dMatch = input.match(/--d\s+(\d+)/i);

    if (gMatch) {
      const id = parseInt(gMatch[1]);
      if (GENRES[id]) genre = GENRES[id];
    }

    if (sMatch) {
      const id = parseInt(sMatch[1]);
      if (STYLES[id]) style = STYLES[id];
    }

    if (dMatch) {
      const d = parseInt(dMatch[1]);
      if (!isNaN(d)) duration = Math.max(6, Math.min(120, d));
    }

    const prompt = input
      .replace(/--g\s+\d+/i, "")
      .replace(/--s\s+\d+/i, "")
      .replace(/--d\s+\d+/i, "")
      .trim();

    if (!prompt) {
      return api.sendMessage("โŒ Please provide a song prompt.", threadID, messageID);
    }

    const cacheDir = path.join(__dirname, "cache");
    await fs.ensureDir(cacheDir);

    const filePath = path.join(
      cacheDir,
      `xalman_ai_song_${Date.now()}_${Math.random().toString(36).slice(2)}.mp3`
    );

    let loadingMessage = null;

    try {
      api.setMessageReaction("โณ", messageID, () => {}, true);

      loadingMessage = await api.sendMessage(
        `๐ŸŽต Generating AI Song...\n\n๐Ÿ“ ${prompt}\n๐ŸŽผ ${genre} โ€ข ๐ŸŽจ ${style}\nโฑ๏ธ ${duration}s\n\nโšก Please wait...`,
        threadID
      );

      const response = await axios.get(
        "https://xalman-apis.vercel.app/api/ai-song",
        {
          params: { prompt, genre, style, duration },
          responseType: "arraybuffer",
          timeout: 180000,
          maxContentLength: 100 * 1024 * 1024,
          maxBodyLength: 100 * 1024 * 1024
        }
      );

      const audioBuffer = Buffer.from(response.data);

      if (!audioBuffer || audioBuffer.length < 1000) {
        throw new Error("Invalid or empty audio received.");
      }

      const contentType = String(response.headers["content-type"] || "").toLowerCase();
      if (contentType.includes("application/json") || contentType.includes("text/html")) {
        throw new Error("API returned an invalid response.");
      }

      await fs.writeFile(filePath, audioBuffer);

      if (loadingMessage?.messageID) {
        try { await api.unsendMessage(loadingMessage.messageID); } catch {}
      }

      api.setMessageReaction("๐ŸŽง", messageID, () => {}, true);

      return api.sendMessage(
        {
          body: `๐ŸŽต AI Song Generated\n๐Ÿ“ ${prompt}\n๐ŸŽผ ${genre} โ€ข ๐ŸŽจ ${style} โ€ข โฑ๏ธ ${duration}s`,
          attachment: fs.createReadStream(filePath)
        },
        threadID,
        async error => {
          try {
            if (await fs.pathExists(filePath)) await fs.remove(filePath);
          } catch {}

          if (error) {
            console.error("Audio send error:", error);
            api.setMessageReaction("โŒ", messageID, () => {}, true);
          }
        },
        messageID
      );

    } catch (error) {
      console.error("AI Song Error:", error?.response?.data || error);

      api.setMessageReaction("โŒ", messageID, () => {}, true);

      if (loadingMessage?.messageID) {
        try { await api.unsendMessage(loadingMessage.messageID); } catch {}
      }

      try {
        if (await fs.pathExists(filePath)) await fs.remove(filePath);
      } catch {}

      return api.sendMessage(
        `โŒ AI Song Generation Failed\n\nโš ๏ธ ${error.message || "Unknown API error"}\n\nPlease try again.`,
        threadID,
        messageID
      );
    }
  }
};

View raw file