const axios = require("axios");
const fs = require('fs');
function formatSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
module.exports = {
config: {
name: "play",
version: "1.0",
author: "Your Name",
countDown: 10,
role: 0,
shortDescription: "Get lyrics and audio from Spotify",
longDescription: "Get lyrics and audio from Spotify for a given song title",
category: "music",
guide: "{pn} <song title>"
},
onStart: async function ({ api, event, args, message }) {
const query = args.join(" ");
if (!query) {
return message.reply("Please provide a song title.");
}
// Fetching lyrics
const lyricsApiUrl = `https://lyrist.vercel.app/api/${encodeURIComponent(query)}`;
try {
const lyricsResponse = await axios.get(lyricsApiUrl);
const { lyrics, title, artist } = lyricsResponse.data;
let combinedMessage = "";
if (lyrics) {
combinedMessage += `? | Title: ${title}\n? | Artist: ${artist}\n\n${lyrics}\n\n`;
} else {
combinedMessage += `? | Sorry, lyrics for "${query}" not found!\n\n`;
}
// Fetching Spotify audio
const searchApiUrl = `https://for-devs.onrender.com/api/spsearch?apikey=fuck&query;=${encodeURIComponent(query)}`;
const searchResponse = await axios.get(searchApiUrl);
const tracks = searchResponse.data.slice(0, 6);
if (tracks.length === 0) {
combinedMessage += "❎ No tracks found on Spotify for the given query.";
} else {
const topTrack = tracks[0];
const dlApiUrl = `https://for-devs.onrender.com/api/spotifydl?apikey=fuck&url;=${encodeURIComponent(topTrack.url)}`;
const dlResponse = await axios.get(dlApiUrl);
if (dlResponse.data.id) {
const { title, downloadUrl } = dlResponse.data;
const audioResponse = await axios.get(downloadUrl, { responseType: 'arraybuffer' });
fs.writeFileSync(__dirname + '/cache/spotifyAudio.mp3', Buffer.from(audioResponse.data));
const fileSize = fs.statSync(__dirname + '/cache/spotifyAudio.mp3').size;
const sizeFormatted = formatSize(fileSize);
combinedMessage += `? | Downloaded audio: ${title} (${sizeFormatted})`;
const attachment = fs.createReadStream(__dirname + '/cache/spotifyAudio.mp3');
const form = {
body: combinedMessage,
attachment: attachment
};
message.reply(form);
} else {
combinedMessage += "❎ Sorry, the Spotify content could not be downloaded.";
message.reply(combinedMessage);
}
}
} catch (error) {
console.error(error);
api.sendMessage("Error: " + error, event.threadID);
}
},
};