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

Commit

Permalink
fix: update validation in interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Aug 29, 2023
1 parent 3754640 commit 5bc0239
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 219 deletions.
5 changes: 5 additions & 0 deletions src/handlers/interactionComponentHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MessageComponentInteraction } from 'discord.js';
import loggerModule from '../services/logger';
import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator';

const logger = loggerModule.child({
source: 'interactionComponentHandler.js',
Expand All @@ -16,6 +17,10 @@ export const handleComponent = async (interaction: MessageComponentInteraction,

logger.debug(`Parsed componentId: ${componentId}`);

if (await cannotSendMessageInChannel({ interaction, executionId })) {
return;
}

const componentModule = await import(`../interactions/components/${componentId}.js`);
const { default: component } = componentModule;

Expand Down
23 changes: 10 additions & 13 deletions src/interactions/commands/player/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];

if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}

const filterOptions: StringSelectMenuOptionBuilder[] = [];
Expand Down
30 changes: 10 additions & 20 deletions src/interactions/commands/player/leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import loggerModule from '../../../services/logger';
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
import { EmbedOptions } from '../../../types/configTypes';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../../utils/validation/queueValidator';

const embedOptions: EmbedOptions = config.get('embedOptions');

Expand All @@ -27,29 +28,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (!queue) {
logger.debug('There is already no queue.');

logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\n_Hmm.._ It seems I am not in a voice channel!`
)
.setColor(embedOptions.colors.warning)
]
});
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}

if (!queue.deleted) {
Expand Down
18 changes: 9 additions & 9 deletions src/interactions/commands/player/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}

const loopModesFormatted = new Map([
Expand Down
24 changes: 11 additions & 13 deletions src/interactions/commands/player/lyrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,19 @@ const command: CustomSlashCommandInteraction = {
let geniusSearchQuery = '';

if (!query) {
if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];

for (const validator of validators) {
if (await validator()) {
return;
}
}

if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}
geniusSearchQuery = queue.currentTrack!.title.slice(0, 50);

logger.debug(
Expand Down
25 changes: 11 additions & 14 deletions src/interactions/commands/player/nowplaying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

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

if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];

for (const validator of validators) {
if (await validator()) {
return;
}
}

const sourceStringsFormatted = new Map([
Expand Down
25 changes: 11 additions & 14 deletions src/interactions/commands/player/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return;
}

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

if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];

for (const validator of validators) {
if (await validator()) {
return;
}
}

const currentTrack = queue.currentTrack!;
Expand Down
18 changes: 10 additions & 8 deletions src/interactions/commands/player/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});


if (await notInVoiceChannel({ interaction, executionId })) {
let queue: GuildQueue = useQueue(interaction.guild!.id)!;
if (queue && (await notInSameVoiceChannel({ interaction, queue, executionId }))) {
return;
}

if (await cannotJoinVoiceOrTalk({ interaction, executionId })) {
return;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => cannotJoinVoiceOrTalk({ interaction, executionId })
];

let queue: GuildQueue = useQueue(interaction.guild!.id)!;
if (queue && (await notInSameVoiceChannel({ interaction, queue, executionId }))) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}

const player = useMainPlayer()!;
Expand Down
55 changes: 11 additions & 44 deletions src/interactions/commands/player/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import loggerModule from '../../../services/logger';
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
import { EmbedOptions, PlayerOptions } from '../../../types/configTypes';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { queueDoesNotExist } from '../../../utils/validation/queueValidator';

const embedOptions: EmbedOptions = config.get('embedOptions');
const playerOptions: PlayerOptions = config.get('playerOptions');
Expand All @@ -29,57 +30,23 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return;
}

const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];

for (const validator of validators) {
if (await validator()) {
return;
}
}

const pageIndex = (interaction.options.getNumber('page') || 1) - 1;
let queueString = '';

if (!queue) {
if (pageIndex >= 1) {
logger.debug(`There is no queue and user tried to access page ${pageIndex + 1}.`);

logger.debug('Responding with warning embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nPage **\`${
pageIndex + 1
}\`** is not a valid page number.\n\nThe queue is currently empty, first add some tracks with **\`/play\`**!`
)
.setColor(embedOptions.colors.warning)
]
});
}

logger.debug('There is no queue, displaying empty queue.');
queueString = 'The queue is empty, add some tracks with **`/play`**!';

logger.debug('Responding with info embed.');
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: interaction.guild!.name,
iconURL: interaction.guild!.iconURL() || ''
})
.setDescription(`**${embedOptions.icons.queue} Tracks in queue**\n${queueString}`)
.setColor(embedOptions.colors.info)
.setFooter({
text: 'Page 1 of 1 (0 tracks)'
})
]
});
}

const queueLength = queue.tracks.data.length;
const totalPages = Math.ceil(queueLength / 10) || 1;

Expand Down
22 changes: 11 additions & 11 deletions src/interactions/commands/player/remove.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from 'config';
import { NodeResolvable, useQueue } from 'discord-player';
import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';

import loggerModule from '../../../services/logger';
Expand Down Expand Up @@ -35,18 +35,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});

if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}

const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const queue: GuildQueue = useQueue(interaction.guild!.id)!;

if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId })
];

if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}

const removeTrackNumber = interaction.options.getNumber('tracknumber')!;
Expand Down
Loading

0 comments on commit 5bc0239

Please # to comment.