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