mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-03 14:38:49 +00:00
feat!: better handling and error handling for interaction events
This commit is contained in:
1 parent
1ec1281e98
commit
f231c53fe8
6 files changed
+270
-361
No files matched your search
@@ -7,15 +7,13 @@ import {
|
||||
ButtonStyle,
|
||||
ComponentType,
|
||||
EmbedBuilder,
|
||||
GuildMember,
|
||||
Interaction,
|
||||
SlashCommandBuilder,
|
||||
StringSelectMenuBuilder,
|
||||
StringSelectMenuOptionBuilder
|
||||
} from 'discord.js';
|
||||
|
||||
import loggerModule from '../../../services/logger';
|
||||
import { CustomError, CustomSlashCommandInteraction } from '../../../types/interactionTypes';
|
||||
import { CustomSlashCommandInteraction } from '../../../types/interactionTypes';
|
||||
import { EmbedOptions, FFmpegFilterOption, FFmpegFilterOptions } from '../../../types/configTypes';
|
||||
import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator';
|
||||
import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
|
||||
@@ -79,7 +77,7 @@ const command: CustomSlashCommandInteraction = {
|
||||
});
|
||||
|
||||
const filterSelect = new StringSelectMenuBuilder()
|
||||
.setCustomId('filters')
|
||||
.setCustomId('filters-select-menu')
|
||||
.setPlaceholder('Select multiple options.')
|
||||
.setMinValues(0)
|
||||
.setMaxValues(filterOptions.length)
|
||||
@@ -92,7 +90,7 @@ const command: CustomSlashCommandInteraction = {
|
||||
};
|
||||
|
||||
const disableButton = new ButtonBuilder()
|
||||
.setCustomId('disable-filters')
|
||||
.setCustomId('filters-disable-button')
|
||||
.setLabel('Disable all filters')
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setEmoji(embedOptions.icons.disable)
|
||||
@@ -104,7 +102,7 @@ const command: CustomSlashCommandInteraction = {
|
||||
};
|
||||
|
||||
logger.debug('Sending info embed with action row components.');
|
||||
const response = await interaction.editReply({
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription('**Toggle filters**\nEnable or disable audio filters for playback from the menu.')
|
||||
@@ -112,130 +110,6 @@ const command: CustomSlashCommandInteraction = {
|
||||
],
|
||||
components: [filterActionRow, disableFiltersActionRow]
|
||||
});
|
||||
|
||||
const collectorFilter = (i: Interaction) => i.user.id === interaction.user.id;
|
||||
try {
|
||||
const confirmation = await response.awaitMessageComponent({
|
||||
filter: collectorFilter,
|
||||
time: 300_000
|
||||
});
|
||||
|
||||
confirmation.deferUpdate();
|
||||
|
||||
logger.debug('Received component interaction response.');
|
||||
|
||||
queue.filters.ffmpeg.setInputArgs([
|
||||
'-threads',
|
||||
ffmpegFilterOptions.threadAmount,
|
||||
'-reconnect',
|
||||
'1',
|
||||
'-reconnect_streamed',
|
||||
'1',
|
||||
'-reconnect_delay_max',
|
||||
'10',
|
||||
'-vn'
|
||||
]);
|
||||
|
||||
// Reset filters before enabling provided filters
|
||||
if (queue.filters.ffmpeg.filters.length > 0) {
|
||||
queue.filters.ffmpeg.setFilters(false);
|
||||
logger.debug('Reset queue filters.');
|
||||
}
|
||||
|
||||
let authorName: string;
|
||||
|
||||
if (interaction.member instanceof GuildMember) {
|
||||
authorName = interaction.member.nickname || interaction.user.username;
|
||||
} else {
|
||||
authorName = interaction.user.username;
|
||||
}
|
||||
|
||||
if ('values' in confirmation) {
|
||||
// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
|
||||
if (
|
||||
(confirmation.values.includes('bassboost_low') || confirmation.values.includes('bassboost')) &&
|
||||
!confirmation.values.includes('normalizer')
|
||||
) {
|
||||
confirmation.values.push('normalizer');
|
||||
}
|
||||
|
||||
// Enable provided filters
|
||||
queue.filters.ffmpeg.toggle(confirmation.values as (keyof QueueFilters)[]);
|
||||
logger.debug(`Enabled filters ${confirmation.values.join(', ')}.`);
|
||||
|
||||
logger.debug('Responding with success embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: authorName,
|
||||
iconURL: interaction.user.avatarURL() || ''
|
||||
})
|
||||
.setDescription(
|
||||
`**${
|
||||
embedOptions.icons.success
|
||||
} Filters toggled**\nNow using these filters:\n${confirmation.values
|
||||
.map((enabledFilter: string) => {
|
||||
const filter = ffmpegFilterOptions.availableFilters.find(
|
||||
(filter) => enabledFilter == filter.value
|
||||
);
|
||||
|
||||
if (!filter) {
|
||||
return enabledFilter;
|
||||
}
|
||||
|
||||
return `- **${filter.emoji} ${filter.label}**`;
|
||||
})
|
||||
.join('\n')}`
|
||||
)
|
||||
.setColor(embedOptions.colors.success)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
} else if (confirmation.customId === 'disable-filters') {
|
||||
logger.debug('Responding with success embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: authorName,
|
||||
iconURL: interaction.user.avatarURL() || ''
|
||||
})
|
||||
.setDescription(
|
||||
`**${embedOptions.icons.success} Disabled filters**\nAll audio filters have been disabled.`
|
||||
)
|
||||
.setColor(embedOptions.colors.success)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
} else {
|
||||
logger.warn('Unhandled component interaction response.');
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof CustomError) {
|
||||
if (error.code === 'InteractionCollectorError') {
|
||||
logger.debug('Interaction response timed out.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (error.message === 'Collector received no interactions before ending with reason: time') {
|
||||
logger.debug('Interaction response timed out.');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.error(error, 'Unhandled error while awaiting or handling component interaction.');
|
||||
return;
|
||||
} else {
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message === 'Collector received no interactions before ending with reason: time'
|
||||
) {
|
||||
logger.debug('Interaction response timed out.');
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import config from 'config';
|
||||
|
||||
import loggerModule from '../../services/logger';
|
||||
import { EmbedOptions } from '../../types/configTypes';
|
||||
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
||||
import { useQueue } from 'discord-player';
|
||||
import { EmbedBuilder, GuildMember } from 'discord.js';
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
|
||||
const component: CustomComponentInteraction = {
|
||||
execute: async ({ interaction, executionId }) => {
|
||||
const logger = loggerModule.child({
|
||||
source: 'filters-disable-button.js',
|
||||
module: 'componentInteraction',
|
||||
name: 'filters-disable-button',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
|
||||
logger.info('Received disable confirmation.');
|
||||
|
||||
const queue = useQueue(interaction.guild!.id)!;
|
||||
|
||||
// Reset filters before enabling provided filters
|
||||
if (queue.filters.ffmpeg.filters.length > 0) {
|
||||
queue.filters.ffmpeg.setFilters(false);
|
||||
logger.debug('Reset queue filters.');
|
||||
}
|
||||
|
||||
let authorName: string;
|
||||
|
||||
if (interaction.member instanceof GuildMember) {
|
||||
authorName = interaction.member.nickname || interaction.user.username;
|
||||
} else {
|
||||
authorName = interaction.user.username;
|
||||
}
|
||||
|
||||
logger.debug('Responding with success embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: authorName,
|
||||
iconURL: interaction.user.avatarURL() || ''
|
||||
})
|
||||
.setDescription(
|
||||
`**${embedOptions.icons.success} Disabled filters**\nAll audio filters have been disabled.`
|
||||
)
|
||||
.setColor(embedOptions.colors.success)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default component;
|
||||
@@ -0,0 +1,100 @@
|
||||
import config from 'config';
|
||||
|
||||
import loggerModule from '../../services/logger';
|
||||
import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes';
|
||||
import { CustomComponentInteraction } from '../../types/interactionTypes';
|
||||
import { QueueFilters, useQueue } from 'discord-player';
|
||||
import { EmbedBuilder, GuildMember, StringSelectMenuInteraction } from 'discord.js';
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');
|
||||
|
||||
const component: CustomComponentInteraction = {
|
||||
execute: async ({ interaction, executionId }) => {
|
||||
const logger = loggerModule.child({
|
||||
source: 'filters-select-menu.js',
|
||||
module: 'componentInteraction',
|
||||
name: 'filters-select-menu',
|
||||
executionId: executionId,
|
||||
shardId: interaction.guild?.shardId,
|
||||
guildId: interaction.guild?.id
|
||||
});
|
||||
|
||||
logger.info('Received select menu confirmation.');
|
||||
|
||||
const queue = useQueue(interaction.guild!.id)!;
|
||||
|
||||
queue.filters.ffmpeg.setInputArgs([
|
||||
'-threads',
|
||||
ffmpegFilterOptions.threadAmount,
|
||||
'-reconnect',
|
||||
'1',
|
||||
'-reconnect_streamed',
|
||||
'1',
|
||||
'-reconnect_delay_max',
|
||||
'10',
|
||||
'-vn'
|
||||
]);
|
||||
|
||||
// Reset filters before enabling provided filters
|
||||
if (queue.filters.ffmpeg.filters.length > 0) {
|
||||
queue.filters.ffmpeg.setFilters(false);
|
||||
logger.debug('Reset queue filters.');
|
||||
}
|
||||
|
||||
if (!(interaction instanceof StringSelectMenuInteraction)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion
|
||||
if (
|
||||
(interaction.values.includes('bassboost_low') || interaction.values.includes('bassboost')) &&
|
||||
!interaction.values.includes('normalizer')
|
||||
) {
|
||||
interaction.values.push('normalizer');
|
||||
}
|
||||
|
||||
// Enable provided filters
|
||||
queue.filters.ffmpeg.toggle(interaction.values as (keyof QueueFilters)[]);
|
||||
logger.debug(`Enabled filters ${interaction.values.join(', ')}.`);
|
||||
|
||||
let authorName: string;
|
||||
|
||||
if (interaction.member instanceof GuildMember) {
|
||||
authorName = interaction.member.nickname || interaction.user.username;
|
||||
} else {
|
||||
authorName = interaction.user.username;
|
||||
}
|
||||
|
||||
logger.debug('Responding with success embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: authorName,
|
||||
iconURL: interaction.user.avatarURL() || ''
|
||||
})
|
||||
.setDescription(
|
||||
`**${
|
||||
embedOptions.icons.success
|
||||
} Filters toggled**\nNow using these filters:\n${interaction.values
|
||||
.map((enabledFilter: string) => {
|
||||
const filter = ffmpegFilterOptions.availableFilters.find(
|
||||
(filter) => enabledFilter == filter.value
|
||||
);
|
||||
|
||||
if (!filter) {
|
||||
return enabledFilter;
|
||||
}
|
||||
|
||||
return `- **${filter.emoji} ${filter.label}**`;
|
||||
})
|
||||
.join('\n')}`
|
||||
)
|
||||
.setColor(embedOptions.colors.success)
|
||||
],
|
||||
components: []
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default component;
|
||||
@@ -8,7 +8,7 @@ import { CustomComponentInteraction } from '../../types/interactionTypes';
|
||||
const embedOptions: EmbedOptions = config.get('embedOptions');
|
||||
|
||||
const component: CustomComponentInteraction = {
|
||||
execute: async ({ interaction, trackId, executionId }) => {
|
||||
execute: async ({ interaction, referenceId, executionId }) => {
|
||||
const logger = loggerModule.child({
|
||||
source: 'nowplaying-skip.js',
|
||||
module: 'componentInteraction',
|
||||
@@ -20,7 +20,7 @@ const component: CustomComponentInteraction = {
|
||||
|
||||
const queue: NodeResolvable = useQueue(interaction.guild!.id)!;
|
||||
|
||||
logger.debug(`Received skip confirmation for track id ${trackId}.`);
|
||||
logger.debug(`Received skip confirmation for track id ${referenceId}.`);
|
||||
if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) {
|
||||
logger.debug('Tried skipping track but there was no queue.');
|
||||
|
||||
@@ -37,7 +37,7 @@ const component: CustomComponentInteraction = {
|
||||
});
|
||||
}
|
||||
|
||||
if (queue.currentTrack!.id !== trackId) {
|
||||
if (queue.currentTrack!.id !== referenceId) {
|
||||
logger.debug('Tried skipping track but it is not the current track and therefore already skipped/removed.');
|
||||
|
||||
logger.debug('Responding with warning embed.');
|
||||
|
||||
Reference in new issue
Block a user