Skip to content

Commit

Permalink
4.0-dev-2
Browse files Browse the repository at this point in the history
Song/queue/playlist duration now correctly calculated and formatted.
Fixed "undefined" song in player.
Fixed "unknown interaction" with song skip.
  • Loading branch information
AlexInCube committed Oct 17, 2024
1 parent a962a93 commit f72f39e
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion src/audioplayer/AudioPlayersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 8 additions & 7 deletions src/audioplayer/PlayerEmbed.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/audioplayer/PlayerInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion src/audioplayer/util/generateAddedPlaylistMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
)
Expand Down
3 changes: 2 additions & 1 deletion src/audioplayer/util/generateAddedSongMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
},
{
Expand Down
11 changes: 11 additions & 0 deletions src/audioplayer/util/playlistCalculateDuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Track } from 'riffy';

export function playlistCalculateDuration(playlist: Array<Track>): number {
let duration_ms = 0;

for (const track of playlist) {
duration_ms += track.info.length;
}

return duration_ms;
}
8 changes: 4 additions & 4 deletions src/commands/audio/rewind.command.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/audio/skip.command.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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:`
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit f72f39e

Please sign in to comment.