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

View File

@ -1,66 +1,129 @@
const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js"); const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js");
const { getJson } = require("@helpers/HttpUtils");
const { MESSAGES, EMBED_COLORS } = require("@root/config"); const { MESSAGES, EMBED_COLORS } = require("@root/config");
const fetch = require('node-fetch');
const BASE_URL = "https://some-random-api.com/lyrics";
/** /**
* @type {import("@structures/Command")} * @type {import("@structures/Command")}
*/ */
module.exports = { module.exports = {
name: "lyric", name: "lyric",
description: "find lyric of the song", description: "get lyrics for the current song or a specified song",
category: "MUSIC", category: "MUSIC",
botPermissions: ["EmbedLinks"], botPermissions: ["EmbedLinks"],
command: { command: {
enabled: true, enabled: true,
minArgsCount: 1, usage: "[song name]",
usage: "<Song Title - singer>",
}, },
slashCommand: { slashCommand: {
enabled: true, enabled: true,
options: [ options: [
{ {
name: "query", name: "query",
description: "song name to search lyrics for",
type: ApplicationCommandOptionType.String, type: ApplicationCommandOptionType.String,
description: "find lyric of the song", required: false,
required: true,
}, },
], ],
}, },
async messageRun(message, args) { async messageRun(message, args) {
const choice = args.join(" "); const query = args.join(" ");
if (!choice) { const response = await getLyric(message, query);
return message.safeReply("Invalid Lyric selected."); await message.safeReply(response);
}
const response = await getLyric(message.author, choice);
return message.safeReply(response);
}, },
async interactionRun(interaction) { async interactionRun(interaction) {
const choice = interaction.options.getString("query"); const query = interaction.options.getString("query");
const response = await getLyric(interaction.user, choice); const response = await getLyric(interaction, query);
await interaction.followUp(response); await interaction.followUp(response);
}, },
}; };
async function getLyric(user, choice) { async function getLyric({ client, guild, member }, query) {
const lyric = await getJson(`${BASE_URL}?title=${choice}`); const player = client.musicManager.players.resolve(guild.id);
if (!lyric.success) return MESSAGES.API_ERROR;
const thumbnail = lyric.data?.thumbnail.genius; if (!player) {
const author = lyric.data?.author; return "🚫 There's no active music player in this server.";
const lyrics = lyric.data?.lyrics; }
const title = lyric.data?.title;
const embed = new EmbedBuilder(); let track;
embed 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) .setColor(EMBED_COLORS.BOT_EMBED)
.setTitle(`${author} - ${title}`) .setTitle(`${track.author} - ${track.title}`)
.setThumbnail(thumbnail) .setThumbnail(track.artworkUrl)
.setDescription(lyrics) .setFooter({ text: `Requested by: ${member.user.username} | Source: ${lyrics.source || 'Unknown'}` });
.setFooter({ text: `Request By: ${user.username}` });
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] }; return { embeds: [embed] };
} }