diff --git a/package.json b/package.json index 6e816fc..622d1b6 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/runInDocker.sh b/runInDocker.sh index c00a509..defb8ab 100644 --- a/runInDocker.sh +++ b/runInDocker.sh @@ -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 diff --git a/src/audioplayer/AudioPlayersManager.ts b/src/audioplayer/AudioPlayersManager.ts index bf95ff1..f94a942 100644 --- a/src/audioplayer/AudioPlayersManager.ts +++ b/src/audioplayer/AudioPlayersManager.ts @@ -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'; @@ -9,10 +8,9 @@ import { generateAddedSongMessage } from './util/generateAddedSongMessage.js'; import { ButtonInteraction, Client, - CommandInteraction, - EmbedBuilder, Guild, GuildMember, + GuildTextBasedChannel, Interaction, TextChannel, VoiceBasedChannel @@ -54,36 +52,74 @@ export class AudioPlayersManager { member: GuildMember ): Promise { 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.`); } @@ -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'); } @@ -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'); } @@ -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'); }); - - */ } } diff --git a/src/audioplayer/PlayerButtons.ts b/src/audioplayer/PlayerButtons.ts index 2789331..fe04343 100644 --- a/src/audioplayer/PlayerButtons.ts +++ b/src/audioplayer/PlayerButtons.ts @@ -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', @@ -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; diff --git a/src/audioplayer/eventsHandlers/AudioPlayerEventChannelDelete.ts b/src/audioplayer/discordEventsHandlers/AudioPlayerEventChannelDelete.ts similarity index 100% rename from src/audioplayer/eventsHandlers/AudioPlayerEventChannelDelete.ts rename to src/audioplayer/discordEventsHandlers/AudioPlayerEventChannelDelete.ts diff --git a/src/audioplayer/eventsHandlers/AudioPlayerEventMessageCreate.ts b/src/audioplayer/discordEventsHandlers/AudioPlayerEventMessageCreate.ts similarity index 100% rename from src/audioplayer/eventsHandlers/AudioPlayerEventMessageCreate.ts rename to src/audioplayer/discordEventsHandlers/AudioPlayerEventMessageCreate.ts diff --git a/src/audioplayer/eventsHandlers/AudioPlayerEventMessageDeleted.ts b/src/audioplayer/discordEventsHandlers/AudioPlayerEventMessageDeleted.ts similarity index 100% rename from src/audioplayer/eventsHandlers/AudioPlayerEventMessageDeleted.ts rename to src/audioplayer/discordEventsHandlers/AudioPlayerEventMessageDeleted.ts diff --git a/src/audioplayer/eventsHandlers/AudioPlayerEventRaw.ts b/src/audioplayer/discordEventsHandlers/AudioPlayerEventRaw.ts similarity index 100% rename from src/audioplayer/eventsHandlers/AudioPlayerEventRaw.ts rename to src/audioplayer/discordEventsHandlers/AudioPlayerEventRaw.ts diff --git a/src/audioplayer/eventsHandlers/AudioPlayerEventVoiceChannelUpdate.ts b/src/audioplayer/discordEventsHandlers/AudioPlayerEventVoiceChannelUpdate.ts similarity index 100% rename from src/audioplayer/eventsHandlers/AudioPlayerEventVoiceChannelUpdate.ts rename to src/audioplayer/discordEventsHandlers/AudioPlayerEventVoiceChannelUpdate.ts diff --git a/src/audioplayer/util/generateAddedPlaylistMessage.ts b/src/audioplayer/util/generateAddedPlaylistMessage.ts index f30bdff..ecbe625 100644 --- a/src/audioplayer/util/generateAddedPlaylistMessage.ts +++ b/src/audioplayer/util/generateAddedPlaylistMessage.ts @@ -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 + } + ) + ); } diff --git a/src/audioplayer/util/generateAddedSongMessage.ts b/src/audioplayer/util/generateAddedSongMessage.ts index ab1e66f..492d7af 100644 --- a/src/audioplayer/util/generateAddedSongMessage.ts +++ b/src/audioplayer/util/generateAddedSongMessage.ts @@ -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 } ); diff --git a/src/events/interactionHandlers/slashCommandHandler.ts b/src/events/interactionHandlers/slashCommandHandler.ts index 95b666b..b784e98 100644 --- a/src/events/interactionHandlers/slashCommandHandler.ts +++ b/src/events/interactionHandlers/slashCommandHandler.ts @@ -1,6 +1,5 @@ import { GuildMember, Interaction, TextChannel } from 'discord.js'; import { generateErrorEmbed } from '../../utilities/generateErrorEmbed.js'; -import { checkBotInVoice } from '../../utilities/checkBotInVoice.js'; import { checkMemberInVoiceWithBot } from '../../utilities/checkMemberInVoiceWithBot.js'; import { checkMemberInVoice } from '../../utilities/checkMemberInVoice.js'; import { CheckBotPermissions } from '../../utilities/checkPermissions.js'; @@ -29,20 +28,21 @@ export async function slashCommandHandler(interaction: Interaction) { } if (command.guild_data.voice_required) { + const isMemberInVoice = checkMemberInVoice(interaction.member as GuildMember); + if (command.guild_data.voice_with_bot_only) { - if (checkBotInVoice(interaction.guild)) { - const checkObj = await checkMemberInVoiceWithBot(interaction.member); - if (!checkObj.channelTheSame) { - await interaction.reply({ - embeds: [generateErrorEmbed(checkObj.errorMessage)], - ephemeral: true - }); - return; - } + const isMemberInVoiceWithBot = checkMemberInVoiceWithBot(interaction.member as GuildMember); + + if (!isMemberInVoiceWithBot) { + await interaction.reply({ + embeds: [generateErrorEmbed(i18next.t('commandsHandlers:voice_join_with_bot'))], + ephemeral: true + }); + return; } } - if (!checkMemberInVoice(interaction.member)) { + if (!isMemberInVoice) { await interaction.reply({ embeds: [generateErrorEmbed(i18next.t('commandsHandlers:command_only_in_voice'))], ephemeral: true diff --git a/src/events/messageCreate.event.ts b/src/events/messageCreate.event.ts index f3ad852..8f8f52c 100644 --- a/src/events/messageCreate.event.ts +++ b/src/events/messageCreate.event.ts @@ -1,7 +1,7 @@ import { BotEvent } from '../DiscordTypes.js'; import { Client, Events, Message, TextChannel } from 'discord.js'; import { textCommandsHandler } from './messageHandlers/textCommandsHandler.js'; -import { AudioPlayerEventMessageCreate } from '../audioplayer/eventsHandlers/AudioPlayerEventMessageCreate.js'; +import { AudioPlayerEventMessageCreate } from '../audioplayer/discordEventsHandlers/AudioPlayerEventMessageCreate.js'; const event: BotEvent = { name: Events.MessageCreate, diff --git a/src/events/messageDeleted.event.ts b/src/events/messageDeleted.event.ts index dc035da..7b294ce 100644 --- a/src/events/messageDeleted.event.ts +++ b/src/events/messageDeleted.event.ts @@ -1,6 +1,6 @@ import { BotEvent } from '../DiscordTypes.js'; import { Client, Events, Message } from 'discord.js'; -import { AudioPlayerEventMessageDeleted } from '../audioplayer/eventsHandlers/AudioPlayerEventMessageDeleted.js'; +import { AudioPlayerEventMessageDeleted } from '../audioplayer/discordEventsHandlers/AudioPlayerEventMessageDeleted.js'; const event: BotEvent = { name: Events.MessageDelete, diff --git a/src/events/messageHandlers/textCommandsHandler.ts b/src/events/messageHandlers/textCommandsHandler.ts index 223c3e2..71011ce 100644 --- a/src/events/messageHandlers/textCommandsHandler.ts +++ b/src/events/messageHandlers/textCommandsHandler.ts @@ -1,11 +1,10 @@ import { ICommand } from '../../CommandTypes.js'; import { generateSpecificCommandHelp } from '../../commands/info/help.command.js'; import { generateErrorEmbed } from '../../utilities/generateErrorEmbed.js'; -import { checkBotInVoice } from '../../utilities/checkBotInVoice.js'; import { checkMemberInVoiceWithBot } from '../../utilities/checkMemberInVoiceWithBot.js'; import { checkMemberInVoice } from '../../utilities/checkMemberInVoice.js'; import { CheckBotPermissions, CheckMemberPermissions } from '../../utilities/checkPermissions.js'; -import { Client, Message, TextChannel } from 'discord.js'; +import { Client, GuildMember, Message, TextChannel } from 'discord.js'; import { loggerError } from '../../utilities/logger.js'; import i18next from 'i18next'; import { loggerPrefixCommandHandler } from '../../handlers/Command.handler.js'; @@ -70,17 +69,20 @@ export async function textCommandsHandler(client: Client, message: Message) { } if (command.guild_data.voice_required) { + const isMemberInVoice = checkMemberInVoice(message.member as GuildMember); + if (command.guild_data.voice_with_bot_only) { - if (checkBotInVoice(message.member!.guild)) { - const checkObj = await checkMemberInVoiceWithBot(message.member!); - if (!checkObj.channelTheSame) { - await message.reply({ content: checkObj.errorMessage }); - return; - } + const isMemberInVoiceWithBot = checkMemberInVoiceWithBot(message.member as GuildMember); + + if (!isMemberInVoiceWithBot) { + await message.reply({ + embeds: [generateErrorEmbed(i18next.t('commandsHandlers:voice_join_with_bot'))] + }); + return; } } - if (!checkMemberInVoice(message.member!)) { + if (!isMemberInVoice) { await message.reply({ embeds: [generateErrorEmbed(i18next.t('commandsHandlers:command_only_in_voice'))] }); diff --git a/src/events/onChannelDelete.event.ts b/src/events/onChannelDelete.event.ts index 8bb609f..f9a31a6 100644 --- a/src/events/onChannelDelete.event.ts +++ b/src/events/onChannelDelete.event.ts @@ -1,6 +1,6 @@ import { BotEvent } from '../DiscordTypes.js'; import { Client, Events, TextChannel } from 'discord.js'; -import { AudioPlayerEventChannelDelete } from '../audioplayer/eventsHandlers/AudioPlayerEventChannelDelete.js'; +import { AudioPlayerEventChannelDelete } from '../audioplayer/discordEventsHandlers/AudioPlayerEventChannelDelete.js'; const event: BotEvent = { name: Events.ChannelDelete, diff --git a/src/events/raw.event.ts b/src/events/raw.event.ts index 04e5bec..3abfcb2 100644 --- a/src/events/raw.event.ts +++ b/src/events/raw.event.ts @@ -1,6 +1,6 @@ import { BotEvent } from '../DiscordTypes.js'; import { Events, GatewayDispatchEvents } from 'discord.js'; -import { AudioPlayerEventRaw } from '../audioplayer/eventsHandlers/AudioPlayerEventRaw.js'; +import { AudioPlayerEventRaw } from '../audioplayer/discordEventsHandlers/AudioPlayerEventRaw.js'; const event: BotEvent = { name: Events.Raw, diff --git a/src/events/voiceChannelUpdate.event.ts b/src/events/voiceChannelUpdate.event.ts index 029374a..21dd4fb 100644 --- a/src/events/voiceChannelUpdate.event.ts +++ b/src/events/voiceChannelUpdate.event.ts @@ -1,6 +1,6 @@ import { BotEvent } from '../DiscordTypes.js'; import { Client, Events, VoiceState } from 'discord.js'; -import { AudioPlayerEventVoiceChannelUpdate } from '../audioplayer/eventsHandlers/AudioPlayerEventVoiceChannelUpdate.js'; +import { AudioPlayerEventVoiceChannelUpdate } from '../audioplayer/discordEventsHandlers/AudioPlayerEventVoiceChannelUpdate.js'; export const event: BotEvent = { name: Events.VoiceStateUpdate, diff --git a/src/locales/en/commandsHandlers.json b/src/locales/en/commandsHandlers.json index 658dfe1..0cac5e7 100644 --- a/src/locales/en/commandsHandlers.json +++ b/src/locales/en/commandsHandlers.json @@ -6,5 +6,6 @@ "command_only_in_guilds": "This command working only on servers", "command_only_in_voice": "You need to be in voice channel for executing this command", "voice_join_in_any_channel": "Please, join in any voice channel", - "voice_join_in_channel": "Please, join in voice channel" + "voice_join_in_channel": "Please, join in voice channel", + "voice_join_with_bot": "Please, join in voice channel where is bot" } diff --git a/src/locales/ru/commandsHandlers.json b/src/locales/ru/commandsHandlers.json index a8d0bdc..41144b6 100644 --- a/src/locales/ru/commandsHandlers.json +++ b/src/locales/ru/commandsHandlers.json @@ -6,5 +6,6 @@ "command_only_in_guilds": "Эта команда может выполняться только на серверах", "command_only_in_voice": "Вы должны быть в любом голосовом канале для выполнения этой команды", "voice_join_in_any_channel": "Зайди в любой голосовой канал", - "voice_join_in_channel": "Зайди в голосовой канал" + "voice_join_in_channel": "Зайди в голосовой канал", + "voice_join_with_bot": "Зайди в голосовой канал где находится бот" } diff --git a/src/utilities/checkBotInVoice.ts b/src/utilities/checkBotInVoice.ts index b7a4f05..3eb7f81 100644 --- a/src/utilities/checkBotInVoice.ts +++ b/src/utilities/checkBotInVoice.ts @@ -1,7 +1,5 @@ import { Guild } from 'discord.js'; -import { getVoiceConnection, VoiceConnection } from '@discordjs/voice'; -export function checkBotInVoice(guild: Guild): VoiceConnection | undefined { - //return getVoiceConnection(guild.id, guild.client.user?.id) ?? getVoiceConnection(guild.id) - return getVoiceConnection(guild.id); +export function checkBotInVoice(guild: Guild): boolean { + return !!guild.members.me?.voice.channelId; } diff --git a/src/utilities/checkMemberInVoice.ts b/src/utilities/checkMemberInVoice.ts index 939a443..bbc0103 100644 --- a/src/utilities/checkMemberInVoice.ts +++ b/src/utilities/checkMemberInVoice.ts @@ -1,6 +1,6 @@ import { GuildMember } from 'discord.js'; export function checkMemberInVoice(member: GuildMember) { - const voice = member.voice.channel; + const voice = member.voice.channelId; return voice !== null; } diff --git a/src/utilities/checkMemberInVoiceWithBot.ts b/src/utilities/checkMemberInVoiceWithBot.ts index f67cacc..7369826 100644 --- a/src/utilities/checkMemberInVoiceWithBot.ts +++ b/src/utilities/checkMemberInVoiceWithBot.ts @@ -1,39 +1,12 @@ -import { GuildMember, VoiceChannel } from 'discord.js'; -import { checkBotInVoice } from './checkBotInVoice.js'; -import i18next from 'i18next'; +import { GuildMember } from 'discord.js'; -export async function checkMemberInVoiceWithBot( - member: GuildMember -): Promise<{ errorMessage: string; channelTheSame: boolean }> { - const response = { - channelTheSame: false, - errorMessage: 'CheckingVoiceError' - }; +export function checkMemberInVoiceWithBot(member: GuildMember): boolean { + const memberChannel = member.voice.channelId; + const clientChannel = member.guild.members.me?.voice.channelId; - try { - const connection = checkBotInVoice(member.guild); - if (connection) { - if (member.voice.channel) { - response.channelTheSame = member.guild.members.me?.voice.channel?.id === member.voice?.channel.id; - if (response.channelTheSame) { - return response; - } - } else { - response.errorMessage = i18next.t('commandsHandlers:voice_join_in_any_channel'); - return response; - } - - await member.guild.client.channels.fetch(member.guild.members.me?.voice.channel?.id).then((channel) => { - if (channel) { - if (channel instanceof VoiceChannel) { - response.errorMessage = `${i18next.t('commandsHandlers:voice_join_in_channel')} ${channel.name}`; - } - } - }); - } - } catch { - return response; + if (!memberChannel || !clientChannel) { + return false; } - return response; + return memberChannel === clientChannel; }