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

69 lines
2.0 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 { formatMillisecondsAsHumanReadable } from '../utils/timeUtils';
2022-12-16 16:10:16 +01:00
@Command({
name: 'current',
description: 'Print the current track information',
})
@UsePipes(TransformPipe)
export class CurrentTrackCommand implements DiscordCommand {
2022-12-17 19:52:32 +01:00
constructor(
private readonly discordMessageService: DiscordMessageService,
private readonly playbackService: PlaybackService,
) {}
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) => {
const isCurrent = track.id === playList.activeTrack;
return `${this.getListPoint(isCurrent)} ${
track.track.name
}\n${Constants.Design.InvisibleSpace.repeat(
3,
)}${formatMillisecondsAsHumanReadable(
track.track.durationInMilliseconds,
)} ${isCurrent ? ' *(active track)*' : ''}`;
2022-12-17 19:52:32 +01:00
})
.join(',\n');
return {
embeds: [
2022-12-17 19:52:32 +01:00
this.discordMessageService.buildMessage({
title: 'Your Playlist',
description: tracklist,
}),
],
};
2022-12-16 16:10:16 +01:00
}
2022-12-17 19:52:32 +01:00
private getListPoint(isCurrent: boolean) {
if (isCurrent) {
return ':black_small_square:';
}
return ':white_small_square:';
}
2022-12-16 16:10:16 +01:00
}