Skip to content

Commit

Permalink
4.0-dev-1
Browse files Browse the repository at this point in the history
Returned message when added songs/playlists.
Returned player creation.
Returned pause/resume.
Refactored checkInVoice functions.
  • Loading branch information
AlexInCube committed Oct 17, 2024
1 parent 1617947 commit a962a93
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 196 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",
"version": "4.0.0-dev-1",
"description": "Discord Bot for playing music",
"main": "build/main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion runInDocker.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
docker compose up --detach --force-recreate --no-build --remove-orphans
docker compose up --detach --force-recreate --build --remove-orphans
167 changes: 75 additions & 92 deletions src/audioplayer/AudioPlayersManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AudioPlayersStore } from './AudioPlayersStore.js';
import { clamp } from '../utilities/clamp.js';
import { generateErrorEmbed } from '../utilities/generateErrorEmbed.js';
import i18next from 'i18next';
import { loggerError, loggerSend } from '../utilities/logger.js';
Expand All @@ -9,10 +8,9 @@ import { generateAddedSongMessage } from './util/generateAddedSongMessage.js';
import {
ButtonInteraction,
Client,
CommandInteraction,
EmbedBuilder,
Guild,
GuildMember,
GuildTextBasedChannel,
Interaction,

Check warning on line 14 in src/audioplayer/AudioPlayersManager.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'Interaction' is defined but never used
TextChannel,
VoiceBasedChannel
Expand Down Expand Up @@ -54,36 +52,74 @@ export class AudioPlayersManager {
member: GuildMember
): Promise<void> {
try {
const player: Player = this.riffy.createConnection({
const riffyPlayer: Player = this.riffy.createConnection({
guildId: textChannel.guild.id,
voiceChannel: voiceChannel.id,
textChannel: textChannel.id,
deaf: true
});

const player = this.playersManager.get(textChannel.guild.id);

const resolve: nodeResponse = await this.riffy.resolve({ query, requester: member.id });
const { loadType, tracks, playlistInfo } = resolve;

if (loadType === 'playlist') {
if (!playlistInfo) return;
if (resolve.loadType === 'playlist') {
if (!resolve.playlistInfo) return;

for (const track of resolve.tracks) {
track.info.requester = member;
player.queue.add(track);
riffyPlayer.queue.add(track);
}

if (player) {
if (riffyPlayer.textChannel) {
await player.textChannel.send({ embeds: [generateAddedPlaylistMessage(resolve)] });

if (riffyPlayer.queue.length >= ENV.BOT_MAX_SONGS_IN_QUEUE) {
await player.textChannel.send({
embeds: [
generateWarningEmbed(
i18next.t('audioplayer:event_add_list_limit', {
queueLimit: ENV.BOT_MAX_SONGS_IN_QUEUE
}) as string
)
]
});
// Concat songs count in queue to BOT_MAX_SONGS_IN_QUEUE
riffyPlayer.queue.length = ENV.BOT_MAX_SONGS_IN_QUEUE;
}
}
await player.update();
} else {
await textChannel.send({ embeds: [generateAddedPlaylistMessage(resolve)] });
}

await textChannel.send(`Added ${tracks.length} songs from ${playlistInfo.name} playlist.`);
if (ENV.BOT_MAX_SONGS_HISTORY_SIZE > 0) {
//await addSongToGuildSongsHistory(queue.id, playlist);
}

if (!player.playing && !player.paused) await player.play();
} else if (loadType === 'search' || loadType === 'track') {
const track = tracks.shift();
if (!riffyPlayer.playing && !riffyPlayer.paused) await riffyPlayer.play();
} else if (resolve.loadType === 'search' || resolve.loadType === 'track') {
const track = resolve.tracks.shift();
if (!track) return;
track.info.requester = member;

player.queue.add(track);
if (ENV.BOT_MAX_SONGS_HISTORY_SIZE > 0) {
//await addSongToGuildSongsHistory(textChannel.guild.id, track);
}

await textChannel.send(`Added **${track.info.title}** to the queue.`);
if (player) {
if (riffyPlayer.textChannel) {
await player.textChannel.send({ embeds: [generateAddedSongMessage(track)] });
}
await player.update();
} else {
await textChannel.send({ embeds: [generateAddedSongMessage(track)] });
}

riffyPlayer.queue.add(track);

if (!player.playing && !player.paused) await player.play();
if (!riffyPlayer.playing && !riffyPlayer.paused) await riffyPlayer.play();
} else {
await textChannel.send(`There were no results found for your query.`);
}
Expand All @@ -108,7 +144,7 @@ export class AudioPlayersManager {
const player = this.playersManager.get(guild.id);
if (!player) return;
if (!riffyPlayer.paused) {
riffyPlayer.pause();
riffyPlayer.pause(true);
await player.setState('pause');
}

Expand All @@ -121,7 +157,7 @@ export class AudioPlayersManager {
const player = this.playersManager.get(guild.id);
if (!player) return;
if (riffyPlayer.paused) {
await riffyPlayer.play();
riffyPlayer.pause(false);
await player.setState('playing');
}

Expand Down Expand Up @@ -323,97 +359,44 @@ export class AudioPlayersManager {
}

private setupEvents() {
/*
if (ENV.BOT_VERBOSE_LOGGING) {
this.distube.on(DistubeEvents.DEBUG, (message) => {
loggerSend(message, loggerPrefixAudioplayer);
});
}
this.riffy.on('nodeConnect', async (node) => {
loggerSend(`Node ${node.name} has connected.`, loggerPrefixAudioplayer);
});

if (ENV.BOT_FFMPEG_LOGGING) {
this.distube.on(DistubeEvents.FFMPEG_DEBUG, (message) => {
loggerSend(message, loggerPrefixAudioplayer);
this.riffy.on('nodeError', async (node, error) => {
loggerSend(`Node ${node.name} encountered an error: ${error.message}`, loggerPrefixAudioplayer);
});

if (ENV.BOT_VERBOSE_LOGGING) {
this.riffy.on('debug', async (message) => {
loggerSend(`Riffy Debug: ${message}`, loggerPrefixAudioplayer);
});
}

this.distube.on(DistubeEvents.INIT_QUEUE, async (queue) => {
await this.playersManager.add(queue.id, queue.textChannel as TextChannel, queue);
this.riffy.on('playerCreate', async (riffyPlayer) => {
const guildTextChannel = this.client.channels.cache.get(riffyPlayer.textChannel) as GuildTextBasedChannel;
await this.playersManager.add(riffyPlayer.guildId, guildTextChannel, this.riffy);

const player = this.playersManager.get(queue.id);
const player = this.playersManager.get(riffyPlayer.guildId);
if (!player) return;

await player.init();
await player.setLeaveOnEmpty(await getGuildOptionLeaveOnEmpty(queue.id));
await player.setLeaveOnEmpty(await getGuildOptionLeaveOnEmpty(riffyPlayer.guildId));
});
this.distube.on(DistubeEvents.PLAY_SONG, async (queue) => {
const player = this.playersManager.get(queue.id);
if (player) {
await player.setState('playing');
}
});
this.distube.on(DistubeEvents.DISCONNECT, async (queue) => {
await this.playersManager.remove(queue.id);
});
this.distube.on(DistubeEvents.ADD_SONG, async (queue, song) => {
if (queue.textChannel) {
await queue.textChannel.send({ embeds: [generateAddedSongMessage(song)] });
}
if (ENV.BOT_MAX_SONGS_HISTORY_SIZE > 0) {
await addSongToGuildSongsHistory(queue.id, song);
}

const player = this.playersManager.get(queue.id);
if (player) {
await player.update();
}
this.riffy.on('playerDisconnect', async (riffyPlayer) => {
await this.playersManager.remove(riffyPlayer.guildId);
});
this.distube.on(DistubeEvents.ADD_LIST, async (queue, playlist) => {
if (ENV.BOT_MAX_SONGS_HISTORY_SIZE > 0) {
await addSongToGuildSongsHistory(queue.id, playlist);
}
if (!queue.textChannel) return;
await queue.textChannel.send({ embeds: [generateAddedPlaylistMessage(playlist)] });
if (queue.songs.length >= ENV.BOT_MAX_SONGS_IN_QUEUE) {
await queue.textChannel.send({
embeds: [
generateWarningEmbed(
i18next.t('audioplayer:event_add_list_limit', {
queueLimit: ENV.BOT_MAX_SONGS_IN_QUEUE
}) as string
)
]
});
queue.songs.length = ENV.BOT_MAX_SONGS_IN_QUEUE;
}

const player = this.playersManager.get(queue.id);
this.riffy.on('trackStart', async (riffyPlayer, track, payload) => {
const player = this.playersManager.get(riffyPlayer.guildId);
if (player) {
await player.update();
await player.setState('playing');
}
});
this.distube.on(DistubeEvents.FINISH_SONG, async (queue) => {
if (!this.playersManager.has(queue.id)) return;
if (queue._next || queue._prev || queue.stopped || queue.songs.length > 1) return;
await this.playersManager.get(queue.id)?.setState('waiting');
});
this.distube.on(DistubeEvents.ERROR, async (error, queue, song) => {
let errorName = `ERROR`;
const errorMessage = `${error.name} + \n\n + ${error.message}`;
if (song) {
errorName = song.name!;
}
if (queue.songs.length === 0) await this.stop(queue.id);

await queue.textChannel?.send({
embeds: [generateErrorEmbed(errorMessage, errorName)]
});
this.riffy.on('queueEnd', async (riffyPlayer) => {
await this.playersManager.get(riffyPlayer.guildId)?.setState('waiting');
});
*/
}
}
17 changes: 14 additions & 3 deletions src/audioplayer/PlayerButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ENV } from '../EnvironmentVariables.js';
import { UserPlaylistAddFavoriteSong } from '../schemas/SchemaPlaylist.js';
import { generateSimpleEmbed } from '../utilities/generateSimpleEmbed.js';
import i18next from 'i18next';
import { checkMemberInVoice } from '../utilities/checkMemberInVoice.js';

enum ButtonIDs {
stopMusic = 'stopMusic',
Expand Down Expand Up @@ -105,10 +106,20 @@ export class PlayerButtons {

this.collector.on('collect', async (ButtonInteraction: ButtonInteraction) => {
try {
const checkObj = await checkMemberInVoiceWithBot(ButtonInteraction.member as GuildMember);
if (!checkObj.channelTheSame) {
const isMemberInVoice = checkMemberInVoice(ButtonInteraction.member as GuildMember);
const isMemberInVoiceWithBot = checkMemberInVoiceWithBot(ButtonInteraction.member as GuildMember);

if (!isMemberInVoice) {
await ButtonInteraction.reply({
embeds: [generateErrorEmbed(i18next.t('commandsHandlers:voice_join_in_any_channel'))],
ephemeral: true
});
return;
}

if (!isMemberInVoiceWithBot) {
await ButtonInteraction.reply({
embeds: [generateErrorEmbed(checkObj.errorMessage)],
embeds: [generateErrorEmbed(i18next.t('commandsHandlers:voice_join_with_bot'))],
ephemeral: true
});
return;
Expand Down
54 changes: 30 additions & 24 deletions src/audioplayer/util/generateAddedPlaylistMessage.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
// @ts-nocheck
import { EmbedBuilder } from 'discord.js';
import i18next from 'i18next';
import { getIconFromSource } from './getIconFromSource.js';
import { nodeResponse } from 'riffy';

export function generateAddedPlaylistMessage(playlist: nodeResponse) {
const serviceIcon = getIconFromSource(playlist.pluginInfo?.name ?? undefined);
const serviceIcon = getIconFromSource(playlist.tracks[0].info.sourceName ?? undefined);

return new EmbedBuilder()
.setTitle(playlist.name ? `${serviceIcon} ${playlist.name}` : i18next.t('audioplayer:player_embed_unknown'))
.setURL(playlist.url ?? null)
.setAuthor({ name: `${i18next.t('audioplayer:event_add_list')}` })
.setThumbnail(playlist.thumbnail ?? null)
.addFields(
{
name: `${i18next.t('audioplayer:player_embed_requester')}`,
value: `${playlist.member!.user.toString()}`,
inline: true
},
{
name: `${i18next.t('audioplayer:event_add_list_songs_count')}`,
value: `\`${playlist.songs.length}\``,
inline: true
},
{
name: `${i18next.t('audioplayer:event_add_song_length')}`,
value: `\`${playlist.formattedDuration}\``,
inline: true
}
);
return (
new EmbedBuilder()
.setTitle(
playlist.playlistInfo?.name
? `${serviceIcon} ${playlist.playlistInfo.name}`
: i18next.t('audioplayer:player_embed_unknown')
)
// @ts-expect-error because
.setURL(playlist.pluginInfo?.url ?? null)
.setAuthor({ name: `${i18next.t('audioplayer:event_add_list')}` })
.setThumbnail(playlist.tracks[0].info.thumbnail ?? null)
.addFields(
{
name: `${i18next.t('audioplayer:player_embed_requester')}`,
value: `${playlist.tracks[0].info.requester}`,
inline: true
},
{
name: `${i18next.t('audioplayer:event_add_list_songs_count')}`,
value: `\`${playlist.tracks.length}\``,
inline: true
},
{
name: `${i18next.t('audioplayer:event_add_song_length')}`,
value: `\`${99999}\``,
inline: true
}
)
);
}
18 changes: 9 additions & 9 deletions src/audioplayer/util/generateAddedSongMessage.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// @ts-nocheck
import { EmbedBuilder } from 'discord.js';
import i18next from 'i18next';
import { getIconFromSource } from './getIconFromSource.js';
import { Track } from 'riffy';

export function generateAddedSongMessage(song: Song) {
const serviceIcon = getIconFromSource(song.source);
export function generateAddedSongMessage(song: Track) {
const serviceIcon = getIconFromSource(song.info.sourceName);

return new EmbedBuilder()
.setTitle(song.name ? `${serviceIcon} ${song.name}` : i18next.t('audioplayer:player_embed_unknown'))
.setURL(song.url ?? null)
.setTitle(song.info.title ? `${serviceIcon} ${song.info.title}` : i18next.t('audioplayer:player_embed_unknown'))
.setURL(song.info.uri ?? null)
.setAuthor({ name: `${i18next.t('audioplayer:event_add_song')}` })
.setThumbnail(song.thumbnail ?? null)
.setThumbnail(song.info.thumbnail ?? null)
.addFields(
{
name: `${i18next.t('audioplayer:player_embed_requester')}`,
value: `${song.member!.user.toString()}`,
value: `${song.info.requester}`,
inline: true
},
{
name: `${i18next.t('audioplayer:event_add_song_length')}`,
value: `\`${song.formattedDuration}\``,
value: `\`${song.info.length}\``,
inline: true
},
{
name: `${i18next.t('audioplayer:event_add_song_author')}`,
value: `\`${song.uploader.name ?? i18next.t('audioplayer:player_embed_unknown')}\``,
value: `\`${song.info.author ?? i18next.t('audioplayer:player_embed_unknown')}\``,
inline: true
}
);
Expand Down
Loading

0 comments on commit a962a93

Please sign in to comment.