mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: add validation to component interactions
This commit is contained in:
1 parent
5bc0239cdf
commit
7053f01bc5
8 files changed
+89
-36
No files matched your search
@@ -3,8 +3,10 @@ import config from 'config';
|
|||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions } from '../../types/configTypes';
|
import { EmbedOptions } from '../../types/configTypes';
|
||||||
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
||||||
import { useQueue } from 'discord-player';
|
import { GuildQueue, useQueue } from 'discord-player';
|
||||||
import { EmbedBuilder, GuildMember } from 'discord.js';
|
import { EmbedBuilder, GuildMember } from 'discord.js';
|
||||||
|
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
|
||||||
|
import { queueDoesNotExist } from '../../utils/validation/queueValidator';
|
||||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||||
|
|
||||||
const component: CustomComponentInteraction = {
|
const component: CustomComponentInteraction = {
|
||||||
@@ -20,7 +22,19 @@ const component: CustomComponentInteraction = {
|
|||||||
|
|
||||||
logger.debug('Received disable confirmation.');
|
logger.debug('Received disable confirmation.');
|
||||||
|
|
||||||
const queue = useQueue(interaction.guild!.id)!;
|
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
|
||||||
|
|
||||||
|
const validators = [
|
||||||
|
() => notInVoiceChannel({ interaction, executionId }),
|
||||||
|
() => notInSameVoiceChannel({ interaction, queue, executionId }),
|
||||||
|
() => queueDoesNotExist({ interaction, queue, executionId })
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const validator of validators) {
|
||||||
|
if (await validator()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Reset filters before enabling provided filters
|
// Reset filters before enabling provided filters
|
||||||
if (queue.filters.ffmpeg.filters.length > 0) {
|
if (queue.filters.ffmpeg.filters.length > 0) {
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ import config from 'config';
|
|||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes';
|
import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes';
|
||||||
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
||||||
import { QueueFilters, useQueue } from 'discord-player';
|
import { GuildQueue, QueueFilters, useQueue } from 'discord-player';
|
||||||
import { EmbedBuilder, GuildMember, StringSelectMenuInteraction } from 'discord.js';
|
import { EmbedBuilder, GuildMember, StringSelectMenuInteraction } from 'discord.js';
|
||||||
|
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
|
||||||
|
import { queueDoesNotExist } from '../../utils/validation/queueValidator';
|
||||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||||
const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');
|
const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');
|
||||||
|
|
||||||
@@ -22,7 +24,19 @@ const component: CustomComponentInteraction = {
|
|||||||
logger.debug('Received select menu confirmation.');
|
logger.debug('Received select menu confirmation.');
|
||||||
|
|
||||||
const selectMenuInteraction = interaction as StringSelectMenuInteraction;
|
const selectMenuInteraction = interaction as StringSelectMenuInteraction;
|
||||||
const queue = useQueue(selectMenuInteraction.guild!.id)!;
|
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
|
||||||
|
|
||||||
|
const validators = [
|
||||||
|
() => notInVoiceChannel({ interaction, executionId }),
|
||||||
|
() => notInSameVoiceChannel({ interaction, queue, executionId }),
|
||||||
|
() => queueDoesNotExist({ interaction, queue, executionId })
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const validator of validators) {
|
||||||
|
if (await validator()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
queue.filters.ffmpeg.setInputArgs([
|
queue.filters.ffmpeg.setInputArgs([
|
||||||
'-threads',
|
'-threads',
|
||||||
@@ -42,10 +56,10 @@ const component: CustomComponentInteraction = {
|
|||||||
logger.debug('Reset queue filters.');
|
logger.debug('Reset queue filters.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
|
// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
|
||||||
if (
|
if (
|
||||||
(selectMenuInteraction.values.includes('bassboost_low') || selectMenuInteraction.values.includes('bassboost')) &&
|
(selectMenuInteraction.values.includes('bassboost_low') ||
|
||||||
|
selectMenuInteraction.values.includes('bassboost')) &&
|
||||||
!selectMenuInteraction.values.includes('normalizer')
|
!selectMenuInteraction.values.includes('normalizer')
|
||||||
) {
|
) {
|
||||||
selectMenuInteraction.values.push('normalizer');
|
selectMenuInteraction.values.push('normalizer');
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import { EmbedBuilder, GuildMember } from 'discord.js';
|
|||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions } from '../../types/configTypes';
|
import { EmbedOptions } from '../../types/configTypes';
|
||||||
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
||||||
|
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
|
||||||
|
import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator';
|
||||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||||
|
|
||||||
const component: CustomComponentInteraction = {
|
const component: CustomComponentInteraction = {
|
||||||
@@ -22,6 +24,19 @@ const component: CustomComponentInteraction = {
|
|||||||
|
|
||||||
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
|
const queue: GuildQueue = useQueue(interaction.guild!.id)!;
|
||||||
|
|
||||||
|
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 (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
|
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
|
||||||
logger.debug('Tried skipping track but there was no queue.');
|
logger.debug('Tried skipping track but there was no queue.');
|
||||||
|
|
||||||
|
|||||||
@@ -24,18 +24,18 @@ export interface CustomEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface NotInVoiceChannelParams {
|
export interface NotInVoiceChannelParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NotInSameVoiceChannelParams {
|
export interface NotInSameVoiceChannelParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
queue: GuildQueue;
|
queue: GuildQueue;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NotValidGuildIdParams {
|
export interface NotValidGuildIdParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,25 +45,25 @@ export interface TransformQueryParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface QueueDoesNotExistParams {
|
export interface QueueDoesNotExistParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
queue: GuildQueue;
|
queue: GuildQueue;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueueNoCurrentTrackParams {
|
export interface QueueNoCurrentTrackParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
queue: GuildQueue;
|
queue: GuildQueue;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueueIsEmptyParams {
|
export interface QueueIsEmptyParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
queue: GuildQueue;
|
queue: GuildQueue;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CannotJoinVoiceOrTalkParams {
|
export interface CannotJoinVoiceOrTalkParams {
|
||||||
interaction: ChatInputCommandInteraction;
|
interaction: ChatInputCommandInteraction | MessageComponentInteraction;
|
||||||
executionId: string;
|
executionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import {
|
import { EmbedBuilder, GuildMember, InteractionType, TextChannel, VoiceChannel } from 'discord.js';
|
||||||
ChatInputCommandInteraction,
|
|
||||||
EmbedBuilder,
|
|
||||||
GuildMember,
|
|
||||||
InteractionType,
|
|
||||||
MessageComponentInteraction,
|
|
||||||
TextChannel,
|
|
||||||
VoiceChannel
|
|
||||||
} from 'discord.js';
|
|
||||||
|
|
||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions } from '../../types/configTypes';
|
import { EmbedOptions } from '../../types/configTypes';
|
||||||
@@ -25,6 +17,8 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Cannot
|
|||||||
});
|
});
|
||||||
|
|
||||||
const channel = interaction.member instanceof GuildMember ? interaction.member.voice.channel : null;
|
const channel = interaction.member instanceof GuildMember ? interaction.member.voice.channel : null;
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (channel instanceof VoiceChannel && (!channel.joinable || !channel.speakable)) {
|
if (channel instanceof VoiceChannel && (!channel.joinable || !channel.speakable)) {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
@@ -38,7 +32,7 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: Cannot
|
|||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`User tried to use command '${interaction.commandName}' but the bot had no permission to join/speak in the voice channel.`
|
`User tried to use command '${interactionIdentifier}' but the bot had no permission to join/speak in the voice channel.`
|
||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import { EmbedBuilder } from 'discord.js';
|
import { EmbedBuilder, InteractionType } from 'discord.js';
|
||||||
|
|
||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions } from '../../types/configTypes';
|
import { EmbedOptions } from '../../types/configTypes';
|
||||||
import {
|
import { QueueDoesNotExistParams, QueueIsEmptyParams, QueueNoCurrentTrackParams } from '../../types/utilTypes';
|
||||||
QueueDoesNotExistParams, QueueIsEmptyParams, QueueNoCurrentTrackParams
|
|
||||||
} from '../../types/utilTypes';
|
|
||||||
|
|
||||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||||
export const queueDoesNotExist = async ({ interaction, queue, executionId }: QueueDoesNotExistParams) => {
|
export const queueDoesNotExist = async ({ interaction, queue, executionId }: QueueDoesNotExistParams) => {
|
||||||
@@ -18,6 +16,9 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }: Que
|
|||||||
guildId: interaction.guild?.id
|
guildId: interaction.guild?.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (!queue) {
|
if (!queue) {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
@@ -29,7 +30,7 @@ export const queueDoesNotExist = async ({ interaction, queue, executionId }: Que
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(`User tried to use command '${interaction.commandName}' but there was no queue.`);
|
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no queue.`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +47,9 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: Q
|
|||||||
guildId: interaction.guild?.id
|
guildId: interaction.guild?.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (!queue.currentTrack) {
|
if (!queue.currentTrack) {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
@@ -57,7 +61,7 @@ export const queueNoCurrentTrack = async ({ interaction, queue, executionId }: Q
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(`User tried to use command '${interaction.commandName}' but there was no current track.`);
|
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no current track.`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +78,9 @@ export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsE
|
|||||||
guildId: interaction.guild?.id
|
guildId: interaction.guild?.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (queue.tracks.data.length === 0) {
|
if (queue.tracks.data.length === 0) {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
@@ -85,7 +92,7 @@ export const queueIsEmpty = async ({ interaction, queue, executionId }: QueueIsE
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(`User tried to use command '${interaction.commandName}' but there was no tracks in the queue.`);
|
logger.debug(`User tried to use command '${interactionIdentifier}' but there was no tracks in the queue.`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import { EmbedBuilder } from 'discord.js';
|
import { EmbedBuilder, InteractionType } from 'discord.js';
|
||||||
|
|
||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions, SystemOptions } from '../../types/configTypes';
|
import { EmbedOptions, SystemOptions } from '../../types/configTypes';
|
||||||
@@ -17,19 +17,22 @@ export const notValidGuildId = async ({ interaction, executionId }: NotValidGuil
|
|||||||
guildId: interaction.guild?.id
|
guildId: interaction.guild?.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (interaction.guildId && !systemOptions.systemGuildIds.includes(interaction.guildId)) {
|
if (interaction.guildId && !systemOptions.systemGuildIds.includes(interaction.guildId)) {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
.setDescription(
|
.setDescription(
|
||||||
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command **\`/${interaction.commandName}\`** cannot be executed in this server.`
|
`**${embedOptions.icons.warning} Oops!**\nNo permission to execute this command.\n\nThe command **\`/${interactionIdentifier}\`** cannot be executed in this server.`
|
||||||
)
|
)
|
||||||
.setColor(embedOptions.colors.warning)
|
.setColor(embedOptions.colors.warning)
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`User tried to use command '${interaction.commandName}' but system command cannot be executed in the specified guild.`
|
`User tried to use command '${interactionIdentifier}' but system command cannot be executed in the specified guild.`
|
||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import config from 'config';
|
import config from 'config';
|
||||||
import { EmbedBuilder, GuildMember } from 'discord.js';
|
import { EmbedBuilder, GuildMember, InteractionType } from 'discord.js';
|
||||||
|
|
||||||
import loggerModule from '../../services/logger';
|
import loggerModule from '../../services/logger';
|
||||||
import { EmbedOptions } from '../../types/configTypes';
|
import { EmbedOptions } from '../../types/configTypes';
|
||||||
@@ -16,6 +16,9 @@ export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoice
|
|||||||
guildId: interaction.guild?.id
|
guildId: interaction.guild?.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (interaction.member instanceof GuildMember && !interaction.member.voice.channel) {
|
if (interaction.member instanceof GuildMember && !interaction.member.voice.channel) {
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
@@ -27,7 +30,7 @@ export const notInVoiceChannel = async ({ interaction, executionId }: NotInVoice
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(`User tried to use command '${interaction.commandName}' but was not in a voice channel.`);
|
logger.debug(`User tried to use command '${interactionIdentifier}' but was not in a voice channel.`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +47,9 @@ export const notInSameVoiceChannel = async ({ interaction, queue, executionId }:
|
|||||||
guildId: interaction.guild?.id
|
guildId: interaction.guild?.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const interactionIdentifier =
|
||||||
|
interaction.type === InteractionType.ApplicationCommand ? interaction.commandName : interaction.customId;
|
||||||
|
|
||||||
if (!queue || !queue.dispatcher) {
|
if (!queue || !queue.dispatcher) {
|
||||||
// If there is no queue or bot is not in voice channel, then there is no need to check if user is in same voice channel.
|
// If there is no queue or bot is not in voice channel, then there is no need to check if user is in same voice channel.
|
||||||
return false;
|
return false;
|
||||||
@@ -63,7 +69,7 @@ export const notInSameVoiceChannel = async ({ interaction, queue, executionId }:
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug(`User tried to use command '${interaction.commandName}' but was not in the same voice channel.`);
|
logger.debug(`User tried to use command '${interactionIdentifier}' but was not in the same voice channel.`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user