From 311e1419922c4fc7a02952bda772ce705e567736 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Thu, 27 Jul 2023 22:59:13 +0200 Subject: [PATCH] fix: Also validate is user is in same voice channel as bot for commands --- src/commands/player/filters.js | 6 ++++- src/commands/player/leave.js | 6 ++++- src/commands/player/loop.js | 6 ++++- src/commands/player/nowplaying.js | 6 ++++- src/commands/player/pause.js | 6 ++++- src/commands/player/play.js | 13 +++++++--- src/commands/player/queue.js | 7 +++++- src/commands/player/remove.js | 6 ++++- src/commands/player/seek.js | 6 ++++- src/commands/player/shuffle.js | 6 ++++- src/commands/player/skip.js | 6 ++++- src/commands/player/stop.js | 6 ++++- src/commands/player/volume.js | 6 ++++- src/utils/validation/voiceChannelValidator.js | 25 ++++++++++++++++++- 14 files changed, 94 insertions(+), 17 deletions(-) diff --git a/src/commands/player/filters.js b/src/commands/player/filters.js index 77573acf..2b6efa16 100644 --- a/src/commands/player/filters.js +++ b/src/commands/player/filters.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions, ffmpegFilterOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, @@ -31,6 +31,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (await queueNoCurrentTrack(interaction, queue)) { return; } diff --git a/src/commands/player/leave.js b/src/commands/player/leave.js index 713a049a..b8226c02 100644 --- a/src/commands/player/leave.js +++ b/src/commands/player/leave.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -34,6 +34,10 @@ module.exports = { }); } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (!queue.deleted) { queue.delete(); logger.debug( diff --git a/src/commands/player/loop.js b/src/commands/player/loop.js index 87ec55b0..d727a004 100644 --- a/src/commands/player/loop.js +++ b/src/commands/player/loop.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions, botOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -36,6 +36,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + const loopModesFormatted = new Map([ [0, 'disabled'], [1, 'track'], diff --git a/src/commands/player/nowplaying.js b/src/commands/player/nowplaying.js index e6849545..7f9feab9 100644 --- a/src/commands/player/nowplaying.js +++ b/src/commands/player/nowplaying.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions, playerOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -24,6 +24,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (await queueNoCurrentTrack(interaction, queue)) { return; } diff --git a/src/commands/player/pause.js b/src/commands/player/pause.js index 0a2c3c10..a5ff91a3 100644 --- a/src/commands/player/pause.js +++ b/src/commands/player/pause.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -24,6 +24,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (await queueNoCurrentTrack(interaction, queue)) { return; } diff --git a/src/commands/player/play.js b/src/commands/player/play.js index 6a0e0ada..6ccaa4fc 100644 --- a/src/commands/player/play.js +++ b/src/commands/player/play.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions, playerOptions, botOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { cannotJoinVoiceOrTalk } = require('../../utils/validation/permissionValidator'); const { transformQuery } = require('../../utils/validation/searchQueryValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); @@ -48,8 +48,8 @@ module.exports = { return interaction.respond(response); }, - execute: async ({ interaction }) => { - if (await notInVoiceChannel(interaction)) { + execute: async ({ interaction, client }) => { + if (await notInVoiceChannel(interaction, client)) { return; } @@ -57,6 +57,11 @@ module.exports = { return; } + let queue = useQueue(interaction.guild.id); + if (queue && (await notInSameVoiceChannel(interaction, queue))) { + return; + } + const player = useMainPlayer(); const query = interaction.options.getString('query'); @@ -107,7 +112,7 @@ module.exports = { }); } - let queue = useQueue(interaction.guild.id); + queue = useQueue(interaction.guild.id); let queueSize = queue?.size ?? 0; if ((searchResult.playlist && searchResult.tracks.length) > playerOptions.maxQueueSize - queueSize) { diff --git a/src/commands/player/queue.js b/src/commands/player/queue.js index 9eebfd80..eb41e3f3 100644 --- a/src/commands/player/queue.js +++ b/src/commands/player/queue.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions, playerOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -19,6 +19,11 @@ module.exports = { } const queue = useQueue(interaction.guild.id); + + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + const pageIndex = (interaction.options.getNumber('page') || 1) - 1; let queueString = ''; diff --git a/src/commands/player/remove.js b/src/commands/player/remove.js index e371dff8..85b96bbd 100644 --- a/src/commands/player/remove.js +++ b/src/commands/player/remove.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -31,6 +31,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + const removeTrackNumber = interaction.options.getNumber('tracknumber'); if (removeTrackNumber > queue.tracks.data.length) { diff --git a/src/commands/player/seek.js b/src/commands/player/seek.js index e5bd3f79..311ac755 100644 --- a/src/commands/player/seek.js +++ b/src/commands/player/seek.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -30,6 +30,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (await queueNoCurrentTrack(interaction, queue)) { return; } diff --git a/src/commands/player/shuffle.js b/src/commands/player/shuffle.js index 06204eb8..f74d2b17 100644 --- a/src/commands/player/shuffle.js +++ b/src/commands/player/shuffle.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist, queueIsEmpty } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -24,6 +24,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (await queueIsEmpty(interaction, queue)) { return; } diff --git a/src/commands/player/skip.js b/src/commands/player/skip.js index 18a16458..c2170209 100644 --- a/src/commands/player/skip.js +++ b/src/commands/player/skip.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -27,6 +27,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (await queueNoCurrentTrack(interaction, queue)) { return; } diff --git a/src/commands/player/stop.js b/src/commands/player/stop.js index 7ac9ef48..3bc7d4d5 100644 --- a/src/commands/player/stop.js +++ b/src/commands/player/stop.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -34,6 +34,10 @@ module.exports = { }); } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + if (!queue.deleted) { queue.setRepeatMode(0); queue.clear(); diff --git a/src/commands/player/volume.js b/src/commands/player/volume.js index efbaf4b2..5c7fe9e4 100644 --- a/src/commands/player/volume.js +++ b/src/commands/player/volume.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { notInVoiceChannel, notInSameVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { queueDoesNotExist } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); @@ -31,6 +31,10 @@ module.exports = { return; } + if (await notInSameVoiceChannel(interaction, queue)) { + return; + } + const volume = interaction.options.getNumber('percentage'); if (!volume && volume !== 0) { diff --git a/src/utils/validation/voiceChannelValidator.js b/src/utils/validation/voiceChannelValidator.js index 750e96ec..8fc85092 100644 --- a/src/utils/validation/voiceChannelValidator.js +++ b/src/utils/validation/voiceChannelValidator.js @@ -8,7 +8,7 @@ exports.notInVoiceChannel = async (interaction) => { embeds: [ new EmbedBuilder() .setDescription( - `**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.` + `**${embedOptions.icons.warning} Not in a voice channel**\nYou need to be in a voice channel to use this command.` ) .setColor(embedOptions.colors.warning) ] @@ -20,3 +20,26 @@ exports.notInVoiceChannel = async (interaction) => { return false; }; + +exports.notInSameVoiceChannel = async (interaction, queue) => { + if (!queue.dispatcher) { + return true; + } + + if (interaction.member.voice.channel.id !== queue.dispatcher.channel.id) { + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${embedOptions.icons.warning} Not in same voice channel**\nYou need to be in the same voice channel as me to use this command.\n\n**Voice channel:** ${queue.dispatcher.channel.name}` + ) + .setColor(embedOptions.colors.warning) + ] + }); + + logger.debug(`User tried to use command ${interaction.commandName} but was not in the same voice channel.`); + return true; + } + + return false; +};