From eb09feee6a8ab9243722e003bd6b99bdd01511d8 Mon Sep 17 00:00:00 2001 From: frostice482 Date: Thu, 19 Sep 2024 18:32:15 +0700 Subject: [PATCH] i fixed it --- src/commands/music/lyric.js | 155 +++++++++++++++++++++--------------- 1 file changed, 90 insertions(+), 65 deletions(-) diff --git a/src/commands/music/lyric.js b/src/commands/music/lyric.js index ebf5400..40cdb37 100644 --- a/src/commands/music/lyric.js +++ b/src/commands/music/lyric.js @@ -1,6 +1,6 @@ +//@ts-check const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js"); const { MESSAGES, EMBED_COLORS } = require("@root/config"); -const fetch = require('node-fetch'); /** * @type {import("@structures/Command")} @@ -39,91 +39,116 @@ module.exports = { }, }; +/** + * @param {import('discord.js').Message} param0 + * @param {string} query ☺ + */ async function getLyric({ client, guild, member }, query) { - const player = client.musicManager.players.resolve(guild.id); - - if (!player) { - return "🚫 There's no active music player in this server."; - } - - let track; - const node = player.node; + /** @type {Response} */ + let res; try { if (!query) { - // Get lyrics for currently playing song - if (!player.queue.current) { - return "🚫 No music is currently playing!"; - } - track = player.queue.current; + /** @type { import('lavaclient').Player } */ + const player = client.musicManager.players.resolve(guild.id); + + if (!player) return "🚫 There's no active music player in this server."; // 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); + const api = player.node.api; + res = await api.client.execute({ path: `/v4/sessions/${player.api.session.id}/players/${guild.id}/lyrics`, method: 'GET' }) } else { + /** @type {import('lavaclient').ClusterNode} */ + const [[, node]] = client.musicManager.nodes + const api = node.api + // 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); + res = await api.client.execute({ path: `/v4/lyrics/search?query=${encodeURIComponent(query)}&source=genius`, method: 'GET' }) } + + // throws error anyway + if (res.status === 404) return "Lyric not found" + if (!res.ok) throw new Error(`Failed to search lyrics: ${res.status} ${res.statusText}`); + + const lyricData = await res.json() + return createLyricsEmbed(lyricData, 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)) { +/** + * @param {LyricResult} lyrics + * @param {import('discord.js').GuildMember} member + * @returns + */ +function createLyricsEmbed(lyrics, member) { + if (!lyrics) { return "No lyrics found for this song."; } + const track = lyrics.track + const embed = new EmbedBuilder() .setColor(EMBED_COLORS.BOT_EMBED) .setTitle(`${track.author} - ${track.title}`) - .setThumbnail(track.artworkUrl) - .setFooter({ text: `Requested by: ${member.user.username} | Source: ${lyrics.source || 'Unknown'}` }); + .setThumbnail(track.albumArt[0].url) + .setFooter({ text: `Requested by: ${member} | 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); - } + const ltext = lyrics.type === 'text' ? lyrics.text : lyrics.lines.map(v => v.line).join('\n') + embed.setDescription(ltext.length > 4096 ? ltext.slice(0, 4093) + "..." : ltext); return { embeds: [embed] }; -} \ No newline at end of file +} + +/** + * @typedef LyricResult + * @type {LyricResultBase & (LyricResultText | LyricResultTimed)} + */ + +/** + * @typedef LyricResultBase + * @type {object} + * @property {LyricResultTrack} track + * @property {string} source + */ + +/** + * @typedef LyricResultText + * @type {object} + * @property {'text'} type + * @property {string} text + */ + +/** + * @typedef LyricResultTimed + * @type {object} + * @property {'time'} type + * @property {LyricResultLinePart[]} lines + */ + +/** + * @typedef LyricResultLinePart + * @type {object} + * @property {string} line + * @property {number} start + * @property {number} end + */ + +/** + * @typedef LyricResultTrack + * @type {object} + * @property {string} title + * @property {string} author + * @property {string | null} album + * @property {LyricResultTrackAlbumArt[]} albumArt + */ + +/** + * @typedef LyricResultTrackAlbumArt + * @type {object} + * @property {string} url + * @property {number} width + * @property {number} height + */