diff --git a/package.json b/package.json index 622d1b6..768f538 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aicbot", - "version": "4.0.0-dev-1", + "version": "4.0.0-dev-2", "description": "Discord Bot for playing music", "main": "build/main.js", "scripts": { diff --git a/src/audioplayer/AudioPlayersManager.ts b/src/audioplayer/AudioPlayersManager.ts index f94a942..907c4eb 100644 --- a/src/audioplayer/AudioPlayersManager.ts +++ b/src/audioplayer/AudioPlayersManager.ts @@ -209,8 +209,10 @@ export class AudioPlayersManager { const riffyPlayer = this.riffy.players.get(guild.id); if (!riffyPlayer) return; + const current_song = riffyPlayer.current; + // I don`t know why stop(), but Riffy Player don`t have method skip() riffyPlayer.stop(); - return riffyPlayer.queue.first; + return current_song; } catch (e) { if (ENV.BOT_VERBOSE_LOGGING) loggerError(e); } diff --git a/src/audioplayer/PlayerEmbed.ts b/src/audioplayer/PlayerEmbed.ts index 8edb916..2817577 100644 --- a/src/audioplayer/PlayerEmbed.ts +++ b/src/audioplayer/PlayerEmbed.ts @@ -1,10 +1,11 @@ import { EmbedBuilder, User } from 'discord.js'; import { AudioPlayerLoopMode, AudioPlayerState, AudioSourceIcons } from './AudioPlayerIcons.js'; -import { formatSecondsToTime } from '../utilities/formatSecondsToTime.js'; import i18next from 'i18next'; import { getIconFromSource } from './util/getIconFromSource.js'; import { getSongsNoun } from './util/getSongsNoun.js'; -import { nodeResponse, Track } from 'riffy'; +import { Queue, Track } from 'riffy'; +import { formatMilliseconds } from '../utilities/formatMillisecondsToTime.js'; +import { playlistCalculateDuration } from './util/playlistCalculateDuration.js'; export class PlayerEmbed extends EmbedBuilder { private playerState: AudioPlayerState = 'loading'; @@ -130,9 +131,9 @@ export class PlayerEmbed extends EmbedBuilder { this.uploader = uploader ?? i18next.t('audioplayer:player_embed_unknown'); } - setQueueData(songs_count: number, queue_duration: number) { - this.songsCount = songs_count; - this.queueDuration = formatSecondsToTime(queue_duration); + setQueueData(riffyQueue: Queue) { + this.songsCount = riffyQueue.length; + this.queueDuration = formatMilliseconds(playlistCalculateDuration(riffyQueue)); } setThumbnailURL(url: string | null) { @@ -161,11 +162,11 @@ export class PlayerEmbed extends EmbedBuilder { this.nextSong = songName; } - setSongDuration(formattedDuration: number, isLive = false) { + setSongDuration(ms: number, isLive = false) { if (isLive) { this.formattedDuration = i18next.t('audioplayer:player_embed_duration_stream'); } else { - this.formattedDuration = formatSecondsToTime(formattedDuration); + this.formattedDuration = formatMilliseconds(ms); } } diff --git a/src/audioplayer/PlayerInstance.ts b/src/audioplayer/PlayerInstance.ts index 9c759ac..19f617d 100644 --- a/src/audioplayer/PlayerInstance.ts +++ b/src/audioplayer/PlayerInstance.ts @@ -105,7 +105,7 @@ export class PlayerInstance { if (!riffyPlayer) return; this.embedBuilder.setPlayerState(this.state); - const currentSong: Track | null = riffyPlayer.queue.first; + const currentSong: Track | null = riffyPlayer.current; if (currentSong) { this.embedBuilder.setSongDuration(currentSong.info.length, currentSong.info.stream); this.embedBuilder.setSongSource(currentSong); @@ -121,7 +121,7 @@ export class PlayerInstance { } } this.embedBuilder.setNextSong(riffyPlayer.queue.at(1)?.info.title); - this.embedBuilder.setQueueData(riffyPlayer.queue.length, 99999); + this.embedBuilder.setQueueData(riffyPlayer.queue); this.embedBuilder.update(); } diff --git a/src/audioplayer/util/generateAddedPlaylistMessage.ts b/src/audioplayer/util/generateAddedPlaylistMessage.ts index ecbe625..225dc45 100644 --- a/src/audioplayer/util/generateAddedPlaylistMessage.ts +++ b/src/audioplayer/util/generateAddedPlaylistMessage.ts @@ -2,6 +2,8 @@ import { EmbedBuilder } from 'discord.js'; import i18next from 'i18next'; import { getIconFromSource } from './getIconFromSource.js'; import { nodeResponse } from 'riffy'; +import { formatMilliseconds } from '../../utilities/formatMillisecondsToTime.js'; +import { playlistCalculateDuration } from './playlistCalculateDuration.js'; export function generateAddedPlaylistMessage(playlist: nodeResponse) { const serviceIcon = getIconFromSource(playlist.tracks[0].info.sourceName ?? undefined); @@ -30,7 +32,7 @@ export function generateAddedPlaylistMessage(playlist: nodeResponse) { }, { name: `${i18next.t('audioplayer:event_add_song_length')}`, - value: `\`${99999}\``, + value: `\`${formatMilliseconds(playlistCalculateDuration(playlist.tracks))}\``, inline: true } ) diff --git a/src/audioplayer/util/generateAddedSongMessage.ts b/src/audioplayer/util/generateAddedSongMessage.ts index 492d7af..7ec0c14 100644 --- a/src/audioplayer/util/generateAddedSongMessage.ts +++ b/src/audioplayer/util/generateAddedSongMessage.ts @@ -2,6 +2,7 @@ import { EmbedBuilder } from 'discord.js'; import i18next from 'i18next'; import { getIconFromSource } from './getIconFromSource.js'; import { Track } from 'riffy'; +import { formatMilliseconds } from '../../utilities/formatMillisecondsToTime.js'; export function generateAddedSongMessage(song: Track) { const serviceIcon = getIconFromSource(song.info.sourceName); @@ -19,7 +20,7 @@ export function generateAddedSongMessage(song: Track) { }, { name: `${i18next.t('audioplayer:event_add_song_length')}`, - value: `\`${song.info.length}\``, + value: `\`${formatMilliseconds(song.info.length)}\``, inline: true }, { diff --git a/src/audioplayer/util/playlistCalculateDuration.ts b/src/audioplayer/util/playlistCalculateDuration.ts new file mode 100644 index 0000000..43885e6 --- /dev/null +++ b/src/audioplayer/util/playlistCalculateDuration.ts @@ -0,0 +1,11 @@ +import { Track } from 'riffy'; + +export function playlistCalculateDuration(playlist: Array): number { + let duration_ms = 0; + + for (const track of playlist) { + duration_ms += track.info.length; + } + + return duration_ms; +} diff --git a/src/commands/audio/rewind.command.ts b/src/commands/audio/rewind.command.ts index 679309e..0677b07 100644 --- a/src/commands/audio/rewind.command.ts +++ b/src/commands/audio/rewind.command.ts @@ -1,13 +1,13 @@ import { CommandArgument, ICommand } from '../../CommandTypes.js'; -import { EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder } from 'discord.js'; +import { EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, time } from 'discord.js'; import { GroupAudio } from './AudioTypes.js'; import { AudioCommandWrapperInteraction, AudioCommandWrapperText } from '../../audioplayer/util/AudioCommandWrappers.js'; -import { formatSecondsToTime } from '../../utilities/formatSecondsToTime.js'; import i18next from 'i18next'; import { generateSimpleEmbed } from '../../utilities/generateSimpleEmbed.js'; +import { formatMilliseconds } from '../../utilities/formatMillisecondsToTime.js'; export default function (): ICommand { return { @@ -94,8 +94,8 @@ function hmsToSeconds(str: string): number | undefined { return s; } -export function generateEmbedAudioPlayerRewind(member: GuildMember, time: number): EmbedBuilder { - return generateSimpleEmbed(`${member} ${i18next.t('commands:rewind_success')} ${formatSecondsToTime(time)}`); +export function generateEmbedAudioPlayerRewind(member: GuildMember, seconds: number): EmbedBuilder { + return generateSimpleEmbed(`${member} ${i18next.t('commands:rewind_success')} ${formatMilliseconds(seconds * 1000)}`); } export function generateEmbedAudioPlayerRewindFailure(): EmbedBuilder { diff --git a/src/commands/audio/skip.command.ts b/src/commands/audio/skip.command.ts index a8e10db..af1c269 100644 --- a/src/commands/audio/skip.command.ts +++ b/src/commands/audio/skip.command.ts @@ -1,4 +1,3 @@ -// @ts-nocheck import { ICommand } from '../../CommandTypes.js'; import { EmbedBuilder, GuildMember, Message, PermissionsBitField, SlashCommandBuilder } from 'discord.js'; import { GroupAudio } from './AudioTypes.js'; @@ -8,6 +7,7 @@ import { } from '../../audioplayer/util/AudioCommandWrappers.js'; import i18next from 'i18next'; import { generateSimpleEmbed } from '../../utilities/generateSimpleEmbed.js'; +import { Track } from 'riffy'; export default function (): ICommand { return { @@ -50,9 +50,9 @@ export default function (): ICommand { }; } -export function generateSkipEmbed(song: Song, member: GuildMember): EmbedBuilder { +export function generateSkipEmbed(song: Track, member: GuildMember): EmbedBuilder { return generateSimpleEmbed( - `:fast_forward: ${member} ${i18next.t('commands:skip_success')} ${song.name} - ${song.uploader.name} :fast_forward:` + `:fast_forward: ${member} ${i18next.t('commands:skip_success')} ${song.info.title} - ${song.info.author} :fast_forward:` ); } diff --git a/src/utilities/formatSecondsToTime.ts b/src/utilities/formatMillisecondsToTime.ts similarity index 67% rename from src/utilities/formatSecondsToTime.ts rename to src/utilities/formatMillisecondsToTime.ts index bf555fa..3c22cb8 100644 --- a/src/utilities/formatSecondsToTime.ts +++ b/src/utilities/formatMillisecondsToTime.ts @@ -1,8 +1,10 @@ -export function formatSecondsToTime(seconds: number | string): string { - if (typeof seconds === 'string') { - seconds = parseInt(seconds); +export function formatMilliseconds(ms: number | string): string { + if (typeof ms === 'string') { + ms = parseInt(ms); } + const seconds = ms / 1000; + const array_with_time = []; const hours = Math.floor(seconds / 60 / 60);