Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: Also validate is user is in same voice channel as bot for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Jul 27, 2023
1 parent f590558 commit 311e141
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/commands/player/filters.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -31,6 +31,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/leave.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -34,6 +34,10 @@ module.exports = {
});
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (!queue.deleted) {
queue.delete();
logger.debug(
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/loop.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -36,6 +36,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

const loopModesFormatted = new Map([
[0, 'disabled'],
[1, 'track'],
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/nowplaying.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -24,6 +24,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/pause.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -24,6 +24,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
Expand Down
13 changes: 9 additions & 4 deletions src/commands/player/play.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -48,15 +48,20 @@ module.exports = {

return interaction.respond(response);
},
execute: async ({ interaction }) => {
if (await notInVoiceChannel(interaction)) {
execute: async ({ interaction, client }) => {
if (await notInVoiceChannel(interaction, client)) {
return;
}

if (await cannotJoinVoiceOrTalk(interaction)) {
return;
}

let queue = useQueue(interaction.guild.id);
if (queue && (await notInSameVoiceChannel(interaction, queue))) {
return;
}

const player = useMainPlayer();
const query = interaction.options.getString('query');

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/commands/player/queue.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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 = '';

Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/remove.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/seek.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -30,6 +30,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/shuffle.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -24,6 +24,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (await queueIsEmpty(interaction, queue)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/skip.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -27,6 +27,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/stop.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -34,6 +34,10 @@ module.exports = {
});
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

if (!queue.deleted) {
queue.setRepeatMode(0);
queue.clear();
Expand Down
6 changes: 5 additions & 1 deletion src/commands/player/volume.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -31,6 +31,10 @@ module.exports = {
return;
}

if (await notInSameVoiceChannel(interaction, queue)) {
return;
}

const volume = interaction.options.getNumber('percentage');

if (!volume && volume !== 0) {
Expand Down
25 changes: 24 additions & 1 deletion src/utils/validation/voiceChannelValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand All @@ -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;
};

0 comments on commit 311e141

Please sign in to comment.