Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasB25 committed Sep 10, 2024
1 parent 110b3eb commit 054d932
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 68 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
"@top-gg/sdk": "^3.1.6",
"discord.js": "^14.16.1",
"dotenv": "^16.4.5",
"genius-lyrics-api": "^3.2.1",
"i18n": "^0.15.1",
"node-system-stats": "^1.3.0",
"shoukaku": "github:shipgirlproject/Shoukaku#master",
"signale": "^1.4.0",
"topgg-autoposter": "^2.0.2",
"tslib": "^2.7.0",
"undici": "^6.19.8",
"genius-lyrics-api": "^3.2.1"
"undici": "^6.19.8"
},
"signale": {
"displayScope": true,
Expand Down
151 changes: 87 additions & 64 deletions src/commands/music/Lyrics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command, type Context, type Lavamusic } from "../../structures/index.js";
import { getLyrics } from 'genius-lyrics-api';
import { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } from 'discord.js';
import { getLyrics } from "genius-lyrics-api";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } from "discord.js";

export default class Lyrics extends Command {
constructor(client: Lavamusic) {
Expand Down Expand Up @@ -34,28 +34,27 @@ export default class Lyrics extends Command {

public async run(client: Lavamusic, ctx: Context): Promise<any> {
const player = client.queue.get(ctx.guild!.id);
const embed = new EmbedBuilder();
if (!player || !player.isPlaying) {
const embed = this.client.embed();

if (!(player && !player.isPlaying)) {
return await ctx.sendMessage({
embeds: [embed.setColor(client.color.red).setDescription(ctx.locale("cmd.lyrics.errors.no_playing"))],
});
}

const currentTrack = player.current;
const trackTitle = currentTrack.info.title.replace(/\[.*?\]/g, '').trim();
const artistName = currentTrack.info.author.replace(/\[.*?\]/g, '').trim();
const trackTitle = currentTrack.info.title.replace(/\[.*?\]/g, "").trim();
const artistName = currentTrack.info.author.replace(/\[.*?\]/g, "").trim();
const trackUrl = currentTrack.info.uri;
const artworkUrl = currentTrack.info.artworkUrl;

// Send initial "Searching for lyrics" message
const searchMessage = await ctx.sendDeferMessage(ctx.locale("cmd.lyrics.searching", { trackTitle }))
await ctx.sendDeferMessage(ctx.locale("cmd.lyrics.searching", { trackTitle }));

const options = {
apiKey: client.config.lyricsApi,
title: trackTitle,
artist: artistName,
optimizeQuery: true
optimizeQuery: true,
};

try {
Expand All @@ -64,92 +63,105 @@ export default class Lyrics extends Command {
const lyricsPages = this.paginateLyrics(lyrics);
let currentPage = 0;

const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId('prev')
.setEmoji(this.client.emoji.page.back)
.setStyle(ButtonStyle.Secondary)
.setDisabled(true),
new ButtonBuilder()
.setCustomId('stop')
.setEmoji(this.client.emoji.page.cancel)
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId('next')
.setEmoji(this.client.emoji.page.next)
.setStyle(ButtonStyle.Secondary)
.setDisabled(lyricsPages.length <= 1)
);

// Edit the initial message to include lyrics
await searchMessage.edit({
embeds: [embed.setColor(client.color.main).setDescription(ctx.locale("cmd.lyrics.lyrics_track", { trackTitle, trackUrl, lyrics: lyricsPages[currentPage] })).setThumbnail(artworkUrl).setTimestamp()],
components: [row]
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId("prev")
.setEmoji(this.client.emoji.page.back)
.setStyle(ButtonStyle.Secondary)
.setDisabled(true),
new ButtonBuilder().setCustomId("stop").setEmoji(this.client.emoji.page.cancel).setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("next")
.setEmoji(this.client.emoji.page.next)
.setStyle(ButtonStyle.Secondary)
.setDisabled(lyricsPages.length <= 1),
);

await ctx.editMessage({
embeds: [
embed
.setColor(client.color.main)
.setDescription(
ctx.locale("cmd.lyrics.lyrics_track", { trackTitle, trackUrl, lyrics: lyricsPages[currentPage] }),
)
.setThumbnail(artworkUrl)
.setTimestamp(),
],
components: [row],
});

const filter = (interaction) => interaction.user.id === ctx.author.id;
const collector = searchMessage.createMessageComponentCollector({ filter, componentType: ComponentType.Button, time: 60000 });
const collector = ctx.channel.createMessageComponentCollector({
filter,
componentType: ComponentType.Button,
time: 60000,
});

collector.on('collect', async (interaction) => {
if (interaction.customId === 'prev') {
collector.on("collect", async (interaction) => {
if (interaction.customId === "prev") {
currentPage--;
} else if (interaction.customId === 'next') {
} else if (interaction.customId === "next") {
currentPage++;
} else if (interaction.customId === 'stop') {
} else if (interaction.customId === "stop") {
collector.stop();
return interaction.update({ components: [] });
}

await interaction.update({
embeds: [embed.setDescription(ctx.locale("cmd.lyrics.lyrics_track", { trackTitle, trackUrl, lyrics: lyricsPages[currentPage] })).setThumbnail(artworkUrl).setTimestamp()],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId('prev')
.setEmoji(this.client.emoji.page.back)
.setStyle(ButtonStyle.Secondary)
.setDisabled(currentPage === 0),
new ButtonBuilder()
.setCustomId('stop')
.setEmoji(this.client.emoji.page.cancel)
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId('next')
.setEmoji(this.client.emoji.page.next)
.setStyle(ButtonStyle.Secondary)
.setDisabled(currentPage === lyricsPages.length - 1)
embeds: [
embed
.setDescription(
ctx.locale("cmd.lyrics.lyrics_track", { trackTitle, trackUrl, lyrics: lyricsPages[currentPage] }),
)
]
.setThumbnail(artworkUrl)
.setTimestamp(),
],
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId("prev")
.setEmoji(this.client.emoji.page.back)
.setStyle(ButtonStyle.Secondary)
.setDisabled(currentPage === 0),
new ButtonBuilder()
.setCustomId("stop")
.setEmoji(this.client.emoji.page.cancel)
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("next")
.setEmoji(this.client.emoji.page.next)
.setStyle(ButtonStyle.Secondary)
.setDisabled(currentPage === lyricsPages.length - 1),
),
],
});
});

collector.on('end', () => {
searchMessage.edit({ components: [] });
collector.on("end", () => {
ctx.editMessage({ components: [] });
});
} else {
await searchMessage.edit({
await ctx.editMessage({
embeds: [embed.setColor(client.color.red).setDescription(ctx.locale("cmd.lyrics.errors.no_results"))],
});
}
} catch (error) {
console.error(error);
await searchMessage.edit({
await ctx.editMessage({
embeds: [embed.setColor(client.color.red).setDescription(ctx.locale("cmd.lyrics.errors.lyrics_error"))],
});
}
}

paginateLyrics(lyrics) {
const lines = lyrics.split('\n');
const lines = lyrics.split("\n");
const pages = [];
let page = '';
let page = "";

for (const line of lines) {
if (page.length + line.length > 2048) {
pages.push(page);
page = '';
page = "";
}
page += `${line}\n`;
}
Expand All @@ -158,3 +170,14 @@ export default class Lyrics extends Command {
return pages;
}
}

/**
* Project: lavamusic
* Author: Appu
* Main Contributor: LucasB25
* Company: Coders
* Copyright (c) 2024. All rights reserved.
* This code is the property of Coder and may not be reproduced or
* modified without permission. For more information, contact us at
* https://discord.gg/ns8CTk9J3e
*/
2 changes: 1 addition & 1 deletion src/events/client/VoiceStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ export default class VoiceStateUpdate extends Event {
* This code is the property of Coder and may not be reproduced or
* modified without permission. For more information, contact us at
* https://discord.gg/ns8CTk9J3e
*/
*/
2 changes: 1 addition & 1 deletion src/structures/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class Context {
this.message = ctx instanceof Message ? ctx : null;

if (ctx.channel && ctx.channel.type !== ChannelType.GroupDM) {
this.channel = ctx.channel
this.channel = ctx.channel;
} else {
this.channel = null;
}
Expand Down

0 comments on commit 054d932

Please sign in to comment.