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

Commit

Permalink
feat: Added common logic for validating command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Jul 18, 2023
1 parent 780d6f4 commit c7400f9
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 244 deletions.
32 changes: 11 additions & 21 deletions src/commands/player/filters.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = [];
Expand Down
13 changes: 3 additions & 10 deletions src/commands/player/leave.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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);
Expand Down
26 changes: 6 additions & 20 deletions src/commands/player/loop.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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([
Expand Down
38 changes: 8 additions & 30 deletions src/commands/player/nowplaying.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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([
Expand Down
38 changes: 8 additions & 30 deletions src/commands/player/pause.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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 =
Expand Down
13 changes: 3 additions & 10 deletions src/commands/player/play.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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();
Expand Down
15 changes: 4 additions & 11 deletions src/commands/player/queue.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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) {
Expand Down
26 changes: 6 additions & 20 deletions src/commands/player/remove.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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');
Expand Down
38 changes: 8 additions & 30 deletions src/commands/player/seek.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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');
Expand Down
Loading

0 comments on commit c7400f9

Please # to comment.