mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 06:28:53 +00:00
fix: refactor nowplaying command
This commit is contained in:
1 parent
8a84492f45
commit
f061000f93
1 file changed
+152
-97
@@ -8,6 +8,7 @@ import {
|
|||||||
ButtonStyle,
|
ButtonStyle,
|
||||||
ComponentType,
|
ComponentType,
|
||||||
EmbedBuilder,
|
EmbedBuilder,
|
||||||
|
EmbedField,
|
||||||
SlashCommandBuilder
|
SlashCommandBuilder
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||||
@@ -39,44 +40,76 @@ class NowPlayingCommand extends BaseSlashCommandInteraction {
|
|||||||
checkQueueCurrentTrack
|
checkQueueCurrentTrack
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const sourceStringsFormatted: Map<string, string> = new Map([
|
const tracksInQueueCount: number = queue.tracks.data.length;
|
||||||
['youtube', 'YouTube'],
|
|
||||||
['soundcloud', 'SoundCloud'],
|
|
||||||
['spotify', 'Spotify'],
|
|
||||||
['apple_music', 'Apple Music'],
|
|
||||||
['arbitrary', 'Direct source']
|
|
||||||
]);
|
|
||||||
|
|
||||||
const sourceIcons: Map<string, string> = new Map([
|
|
||||||
['youtube', this.embedOptions.icons.sourceYouTube],
|
|
||||||
['soundcloud', this.embedOptions.icons.sourceSoundCloud],
|
|
||||||
['spotify', this.embedOptions.icons.sourceSpotify],
|
|
||||||
['apple_music', this.embedOptions.icons.sourceAppleMusic],
|
|
||||||
['arbitrary', this.embedOptions.icons.sourceArbitrary]
|
|
||||||
]);
|
|
||||||
|
|
||||||
const currentTrack: Track = queue.currentTrack!;
|
const currentTrack: Track = queue.currentTrack!;
|
||||||
|
const displayTrackUrl: string = this.getDisplayTrackUrl(currentTrack);
|
||||||
|
const displayTrackRequestedBy: string = this.getDisplayTrackRequestedBy(currentTrack);
|
||||||
|
const displayTrackPlayingStatus: string = this.getDisplayTrackPlayingStatus(queue);
|
||||||
|
const displayQueueRepeatMode: string = this.getDisplayQueueRepeatMode(queue);
|
||||||
|
const displayEmbedProgressBar: string = await this.getDisplayQueueProgressBar(queue);
|
||||||
|
|
||||||
let author: string = currentTrack.author ? currentTrack.author : 'Unavailable';
|
const customId: string = `nowplaying-skip-button_${currentTrack.id}`;
|
||||||
if (author === 'cdn.discordapp.com') {
|
logger.debug(`Generated custom id for skip button: ${customId}`);
|
||||||
author = 'Unavailable';
|
|
||||||
}
|
|
||||||
const plays: number = currentTrack.views !== 0 ? currentTrack.views : 0;
|
|
||||||
|
|
||||||
let displayPlays: string = plays.toLocaleString('en-US');
|
const skipButton: APIButtonComponent = new ButtonBuilder()
|
||||||
|
.setCustomId(customId)
|
||||||
|
.setLabel('Skip track')
|
||||||
|
.setStyle(ButtonStyle.Secondary)
|
||||||
|
.setEmoji(this.embedOptions.icons.nextTrack)
|
||||||
|
.toJSON();
|
||||||
|
|
||||||
const metadata: TrackMetadata = currentTrack.metadata as TrackMetadata;
|
const embedActionRow: APIActionRowComponent<APIMessageActionRowComponent> = {
|
||||||
|
type: ComponentType.ActionRow,
|
||||||
|
components: [skipButton]
|
||||||
|
};
|
||||||
|
|
||||||
if (plays === 0 && metadata.bridge && metadata.bridge.views !== 0 && metadata.bridge.views !== undefined) {
|
logger.debug('Sending info embed with action row components.');
|
||||||
displayPlays = metadata.bridge.views.toLocaleString('en-US');
|
return await interaction.editReply({
|
||||||
} else if (plays === 0) {
|
embeds: [
|
||||||
|
new EmbedBuilder()
|
||||||
|
.setAuthor(await this.getEmbedQueueAuthor(interaction, queue))
|
||||||
|
.setDescription(
|
||||||
|
`${displayTrackPlayingStatus}\n` +
|
||||||
|
`${displayTrackUrl}\n` +
|
||||||
|
`${displayTrackRequestedBy}\n` +
|
||||||
|
`${displayEmbedProgressBar}\n\n ` +
|
||||||
|
`${displayQueueRepeatMode}\n\n`
|
||||||
|
)
|
||||||
|
.addFields(this.getEmbedFields(currentTrack))
|
||||||
|
.setFooter({
|
||||||
|
text: tracksInQueueCount ? `${tracksInQueueCount} other tracks in the queue...` : ' '
|
||||||
|
})
|
||||||
|
.setThumbnail(queue.currentTrack!.thumbnail)
|
||||||
|
.setColor(this.embedOptions.colors.info)
|
||||||
|
],
|
||||||
|
components: [embedActionRow as APIActionRowComponent<APIMessageActionRowComponent>]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private getDisplayPlays(currentTrack: Track | undefined): string {
|
||||||
|
const trackMetadata = currentTrack?.metadata as TrackMetadata;
|
||||||
|
|
||||||
|
let displayPlays: string =
|
||||||
|
(currentTrack?.views || trackMetadata?.bridge?.views || 0).toLocaleString('en-US') ?? '0';
|
||||||
|
|
||||||
|
if (displayPlays === '0') {
|
||||||
displayPlays = 'Unavailable';
|
displayPlays = 'Unavailable';
|
||||||
}
|
}
|
||||||
|
|
||||||
const source: string = sourceStringsFormatted.get(currentTrack.raw.source!) ?? 'Unavailable';
|
return displayPlays;
|
||||||
const queueLength: number = queue.tracks.data.length;
|
}
|
||||||
|
|
||||||
|
private getDisplayTrackAuthor(currentTrack: Track | undefined): string {
|
||||||
|
let author: string = currentTrack?.author ? currentTrack.author : 'Unavailable';
|
||||||
|
if (author === 'cdn.discordapp.com') {
|
||||||
|
author = 'Unavailable';
|
||||||
|
}
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getDisplayQueueProgressBar(queue: GuildQueue): Promise<string> {
|
||||||
const timestamp: PlayerTimestamp = queue.node.getTimestamp()!;
|
const timestamp: PlayerTimestamp = queue.node.getTimestamp()!;
|
||||||
let bar: string = `**\`${timestamp.current.label}\`** ${queue.node.createProgressBar({
|
let progressBar: string = `**\`${timestamp.current.label}\`** ${queue.node.createProgressBar({
|
||||||
queue: false,
|
queue: false,
|
||||||
length: playerOptions.progressBar.length ?? 12,
|
length: playerOptions.progressBar.length ?? 12,
|
||||||
timecodes: playerOptions.progressBar.timecodes ?? false,
|
timecodes: playerOptions.progressBar.timecodes ?? false,
|
||||||
@@ -85,29 +118,49 @@ class NowPlayingCommand extends BaseSlashCommandInteraction {
|
|||||||
rightChar: playerOptions.progressBar.rightChar ?? '▬'
|
rightChar: playerOptions.progressBar.rightChar ?? '▬'
|
||||||
})} **\`${timestamp.total.label}\`**`;
|
})} **\`${timestamp.total.label}\`**`;
|
||||||
|
|
||||||
if (Number(currentTrack.raw.duration) === 0 || currentTrack.duration === '0:00') {
|
if (Number(queue.currentTrack?.raw.duration) === 0 || queue.currentTrack?.duration === '0:00') {
|
||||||
bar = '_No duration available._';
|
progressBar = '_No duration available._';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentTrack.raw.live) {
|
if (queue.currentTrack?.raw.live) {
|
||||||
bar = `${this.embedOptions.icons.liveTrack} **\`LIVE\`** - Playing continuously from live source.`;
|
progressBar = `${this.embedOptions.icons.liveTrack} **\`LIVE\`** - Playing continuously from live source.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const customId: string = `nowplaying-skip-button_${currentTrack.id}`;
|
return progressBar;
|
||||||
logger.debug(`Generated custom id for skip button: ${customId}`);
|
}
|
||||||
|
|
||||||
const nowPlayingButton: APIButtonComponent = new ButtonBuilder()
|
private getTrackSourceString(currentTrack: Track): string {
|
||||||
.setCustomId(customId)
|
const sourceStringsFormatted: Map<string, string> = new Map([
|
||||||
.setLabel('Skip track')
|
['youtube', 'YouTube'],
|
||||||
.setStyle(ButtonStyle.Secondary)
|
['soundcloud', 'SoundCloud'],
|
||||||
.setEmoji(this.embedOptions.icons.nextTrack)
|
['spotify', 'Spotify'],
|
||||||
.toJSON();
|
['apple_music', 'Apple Music'],
|
||||||
|
['arbitrary', 'Direct source']
|
||||||
|
]);
|
||||||
|
|
||||||
const nowPlayingActionRow: APIActionRowComponent<APIMessageActionRowComponent> = {
|
return sourceStringsFormatted.get(currentTrack.raw.source!) ?? 'Unavailable';
|
||||||
type: ComponentType.ActionRow,
|
}
|
||||||
components: [nowPlayingButton]
|
|
||||||
};
|
|
||||||
|
|
||||||
|
private getTrackSourceIcon(currentTrack: Track): string {
|
||||||
|
const sourceIcons: Map<string, string> = new Map([
|
||||||
|
['youtube', this.embedOptions.icons.sourceYouTube],
|
||||||
|
['soundcloud', this.embedOptions.icons.sourceSoundCloud],
|
||||||
|
['spotify', this.embedOptions.icons.sourceSpotify],
|
||||||
|
['apple_music', this.embedOptions.icons.sourceAppleMusic],
|
||||||
|
['arbitrary', this.embedOptions.icons.sourceArbitrary]
|
||||||
|
]);
|
||||||
|
|
||||||
|
return sourceIcons.get(currentTrack.raw.source!) ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
private getDisplayTrackSource(currentTrack: Track) {
|
||||||
|
const trackSource: string = this.getTrackSourceString(currentTrack) ?? 'Unavailable';
|
||||||
|
const trackSourceIcon: string = this.getTrackSourceIcon(currentTrack) ?? '';
|
||||||
|
|
||||||
|
return `**${trackSourceIcon} [${trackSource}](${currentTrack.raw.url ?? currentTrack.url})**`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getRepeatModeString(repeatMode: number): string {
|
||||||
const loopModesFormatted: Map<number, string> = new Map([
|
const loopModesFormatted: Map<number, string> = new Map([
|
||||||
[0, 'disabled'],
|
[0, 'disabled'],
|
||||||
[1, 'track'],
|
[1, 'track'],
|
||||||
@@ -115,59 +168,61 @@ class NowPlayingCommand extends BaseSlashCommandInteraction {
|
|||||||
[3, 'autoplay']
|
[3, 'autoplay']
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const loopModeUserString: string = loopModesFormatted.get(queue.repeatMode)!;
|
return loopModesFormatted.get(repeatMode)!;
|
||||||
|
|
||||||
const getRepeatModeMessage = (repeatMode: number): string => {
|
|
||||||
const icon = repeatMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop;
|
|
||||||
return `**${icon} Looping**\nLoop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.\n\n`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const repeatModeString: string = queue.repeatMode === 0 ? '' : getRepeatModeMessage(queue.repeatMode);
|
|
||||||
|
|
||||||
logger.debug('Successfully retrieved information about the current track.');
|
|
||||||
|
|
||||||
logger.debug('Sending info embed with action row components.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
.setAuthor(await this.getEmbedQueueAuthor(interaction, queue))
|
|
||||||
.setDescription(
|
|
||||||
(queue.node.isPaused()
|
|
||||||
? '**Currently Paused**\n'
|
|
||||||
: `**${this.embedOptions.icons.audioPlaying} Now Playing**\n`) +
|
|
||||||
`**[${currentTrack.title}](${currentTrack.raw.url ?? currentTrack.url})**` +
|
|
||||||
`\nRequested by: <@${currentTrack.requestedBy?.id}>` +
|
|
||||||
`\n ${bar}\n\n` +
|
|
||||||
`${repeatModeString}`
|
|
||||||
)
|
|
||||||
.addFields(
|
|
||||||
{
|
|
||||||
name: '**Author**',
|
|
||||||
value: author,
|
|
||||||
inline: true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '**Plays**',
|
|
||||||
value: displayPlays,
|
|
||||||
inline: true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: '**Track source**',
|
|
||||||
value: `**${sourceIcons.get(currentTrack.raw.source!)} [${source}](${
|
|
||||||
currentTrack.raw.url ?? currentTrack.url
|
|
||||||
})**`,
|
|
||||||
inline: true
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.setFooter({
|
|
||||||
text: queueLength ? `${queueLength} other tracks in the queue...` : ' '
|
|
||||||
})
|
|
||||||
.setThumbnail(queue.currentTrack!.thumbnail)
|
|
||||||
.setColor(this.embedOptions.colors.info)
|
|
||||||
],
|
|
||||||
components: [nowPlayingActionRow as APIActionRowComponent<APIMessageActionRowComponent>]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getDisplayQueueRepeatMode = (queue: GuildQueue): string => {
|
||||||
|
const repeatMode: number = queue.repeatMode;
|
||||||
|
if (repeatMode === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const loopModeUserString: string = this.getRepeatModeString(repeatMode);
|
||||||
|
const icon = repeatMode === 3 ? this.embedOptions.icons.autoplay : this.embedOptions.icons.loop;
|
||||||
|
return `**${icon} Looping**\nLoop mode is set to **\`${loopModeUserString}\`**. You can change it with **\`/loop\`**.`;
|
||||||
|
};
|
||||||
|
|
||||||
|
private getDisplayTrackPlayingStatus = (queue: GuildQueue): string => {
|
||||||
|
return queue.node.isPaused()
|
||||||
|
? '**Currently Paused**'
|
||||||
|
: `**${this.embedOptions.icons.audioPlaying} Now Playing**`;
|
||||||
|
};
|
||||||
|
|
||||||
|
private getDisplayTrackUrl = (currentTrack: Track): string => {
|
||||||
|
const trackTitle = currentTrack.title ?? 'Title unavailable';
|
||||||
|
const trackUrl = currentTrack.url ?? currentTrack.raw.url;
|
||||||
|
if (!trackTitle || !trackUrl) {
|
||||||
|
return '**Unavailable**';
|
||||||
|
}
|
||||||
|
|
||||||
|
return `**[${trackTitle}](${trackUrl})**`;
|
||||||
|
};
|
||||||
|
|
||||||
|
private getDisplayTrackRequestedBy = (currentTrack: Track): string => {
|
||||||
|
return currentTrack.requestedBy ? `<@${currentTrack.requestedBy.id}>` : 'Unavailable';
|
||||||
|
};
|
||||||
|
|
||||||
|
private getEmbedFields = (currentTrack: Track): EmbedField[] => {
|
||||||
|
const fields: EmbedField[] = [
|
||||||
|
{
|
||||||
|
name: '**Author**',
|
||||||
|
value: this.getDisplayTrackAuthor(currentTrack),
|
||||||
|
inline: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '**Plays**',
|
||||||
|
value: this.getDisplayPlays(currentTrack),
|
||||||
|
inline: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '**Track source**',
|
||||||
|
value: this.getDisplayTrackSource(currentTrack),
|
||||||
|
inline: true
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
return fields;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new NowPlayingCommand();
|
export default new NowPlayingCommand();
|
||||||
Reference in new issue
Block a user