mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-03 14:38:49 +00:00
fix: refactored pause command, added common method getDisplayTrackDurationAndUrl()
This commit is contained in:
1 parent
ee8cc06e4d
commit
0850d9b26d
7 files changed
+64
-94
No files matched your search
+36
-10
@@ -1,5 +1,5 @@
|
||||
import config from 'config';
|
||||
import { GuildQueue } from 'discord-player';
|
||||
import { GuildQueue, Track } from 'discord-player';
|
||||
import {
|
||||
ApplicationCommandOptionChoiceData,
|
||||
AutocompleteInteraction,
|
||||
@@ -27,6 +27,14 @@ import {
|
||||
import { Validator, ValidatorParams } from '../types/utilTypes';
|
||||
|
||||
abstract class BaseInteraction {
|
||||
embedOptions: EmbedOptions;
|
||||
botOptions: BotOptions;
|
||||
|
||||
constructor() {
|
||||
this.embedOptions = config.get('embedOptions');
|
||||
this.botOptions = config.get('botOptions');
|
||||
}
|
||||
|
||||
protected getLoggerBase(
|
||||
module: string,
|
||||
name: string,
|
||||
@@ -50,19 +58,41 @@ abstract class BaseInteraction {
|
||||
}
|
||||
}
|
||||
|
||||
protected getFormattedDuration(track: Track): string {
|
||||
let durationFormat =
|
||||
Number(track.raw.duration) === 0 || track.duration === '0:00' ? '' : `**\`${track.duration}\`**`;
|
||||
|
||||
if (track.raw.live) {
|
||||
durationFormat = `**${this.embedOptions.icons.liveTrack} \`LIVE\`**`;
|
||||
}
|
||||
|
||||
return durationFormat;
|
||||
}
|
||||
|
||||
protected getFormattedTrackUrl(track: Track): string {
|
||||
const trackTitle = track.title ?? 'Title unavailable';
|
||||
const trackUrl = track.url ?? track.raw.url;
|
||||
if (!trackTitle || !trackUrl) {
|
||||
return '**Unavailable**';
|
||||
}
|
||||
return `**[${trackTitle}](${trackUrl})**`;
|
||||
}
|
||||
|
||||
protected getDisplayTrackDurationAndUrl(track: Track): string {
|
||||
const formattedDuration = this.getFormattedDuration(track);
|
||||
const formattedUrl = this.getFormattedTrackUrl(track);
|
||||
|
||||
return `${formattedDuration} ${formattedUrl}`;
|
||||
}
|
||||
|
||||
abstract execute(
|
||||
params: BaseInteractionParams
|
||||
): Promise<Message<boolean> | ApplicationCommandOptionChoiceData | void>;
|
||||
}
|
||||
|
||||
abstract class BaseInteractionWithEmbedResponse extends BaseInteraction {
|
||||
embedOptions: EmbedOptions;
|
||||
botOptions: BotOptions;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.embedOptions = config.get('embedOptions');
|
||||
this.botOptions = config.get('botOptions');
|
||||
}
|
||||
|
||||
protected async getEmbedUserAuthor(
|
||||
@@ -121,14 +151,10 @@ export abstract class BaseSlashCommandInteraction extends BaseInteractionWithEmb
|
||||
}
|
||||
|
||||
export abstract class BaseComponentInteraction extends BaseInteractionWithEmbedResponse {
|
||||
embedOptions: EmbedOptions;
|
||||
botOptions: BotOptions;
|
||||
name: string;
|
||||
|
||||
constructor(name: string) {
|
||||
super();
|
||||
this.embedOptions = config.get('embedOptions');
|
||||
this.botOptions = config.get('botOptions');
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class NowPlayingCommand extends BaseSlashCommandInteraction {
|
||||
|
||||
const tracksInQueueCount: number = queue.tracks.data.length;
|
||||
const currentTrack: Track = queue.currentTrack!;
|
||||
const displayTrackUrl: string = this.getDisplayTrackUrl(currentTrack);
|
||||
const displayTrackUrl: string = this.getFormattedTrackUrl(currentTrack);
|
||||
const displayTrackRequestedBy: string = this.getDisplayTrackRequestedBy(currentTrack);
|
||||
const displayTrackPlayingStatus: string = this.getDisplayTrackPlayingStatus(queue);
|
||||
const displayQueueRepeatMode: string = this.getDisplayQueueRepeatMode(queue);
|
||||
@@ -188,16 +188,6 @@ class NowPlayingCommand extends BaseSlashCommandInteraction {
|
||||
: `**${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';
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { GuildQueue, Track, useQueue } from 'discord-player';
|
||||
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||
import { Logger } from 'pino';
|
||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||
import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator';
|
||||
@@ -26,18 +27,7 @@ class PauseCommand extends BaseSlashCommandInteraction {
|
||||
|
||||
const currentTrack: Track = queue.currentTrack!;
|
||||
|
||||
let durationFormat =
|
||||
Number(currentTrack.raw.duration) === 0 || currentTrack.duration === '0:00'
|
||||
? ''
|
||||
: `\`${currentTrack.duration}\``;
|
||||
|
||||
if (currentTrack.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
|
||||
// change paused state to opposite of current state
|
||||
queue.node.setPaused(!queue.node.isPaused());
|
||||
logger.debug(`Set paused state to ${queue.node.isPaused()}.`);
|
||||
this.togglePauseState(queue, logger);
|
||||
|
||||
logger.debug('Responding with success embed.');
|
||||
return await interaction.editReply({
|
||||
@@ -47,13 +37,18 @@ class PauseCommand extends BaseSlashCommandInteraction {
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.pauseResumed} ${
|
||||
queue.node.isPaused() ? 'Paused Track' : 'Resumed track'
|
||||
}**\n**${durationFormat} [${currentTrack.title}](${currentTrack.raw.url ?? currentTrack.url})**`
|
||||
}**\n ${this.getDisplayTrackDurationAndUrl(currentTrack)}`
|
||||
)
|
||||
.setThumbnail(currentTrack.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
private togglePauseState(queue: GuildQueue, logger: Logger): void {
|
||||
queue.node.setPaused(!queue.node.isPaused());
|
||||
logger.debug(`Set paused state to ${queue.node.isPaused()}.`);
|
||||
}
|
||||
}
|
||||
|
||||
export default new PauseCommand();
|
||||
@@ -226,13 +226,6 @@ class PlayCommand extends BaseSlashCommandInteraction {
|
||||
track.thumbnail = this.embedOptions.info.fallbackThumbnailUrl;
|
||||
}
|
||||
|
||||
let durationFormat =
|
||||
Number(track.raw.duration) === 0 || track.duration === '0:00' ? '' : `\`${track.duration}\``;
|
||||
|
||||
if (track.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
|
||||
if (searchResult.playlist && searchResult.tracks.length > 1) {
|
||||
logger.debug(`Playlist found and added with player.play(). Query: '${query}'`);
|
||||
|
||||
@@ -242,9 +235,9 @@ class PlayCommand extends BaseSlashCommandInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.success} Added playlist to queue**\n**${durationFormat} [${
|
||||
track.title
|
||||
}](${track.raw.url ?? track.url})**\n\nAnd **${
|
||||
`**${
|
||||
this.embedOptions.icons.success
|
||||
} Added playlist to queue**\n${this.getDisplayTrackDurationAndUrl(track)}\n\nAnd **${
|
||||
searchResult.tracks.length - 1
|
||||
}** more tracks... **\`/queue\`** to view all.`
|
||||
)
|
||||
@@ -263,9 +256,9 @@ class PlayCommand extends BaseSlashCommandInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.audioStartedPlaying} Started playing**\n**${durationFormat} [${
|
||||
track.title
|
||||
}](${track.raw.url ?? track.url})**`
|
||||
`**${
|
||||
this.embedOptions.icons.audioStartedPlaying
|
||||
} Started playing**\n${this.getDisplayTrackDurationAndUrl(track)}`
|
||||
)
|
||||
.setThumbnail(track.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
@@ -281,9 +274,9 @@ class PlayCommand extends BaseSlashCommandInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`${this.embedOptions.icons.success} **Added to queue**\n**${durationFormat} [${track.title}](${
|
||||
track.raw.url ?? track.url
|
||||
})**`
|
||||
`${this.embedOptions.icons.success} **Added to queue**\n${this.getDisplayTrackDurationAndUrl(
|
||||
track
|
||||
)}`
|
||||
)
|
||||
.setThumbnail(track.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
|
||||
@@ -62,16 +62,7 @@ class QueueCommand extends BaseSlashCommandInteraction {
|
||||
queueString = queue.tracks.data
|
||||
.slice(pageIndex * 10, pageIndex * 10 + 10)
|
||||
.map((track, index) => {
|
||||
let durationFormat =
|
||||
Number(track.raw.duration) === 0 || track.duration === '0:00' ? '' : `\`${track.duration}\``;
|
||||
|
||||
if (track.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
|
||||
return `**${pageIndex * 10 + index + 1}.** **${durationFormat} [${track.title}](${
|
||||
track.raw.url ?? track.url
|
||||
})**`;
|
||||
return `**${pageIndex * 10 + index + 1}.** ${this.getDisplayTrackDurationAndUrl(track)}`;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
@@ -52,14 +52,6 @@ class RemoveCommand extends BaseSlashCommandInteraction {
|
||||
// Remove specified track number from queue
|
||||
const removedTrack: Track = queue.node.remove(removeTrackNumber - 1)!;
|
||||
logger.debug(`Removed track '${removedTrack.url}' from queue.`);
|
||||
let durationFormat =
|
||||
Number(removedTrack.raw.duration) === 0 || removedTrack.duration === '0:00'
|
||||
? ''
|
||||
: `\`${removedTrack.duration}\``;
|
||||
|
||||
if (removedTrack.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
|
||||
logger.debug('Responding with success embed.');
|
||||
return await interaction.editReply({
|
||||
@@ -67,9 +59,9 @@ class RemoveCommand extends BaseSlashCommandInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.success} Removed track**\n**${durationFormat} [${
|
||||
removedTrack.title
|
||||
}](${removedTrack.raw.url ?? removedTrack.url})**`
|
||||
`**${this.embedOptions.icons.success} Removed track**\n${this.getDisplayTrackDurationAndUrl(
|
||||
removedTrack
|
||||
)}`
|
||||
)
|
||||
.setThumbnail(removedTrack.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
|
||||
@@ -49,15 +49,6 @@ class SkipCommand extends BaseSlashCommandInteraction {
|
||||
const skippedTrack: Track = queue.currentTrack!;
|
||||
logger.debug('Responding with warning embed.');
|
||||
|
||||
let durationFormat =
|
||||
Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00'
|
||||
? ''
|
||||
: `\`${skippedTrack.duration}\``;
|
||||
|
||||
if (skippedTrack.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
|
||||
queue.node.skipTo(skipToTrack - 1);
|
||||
logger.debug('Skipped to specified track number.');
|
||||
|
||||
@@ -67,9 +58,9 @@ class SkipCommand extends BaseSlashCommandInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${
|
||||
skippedTrack.title
|
||||
}](${skippedTrack.raw.url ?? skippedTrack.url})**`
|
||||
`**${
|
||||
this.embedOptions.icons.skipped
|
||||
} Skipped track**\n${this.getDisplayTrackDurationAndUrl(skippedTrack)}`
|
||||
)
|
||||
.setThumbnail(skippedTrack.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
@@ -94,14 +85,6 @@ class SkipCommand extends BaseSlashCommandInteraction {
|
||||
|
||||
const skippedTrack: Track = queue.currentTrack!;
|
||||
|
||||
let durationFormat =
|
||||
Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00'
|
||||
? ''
|
||||
: `\`${skippedTrack.duration}\``;
|
||||
|
||||
if (skippedTrack.raw.live) {
|
||||
durationFormat = `${this.embedOptions.icons.liveTrack} \`LIVE\``;
|
||||
}
|
||||
queue.node.skip();
|
||||
logger.debug('Skipped current track.');
|
||||
|
||||
@@ -127,9 +110,9 @@ class SkipCommand extends BaseSlashCommandInteraction {
|
||||
new EmbedBuilder()
|
||||
.setAuthor(await this.getEmbedUserAuthor(interaction))
|
||||
.setDescription(
|
||||
`**${this.embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${
|
||||
skippedTrack.title
|
||||
}](${skippedTrack.raw.url ?? skippedTrack.url})**` + `\n\n${repeatModeString}`
|
||||
`**${this.embedOptions.icons.skipped} Skipped track**\n${this.getDisplayTrackDurationAndUrl(
|
||||
skippedTrack
|
||||
)}` + `\n\n${repeatModeString}`
|
||||
)
|
||||
.setThumbnail(skippedTrack.thumbnail)
|
||||
.setColor(this.embedOptions.colors.success)
|
||||
|
||||
Reference in new issue
Block a user