someone fix this please, i give up

This commit is contained in:
Amane Serenetia 2024-09-19 08:32:02 +07:00
parent 4427ca7f8c
commit 0b12f3cde7

@ -1,66 +1,129 @@
const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js");
const { getJson } = require("@helpers/HttpUtils");
const { MESSAGES, EMBED_COLORS } = require("@root/config");
const BASE_URL = "https://some-random-api.com/lyrics";
const fetch = require('node-fetch');
/**
* @type {import("@structures/Command")}
*/
module.exports = {
name: "lyric",
description: "find lyric of the song",
description: "get lyrics for the current song or a specified song",
category: "MUSIC",
botPermissions: ["EmbedLinks"],
command: {
enabled: true,
minArgsCount: 1,
usage: "<Song Title - singer>",
usage: "[song name]",
},
slashCommand: {
enabled: true,
options: [
{
name: "query",
description: "song name to search lyrics for",
type: ApplicationCommandOptionType.String,
description: "find lyric of the song",
required: true,
required: false,
},
],
},
async messageRun(message, args) {
const choice = args.join(" ");
if (!choice) {
return message.safeReply("Invalid Lyric selected.");
}
const response = await getLyric(message.author, choice);
return message.safeReply(response);
const query = args.join(" ");
const response = await getLyric(message, query);
await message.safeReply(response);
},
async interactionRun(interaction) {
const choice = interaction.options.getString("query");
const response = await getLyric(interaction.user, choice);
const query = interaction.options.getString("query");
const response = await getLyric(interaction, query);
await interaction.followUp(response);
},
};
async function getLyric(user, choice) {
const lyric = await getJson(`${BASE_URL}?title=${choice}`);
if (!lyric.success) return MESSAGES.API_ERROR;
async function getLyric({ client, guild, member }, query) {
const player = client.musicManager.players.resolve(guild.id);
const thumbnail = lyric.data?.thumbnail.genius;
const author = lyric.data?.author;
const lyrics = lyric.data?.lyrics;
const title = lyric.data?.title;
if (!player) {
return "🚫 There's no active music player in this server.";
}
const embed = new EmbedBuilder();
embed
let track;
const node = player.node;
try {
if (!query) {
// Get lyrics for currently playing song
if (!player.queue.current) {
return "🚫 No music is currently playing!";
}
track = player.queue.current;
// Fetch lyrics for the current track
const lyricsUrl = `${node.rest.url}/v4/sessions/${node.sessionId}/players/${guild.id}/lyrics`;
const lyricsRes = await fetch(lyricsUrl, {
headers: { Authorization: node.rest.authorization }
});
if (!lyricsRes.ok) {
throw new Error(`Failed to fetch lyrics: ${lyricsRes.status} ${lyricsRes.statusText}`);
}
const lyrics = await lyricsRes.json();
return createLyricsEmbed(track, lyrics, member);
} else {
// Search for lyrics
const searchUrl = `${node.rest.url}/v4/lyrics/search?query=${encodeURIComponent(query)}`;
const searchRes = await fetch(searchUrl, {
headers: { Authorization: node.rest.authorization }
});
if (!searchRes.ok) {
throw new Error(`Failed to search lyrics: ${searchRes.status} ${searchRes.statusText}`);
}
const searchData = await searchRes.json();
if (!searchData || !searchData.tracks || searchData.tracks.length === 0) {
return "No lyrics found for the given query.";
}
track = searchData.tracks[0];
// Fetch lyrics for the found track
const lyricsUrl = `${node.rest.url}/v4/lyrics/${track.videoId}`;
const lyricsRes = await fetch(lyricsUrl, {
headers: { Authorization: node.rest.authorization }
});
if (!lyricsRes.ok) {
throw new Error(`Failed to fetch lyrics: ${lyricsRes.status} ${lyricsRes.statusText}`);
}
const lyrics = await lyricsRes.json();
return createLyricsEmbed(track, lyrics, member);
}
} catch (error) {
client.logger.error("Lyric Command Error:", error);
return "An error occurred while fetching the lyrics. Please try again later.";
}
}
function createLyricsEmbed(track, lyrics, member) {
if (!lyrics || (!lyrics.lyrics && !lyrics.lines)) {
return "No lyrics found for this song.";
}
const embed = new EmbedBuilder()
.setColor(EMBED_COLORS.BOT_EMBED)
.setTitle(`${author} - ${title}`)
.setThumbnail(thumbnail)
.setDescription(lyrics)
.setFooter({ text: `Request By: ${user.username}` });
.setTitle(`${track.author} - ${track.title}`)
.setThumbnail(track.artworkUrl)
.setFooter({ text: `Requested by: ${member.user.username} | Source: ${lyrics.source || 'Unknown'}` });
if (lyrics.lyrics) {
embed.setDescription(lyrics.lyrics.length > 4096 ? lyrics.lyrics.slice(0, 4093) + "..." : lyrics.lyrics);
} else if (lyrics.lines) {
const lyricsText = lyrics.lines.map(line => line.words).join('\n');
embed.setDescription(lyricsText.length > 4096 ? lyricsText.slice(0, 4093) + "..." : lyricsText);
}
return { embeds: [embed] };
}
}