feat: refactored handling of validators

This commit is contained in:
Marius Begby committed 2023-09-05 21:35:18 +02:00
1 parent e67d0004b0
commit 8c05499f08
31 files changed
+316 -338

No files matched your search

@@ -2,12 +2,18 @@ import { GuildQueue, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember } from 'discord.js';
import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes';
import { BaseComponentInteraction } from '../../classes/interactions';
import { queueDoesNotExist } from '../../utils/validation/queueValidator';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
import { checkQueueExists } from '../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
class FiltersDisableButtonComponent extends BaseComponentInteraction {
constructor() {
super('filters-disable-button');
this.validators = [
(args) => checkInVoiceChannel(args),
(args) => checkSameVoiceChannel(args),
(args) => checkQueueExists(args)
];
}
async execute(params: BaseComponentParams): BaseComponentReturnType {
@@ -16,17 +22,7 @@ class FiltersDisableButtonComponent extends BaseComponentInteraction {
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;
}
}
await this.runValidators({ interaction, queue, executionId });
// Reset filters before enabling provided filters
if (queue.filters.ffmpeg.filters.length > 0) {
@@ -4,13 +4,19 @@ import { EmbedBuilder, GuildMember, StringSelectMenuInteraction } from 'discord.
import { FFmpegFilterOption, FFmpegFilterOptions } from '../../types/configTypes';
import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes';
import { BaseComponentInteraction } from '../../classes/interactions';
import { queueDoesNotExist } from '../../utils/validation/queueValidator';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
import { checkQueueExists } from '../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');
class FiltersSelectMenuComponent extends BaseComponentInteraction {
constructor() {
super('filters-select-menu');
this.validators = [
(args) => checkInVoiceChannel(args),
(args) => checkSameVoiceChannel(args),
(args) => checkQueueExists(args)
];
}
async execute(params: BaseComponentParams): BaseComponentReturnType {
@@ -22,17 +28,7 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction {
const selectMenuInteraction: StringSelectMenuInteraction = interaction as StringSelectMenuInteraction;
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;
}
}
await this.runValidators({ interaction, queue, executionId });
queue.filters.ffmpeg.setInputArgs([
'-threads',
@@ -2,12 +2,19 @@ import { GuildQueue, Track, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember } from 'discord.js';
import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes';
import { BaseComponentInteraction } from '../../classes/interactions';
import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator';
import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
import { checkQueueExists, checkQueueCurrentTrack } from '../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../utils/validation/voiceChannelValidator';
class NowplayingSkipButton extends BaseComponentInteraction {
constructor() {
super('nowplaying-skip-button');
this.validators = [
(args) => checkInVoiceChannel(args),
(args) => checkSameVoiceChannel(args),
(args) => checkQueueExists(args),
(args) => checkQueueCurrentTrack(args)
];
}
async execute(params: BaseComponentParams): BaseComponentReturnType {
@@ -16,18 +23,7 @@ class NowplayingSkipButton extends BaseComponentInteraction {
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;
}
}
await this.runValidators({ interaction, queue, executionId });
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
logger.debug('Tried skipping track but there was no queue.');