From b68b7dac95d564cf1fccc314f74e968e94adc3ae Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 23 Jul 2023 11:19:21 +0200 Subject: [PATCH] feat: Renamed validator files and added permission validator --- src/commands/player/filters.js | 4 ++-- src/commands/player/leave.js | 2 +- src/commands/player/loop.js | 4 ++-- src/commands/player/nowplaying.js | 4 ++-- src/commands/player/pause.js | 4 ++-- src/commands/player/play.js | 9 +++++-- src/commands/player/queue.js | 2 +- src/commands/player/remove.js | 4 ++-- src/commands/player/seek.js | 4 ++-- src/commands/player/skip.js | 4 ++-- src/commands/player/volume.js | 4 ++-- src/commands/system/guilds.js | 2 +- src/commands/system/status.js | 2 +- src/utils/validation/permissionValidator.js | 24 +++++++++++++++++++ .../{queueValidation.js => queueValidator.js} | 0 ...yValidation.js => searchQueryValidator.js} | 0 ...alidation.js => systemCommandValidator.js} | 0 ...Validation.js => voiceChannelValidator.js} | 0 18 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 src/utils/validation/permissionValidator.js rename src/utils/validation/{queueValidation.js => queueValidator.js} (100%) rename src/utils/validation/{searchQueryValidation.js => searchQueryValidator.js} (100%) rename src/utils/validation/{systemCommandValidation.js => systemCommandValidator.js} (100%) rename src/utils/validation/{voiceChannelValidation.js => voiceChannelValidator.js} (100%) diff --git a/src/commands/player/filters.js b/src/commands/player/filters.js index 9eb90ef8..1622f85e 100644 --- a/src/commands/player/filters.js +++ b/src/commands/player/filters.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions, ffmpegFilterOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder, diff --git a/src/commands/player/leave.js b/src/commands/player/leave.js index 8bd2a82a..26e4414d 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/voiceChannelValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/loop.js b/src/commands/player/loop.js index 66da9f76..2e536b29 100644 --- a/src/commands/player/loop.js +++ b/src/commands/player/loop.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions, botOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/nowplaying.js b/src/commands/player/nowplaying.js index 8f303461..9b00bf86 100644 --- a/src/commands/player/nowplaying.js +++ b/src/commands/player/nowplaying.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions, playerOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/pause.js b/src/commands/player/pause.js index 6f7472bf..f60ee759 100644 --- a/src/commands/player/pause.js +++ b/src/commands/player/pause.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/play.js b/src/commands/player/play.js index fc8dd0bc..ad47c319 100644 --- a/src/commands/player/play.js +++ b/src/commands/player/play.js @@ -1,7 +1,8 @@ const logger = require('../../services/logger'); const { embedOptions, playerOptions, botOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { transformQuery } = require('../../utils/validation/searchQueryValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { cannotJoinVoiceOrTalk } = require('../../utils/validation/permissionValidator'); +const { transformQuery } = require('../../utils/validation/searchQueryValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useMainPlayer, useQueue } = require('discord-player'); @@ -16,6 +17,10 @@ module.exports = { return; } + if (await cannotJoinVoiceOrTalk(interaction)) { + return; + } + const player = useMainPlayer(); const query = interaction.options.getString('query'); diff --git a/src/commands/player/queue.js b/src/commands/player/queue.js index c782e478..ae8e64dd 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/voiceChannelValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/remove.js b/src/commands/player/remove.js index bcc585c6..e208a9b3 100644 --- a/src/commands/player/remove.js +++ b/src/commands/player/remove.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/seek.js b/src/commands/player/seek.js index 0a2f680a..e7c66352 100644 --- a/src/commands/player/seek.js +++ b/src/commands/player/seek.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/skip.js b/src/commands/player/skip.js index 1aa8c4c7..d4a886e4 100644 --- a/src/commands/player/skip.js +++ b/src/commands/player/skip.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/player/volume.js b/src/commands/player/volume.js index 4d607e71..f4dc3ea5 100644 --- a/src/commands/player/volume.js +++ b/src/commands/player/volume.js @@ -1,7 +1,7 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation'); -const { queueDoesNotExist } = require('../../utils/validation/queueValidation'); +const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidator'); +const { queueDoesNotExist } = require('../../utils/validation/queueValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { useQueue } = require('discord-player'); diff --git a/src/commands/system/guilds.js b/src/commands/system/guilds.js index 3bb31345..10c46bbe 100644 --- a/src/commands/system/guilds.js +++ b/src/commands/system/guilds.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notValidGuildId } = require('../../utils/validation/systemCommandValidation'); +const { notValidGuildId } = require('../../utils/validation/systemCommandValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); module.exports = { diff --git a/src/commands/system/status.js b/src/commands/system/status.js index 2a30a101..26d7f399 100644 --- a/src/commands/system/status.js +++ b/src/commands/system/status.js @@ -1,6 +1,6 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notValidGuildId } = require('../../utils/validation/systemCommandValidation'); +const { notValidGuildId } = require('../../utils/validation/systemCommandValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const osu = require('node-os-utils'); const { version, dependencies } = require('../../../package.json'); diff --git a/src/utils/validation/permissionValidator.js b/src/utils/validation/permissionValidator.js new file mode 100644 index 00000000..5a308f4d --- /dev/null +++ b/src/utils/validation/permissionValidator.js @@ -0,0 +1,24 @@ +const logger = require('../../services/logger'); +const { embedOptions } = require('../../config'); +const { EmbedBuilder } = require('discord.js'); + +exports.cannotJoinVoiceOrTalk = async (interaction) => { + const channel = interaction.member.voice.channel; + + if (!channel.joinable || !channel.speakable) { + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${embedOptions.icons.warning} Oops!**\nI do not have permission to play audio in the voice channel you are in.\n\nPlease make sure I have the **Connect** and **Speak** permissions in this voice channel.` + ) + .setColor(embedOptions.colors.warning) + ] + }); + + logger.debug(`User tried to use command ${interaction.commandName} but was not in a voice channel.`); + return true; + } + + return false; +}; diff --git a/src/utils/validation/queueValidation.js b/src/utils/validation/queueValidator.js similarity index 100% rename from src/utils/validation/queueValidation.js rename to src/utils/validation/queueValidator.js diff --git a/src/utils/validation/searchQueryValidation.js b/src/utils/validation/searchQueryValidator.js similarity index 100% rename from src/utils/validation/searchQueryValidation.js rename to src/utils/validation/searchQueryValidator.js diff --git a/src/utils/validation/systemCommandValidation.js b/src/utils/validation/systemCommandValidator.js similarity index 100% rename from src/utils/validation/systemCommandValidation.js rename to src/utils/validation/systemCommandValidator.js diff --git a/src/utils/validation/voiceChannelValidation.js b/src/utils/validation/voiceChannelValidator.js similarity index 100% rename from src/utils/validation/voiceChannelValidation.js rename to src/utils/validation/voiceChannelValidator.js