Skip to content

Instantly share code, notes, and snippets.

@altzzdevs
Created February 10, 2026 05:51
Show Gist options
  • Select an option

  • Save altzzdevs/e7b07a8d0af5d44eba403f396bac6b0e to your computer and use it in GitHub Desktop.

Select an option

Save altzzdevs/e7b07a8d0af5d44eba403f396bac6b0e to your computer and use it in GitHub Desktop.
// plugins/play.js
// .play <song name or youtube link> β†’ download & send MP3
import axios from 'axios';
export default {
cmd: ['play', 'song', 'ytmp3', 'audio'],
desc: 'Search YouTube and send MP3 audio using Okatsu API',
async execute(m, { sock, text }) {
if (!text) {
return sock.sendMessage(m.key.remoteJid, {
text: "🎡 *Usage:* .play <song name or YouTube link>\nExample: .play shape of you"
});
}
await sock.sendMessage(m.key.remoteJid, {
text: "πŸ” Searching YouTube & downloading audio... ⏳"
});
try {
// Step 1: If not a full URL, search YouTube for the best match
let videoUrl = text;
if (!text.includes('youtube.com') && !text.includes('youtu.be')) {
const searchQuery = encodeURIComponent(text);
// Using a free public YouTube search API (you can replace with better one)
const searchRes = await axios.get(
`https://www.youtube.com/results?search_query=${searchQuery}`
);
// Very basic regex parse - find first video ID (not perfect, but works for many bots)
const videoIdMatch = searchRes.data.match(/"videoId":"([^"]+)"/);
if (!videoIdMatch) throw new Error("No video found");
videoUrl = `https://www.youtube.com/watch?v=${videoIdMatch[1]}`;
}
// Step 2: Call the MP3 downloader API
const apiUrl = `https://okatsu-rolezapiiz.vercel.app/downloader/ytmp3?url=${encodeURIComponent(videoUrl)}`;
const apiRes = await axios.get(apiUrl, { timeout: 45000 });
const data = apiRes.data;
if (!data?.status || !data?.download_url) {
throw new Error(data?.message || "API failed to get download link");
}
const title = data.title || "Unknown Title";
const dlUrl = data.download_url;
const thumb = data.thumbnail || null;
// Optional: send thumbnail + caption first
let caption = `🎧 *${title}*\n\nDownloaded via SUHO LITE\nPowered by π™Žπ™π™ƒπ™Š π™π™€π˜Ύπ™ƒ β€’ Lord Sung`;
if (thumb) {
await sock.sendMessage(m.key.remoteJid, {
image: { url: thumb },
caption: caption + "\n\nSending audio... πŸ”Š"
});
} else {
await sock.sendMessage(m.key.remoteJid, { text: caption + "\n\nSending audio... πŸ”Š" });
}
// Step 3: Download the MP3 buffer and send as audio
const audioRes = await axios.get(dlUrl, { responseType: 'arraybuffer' });
const buffer = Buffer.from(audioRes.data);
await sock.sendMessage(m.key.remoteJid, {
audio: buffer,
mimetype: 'audio/mpeg',
fileName: `${title}.mp3`,
ptt: false, // set true if you want voice-note style
contextInfo: {
externalAdReply: {
title: title,
body: "SUHO LITE β€’ Play Command",
sourceUrl: videoUrl,
mediaUrl: videoUrl,
thumbnailUrl: thumb || undefined,
mediaType: 2,
renderLargerThumbnail: true
}
}
});
} catch (err) {
console.error('[play cmd error]', err);
let errMsg = "❌ Failed to download/play song.";
if (err.message.includes("timeout")) errMsg += "\nAPI took too long.";
if (err.message.includes("No video found")) errMsg += "\nCouldn't find that song on YouTube.";
await sock.sendMessage(m.key.remoteJid, { text: errMsg + "\n\nTry again or use a direct YT link!" });
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment