fix: update validation in interactions

This commit is contained in:
Marius Begby committed 2023-08-29 15:42:29 +02:00
1 parent 37546405e2
commit 5bc0239cdf
17 files changed
+162 -217

No files matched your search

@@ -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',
@@ -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;
+10 -13
View File
@@ -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;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
const filterOptions: StringSelectMenuOptionBuilder[] = [];
+10 -20
View File
@@ -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');
@@ -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.');
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
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)
]
});
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
if (!queue.deleted) {
+9 -9
View File
@@ -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([
+11 -13
View File
@@ -47,21 +47,19 @@ const command: CustomSlashCommandInteraction = {
let geniusSearchQuery = '';
if (!query) {
if (await notInVoiceChannel({ interaction, 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 queueDoesNotExist({ interaction, queue, executionId })) {
return;
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
}
geniusSearchQuery = queue.currentTrack!.title.slice(0, 50);
logger.debug(
+10 -13
View File
@@ -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;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
const sourceStringsFormatted = new Map([
+10 -13
View File
@@ -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;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
const currentTrack = queue.currentTrack!;
+11 -9
View File
@@ -43,20 +43,22 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return;
}
if (await cannotJoinVoiceOrTalk({ interaction, executionId })) {
return;
}
let queue: GuildQueue = useQueue(interaction.guild!.id)!;
if (queue && (await notInSameVoiceChannel({ interaction, queue, executionId }))) {
return;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => cannotJoinVoiceOrTalk({ interaction, executionId })
];
for (const validator of validators) {
if (await validator()) {
return;
}
}
const player = useMainPlayer()!;
const query = interaction.options.getString('query')!;
+11 -44
View File
@@ -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');
@@ -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;
+11 -11
View File
@@ -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';
@@ -35,18 +35,18 @@ const command: CustomSlashCommandInteraction = {
guildId: interaction.guild?.id
});
if (await notInVoiceChannel({ interaction, executionId })) {
return Promise.resolve();
}
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId })
];
if (await queueDoesNotExist({ interaction, queue, executionId })) {
return Promise.resolve();
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return Promise.resolve();
for (const validator of validators) {
if (await validator()) {
return;
}
}
const removeTrackNumber = interaction.options.getNumber('tracknumber')!;
+10 -13
View File
@@ -34,22 +34,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;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
const durationInput = interaction.options.getString('duration');
+10 -13
View File
@@ -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;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueIsEmpty({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueIsEmpty({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
queue.tracks.shuffle();
+10 -13
View File
@@ -31,22 +31,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;
}
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId }),
() => queueNoCurrentTrack({ interaction, queue, executionId })
];
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
}
if (await queueNoCurrentTrack({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
const skipToTrack = interaction.options.getNumber('tracknumber');
+10 -20
View File
@@ -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');
@@ -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 no queue.');
const validators = [
() => notInVoiceChannel({ interaction, executionId }),
() => notInSameVoiceChannel({ interaction, queue, executionId }),
() => queueDoesNotExist({ interaction, queue, executionId })
];
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)
]
});
}
if (await notInSameVoiceChannel({ interaction, queue, executionId })) {
return;
for (const validator of validators) {
if (await validator()) {
return;
}
}
if (!queue.deleted) {
+9 -9
View File
@@ -35,18 +35,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 volume = interaction.options.getNumber('percentage');
+2 -2
View File
@@ -1,5 +1,5 @@
import { GuildQueue, Player } from 'discord-player';
import { ChatInputCommandInteraction } from 'discord.js';
import { ChatInputCommandInteraction, MessageComponentInteraction } from 'discord.js';
import { ExtendedClient } from './clientTypes';
@@ -68,7 +68,7 @@ export interface CannotJoinVoiceOrTalkParams {
}
export interface CannotSendMessageInChannelParams {
interaction: ChatInputCommandInteraction;
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
executionId: string;
}
+13 -2
View File
@@ -1,5 +1,13 @@
import config from 'config';
import { EmbedBuilder, GuildMember, TextChannel, VoiceChannel } from 'discord.js';
import {
ChatInputCommandInteraction,
EmbedBuilder,
GuildMember,
InteractionType,
MessageComponentInteraction,
TextChannel,
VoiceChannel
} from 'discord.js';
import loggerModule from '../../services/logger';
import { EmbedOptions } from '../../types/configTypes';
@@ -50,10 +58,13 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }: C
const channel = interaction.channel;
const interactionIdentifier =
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
// only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable
if (channel instanceof TextChannel && !channel.viewable) {
logger.info(
`User tried to use command '${interaction.commandName}' but the bot had no permission to send reply in text channel.`
`User tried to use interaction '${interactionIdentifier}' but the bot has permission to send reply in text channel.`
);
try {