discord-jellyfin-bot/src/commands/playlist.command.ts

80 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-12-16 16:10:16 +01:00
import { TransformPipe } from '@discord-nestjs/common';
import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core';
import { CommandInteraction } from 'discord.js';
import { DiscordMessageService } from '../clients/discord/discord.message.service';
import { GenericCustomReply } from '../models/generic-try-handler';
2022-12-17 19:52:32 +01:00
import { PlaybackService } from '../playback/playback.service';
import { Constants } from '../utils/constants';
import { trimStringToFixedLength } from '../utils/stringUtils';
2022-12-17 19:52:32 +01:00
import { formatMillisecondsAsHumanReadable } from '../utils/timeUtils';
2022-12-16 16:10:16 +01:00
@Command({
name: 'playlist',
2022-12-16 16:10:16 +01:00
description: 'Print the current track information',
})
@UsePipes(TransformPipe)
export class PlaylistCommand implements DiscordCommand {
2022-12-17 19:52:32 +01:00
constructor(
private readonly discordMessageService: DiscordMessageService,
private readonly playbackService: PlaybackService,
) {}
2022-12-20 11:03:24 +01:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handler(interaction: CommandInteraction): GenericCustomReply {
2022-12-17 19:52:32 +01:00
const playList = this.playbackService.getPlaylist();
if (playList.tracks.length === 0) {
return {
embeds: [
this.discordMessageService.buildMessage({
title: 'Your Playlist',
description:
'You do not have any tracks in your playlist.\nUse the play command to add new tracks to your playlist',
}),
],
};
}
const tracklist = playList.tracks
.slice(0, 10)
.map((track, index) => {
2022-12-17 19:52:32 +01:00
const isCurrent = track.id === playList.activeTrack;
let point = this.getListPoint(isCurrent, index);
point += `**${trimStringToFixedLength(track.track.name, 30)}**`;
2022-12-18 19:21:33 +01:00
if (isCurrent) {
point += ' :loud_sound:';
}
point += '\n';
point += Constants.Design.InvisibleSpace.repeat(2);
point += 'Duration: ';
point += formatMillisecondsAsHumanReadable(
2022-12-17 19:52:32 +01:00
track.track.durationInMilliseconds,
);
return point;
2022-12-17 19:52:32 +01:00
})
2022-12-18 17:46:31 +01:00
.join('\n');
2022-12-17 19:52:32 +01:00
return {
embeds: [
2022-12-17 19:52:32 +01:00
this.discordMessageService.buildMessage({
title: 'Your Playlist',
description: `${tracklist}\n\nUse the /skip and /previous command to select a track`,
}),
],
};
2022-12-16 16:10:16 +01:00
}
2022-12-17 19:52:32 +01:00
private getListPoint(isCurrent: boolean, index: number) {
2022-12-17 19:52:32 +01:00
if (isCurrent) {
return `${index + 1}. `;
2022-12-17 19:52:32 +01:00
}
return `${index + 1}. `;
2022-12-17 19:52:32 +01:00
}
2022-12-16 16:10:16 +01:00
}