Merge pull request #74 from mariusbegby/validation

feat: Added common logic for validating command execution
This commit is contained in:
Marius Begby authored and GitHub committed 2023-07-18 14:53:50 +02:00
commit aedc6ac06d
16 files changed
+155 -244

No files matched your search

+11 -21
View File
@@ -1,4 +1,6 @@
const { embedOptions, ffmpegFilterOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
const {
SlashCommandBuilder,
EmbedBuilder,
@@ -15,30 +17,18 @@ module.exports = {
.setDescription('Toggle various audio filters during playback.')
.setDMPermission(false),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = await useQueue(interaction.guild.id);
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
let filterOptions = [];
+3 -10
View File
@@ -1,4 +1,5 @@
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -10,16 +11,8 @@ module.exports = {
// todo, allow command to be executed if bot is only member in voice channel
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
+6 -20
View File
@@ -1,4 +1,6 @@
const { embedOptions, botOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -20,30 +22,14 @@ module.exports = {
)
),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
const loopModesFormatted = new Map([
+8 -30
View File
@@ -1,4 +1,6 @@
const { embedOptions, playerOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -8,42 +10,18 @@ module.exports = {
.setDescription('Show information about the track currently playing.')
.setDMPermission(false),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
if (!queue.currentTrack) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
const sourceStringsFormatted = new Map([
+8 -30
View File
@@ -1,4 +1,6 @@
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -8,42 +10,18 @@ module.exports = {
.setDescription('Pause or resume the current track.')
.setDMPermission(false),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
if (!queue.currentTrack) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
let durationFormat =
+3 -10
View File
@@ -1,4 +1,5 @@
const { embedOptions, playerOptions, botOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useMainPlayer, useQueue } = require('discord-player');
@@ -9,16 +10,8 @@ module.exports = {
.setDMPermission(false)
.addStringOption((option) => option.setName('query').setDescription('Search query or URL.').setRequired(true)),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const player = useMainPlayer();
+4 -11
View File
@@ -1,4 +1,5 @@
const { embedOptions, playerOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -9,20 +10,12 @@ module.exports = {
.setDMPermission(false)
.addNumberOption((option) => option.setName('page').setDescription('Page number of the queue').setMinValue(1)),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const pageIndex = (interaction.options.getNumber('page') || 1) - 1;
const queue = useQueue(interaction.guild.id);
const pageIndex = (interaction.options.getNumber('page') || 1) - 1;
let queueString = '';
if (!queue) {
+6 -20
View File
@@ -1,4 +1,6 @@
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -15,30 +17,14 @@ module.exports = {
.setRequired(true)
),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
const removeTrackNumber = interaction.options.getNumber('tracknumber');
+8 -30
View File
@@ -1,4 +1,6 @@
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -14,42 +16,18 @@ module.exports = {
.setRequired(true)
),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
if (!queue.currentTrack) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
const durationInput = interaction.options.getString('duration');
+10 -20
View File
@@ -1,4 +1,6 @@
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist, queueNoCurrentTrack } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -11,30 +13,18 @@ module.exports = {
option.setName('tracknumber').setDescription('Track number to skip to in the queue.').setMinValue(1)
),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
if (await queueNoCurrentTrack(interaction, queue)) {
return;
}
const skipToTrack = interaction.options.getNumber('tracknumber');
+6 -20
View File
@@ -1,4 +1,6 @@
const { embedOptions } = require('../../config');
const { notInVoiceChannel } = require('../../utils/validation/voiceChannelValidation');
const { queueDoesNotExist } = require('../../utils/validation/queueValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
@@ -15,30 +17,14 @@ module.exports = {
.setMaxValue(100)
),
execute: async ({ interaction }) => {
if (!interaction.member.voice.channel) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notInVoiceChannel(interaction)) {
return;
}
const queue = useQueue(interaction.guild.id);
if (!queue) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
if (await queueDoesNotExist(interaction, queue)) {
return;
}
const volume = interaction.options.getNumber('percentage');
+4 -11
View File
@@ -1,4 +1,5 @@
const { embedOptions, systemOptions } = require('../../config');
const { embedOptions } = require('../../config');
const { notValidGuildId } = require('../../utils/validation/systemCommandValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
@@ -8,16 +9,8 @@ module.exports = {
.setDescription('Show list of guilds where bot is added.')
.setDMPermission(false),
execute: async ({ interaction, client }) => {
if (!systemOptions.systemGuildIds.includes(interaction.guildId)) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command \`${interaction.commandName}\` cannot be executed in this server.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notValidGuildId(interaction)) {
return;
}
let guildsList = client.guilds.cache
+4 -11
View File
@@ -1,4 +1,5 @@
const { embedOptions, systemOptions } = require('../../config');
const { embedOptions } = require('../../config');
const { notValidGuildId } = require('../../utils/validation/systemCommandValidation');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { useQueue } = require('discord-player');
const osu = require('node-os-utils');
@@ -8,16 +9,8 @@ module.exports = {
isSystemCommand: true,
data: new SlashCommandBuilder().setName('status').setDescription('Show bot status.').setDMPermission(false),
execute: async ({ interaction, client }) => {
if (!systemOptions.systemGuildIds.includes(interaction.guildId)) {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command \`${interaction.commandName}\` cannot be executed in this server.`
)
.setColor(embedOptions.colors.warning)
]
});
if (await notValidGuildId(interaction)) {
return;
}
const interactionLatency = Date.now() - interaction.createdTimestamp;
+36
View File
@@ -0,0 +1,36 @@
const { embedOptions } = require('../../config');
const { EmbedBuilder } = require('discord.js');
exports.queueDoesNotExist = async (interaction, queue) => {
if (!queue) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere are no tracks in the queue and nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
return true;
}
return false;
};
exports.queueNoCurrentTrack = async (interaction, queue) => {
if (!queue.currentTrack) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
return true;
}
return false;
};
@@ -0,0 +1,19 @@
const { embedOptions, systemOptions } = require('../../config');
const { EmbedBuilder } = require('discord.js');
exports.notValidGuildId = async (interaction) => {
if (!systemOptions.systemGuildIds.includes(interaction.guildId)) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command \`${interaction.commandName}\` cannot be executed in this server.`
)
.setColor(embedOptions.colors.warning)
]
});
return true;
}
return false;
};
@@ -0,0 +1,19 @@
const { embedOptions } = require('../../config');
const { EmbedBuilder } = require('discord.js');
exports.notInVoiceChannel = async (interaction) => {
if (!interaction.member.voice.channel) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nYou need to be in a voice channel to use this command.`
)
.setColor(embedOptions.colors.warning)
]
});
return true;
}
return false;
};