mirror of
https://github.com/informaticker/discord-jellyfin-bot.git
synced 2024-11-23 18:21:55 +01:00
🥅 Add error filter for catching command errors
This commit is contained in:
parent
8f8b8a5b9f
commit
1dae9a8644
@ -1,5 +1,7 @@
|
|||||||
|
import { registerFilterGlobally } from '@discord-nestjs/core';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { OnModuleDestroy } from '@nestjs/common/interfaces/hooks';
|
import { OnModuleDestroy } from '@nestjs/common/interfaces/hooks';
|
||||||
|
import { CommandExecutionError } from '../../middleware/command-execution-filter';
|
||||||
import { PlaybackModule } from '../../playback/playback.module';
|
import { PlaybackModule } from '../../playback/playback.module';
|
||||||
import { DiscordConfigService } from './discord.config.service';
|
import { DiscordConfigService } from './discord.config.service';
|
||||||
import { DiscordMessageService } from './discord.message.service';
|
import { DiscordMessageService } from './discord.message.service';
|
||||||
@ -8,7 +10,15 @@ import { DiscordVoiceService } from './discord.voice.service';
|
|||||||
@Module({
|
@Module({
|
||||||
imports: [PlaybackModule],
|
imports: [PlaybackModule],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [DiscordConfigService, DiscordVoiceService, DiscordMessageService],
|
providers: [
|
||||||
|
DiscordConfigService,
|
||||||
|
DiscordVoiceService,
|
||||||
|
DiscordMessageService,
|
||||||
|
{
|
||||||
|
provide: registerFilterGlobally(),
|
||||||
|
useClass: CommandExecutionError,
|
||||||
|
},
|
||||||
|
],
|
||||||
exports: [DiscordConfigService, DiscordVoiceService, DiscordMessageService],
|
exports: [DiscordConfigService, DiscordVoiceService, DiscordMessageService],
|
||||||
})
|
})
|
||||||
export class DiscordClientModule implements OnModuleDestroy {
|
export class DiscordClientModule implements OnModuleDestroy {
|
||||||
|
57
src/middleware/command-execution-filter.ts
Normal file
57
src/middleware/command-execution-filter.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import {
|
||||||
|
Catch,
|
||||||
|
DiscordArgumentMetadata,
|
||||||
|
DiscordExceptionFilter,
|
||||||
|
On,
|
||||||
|
} from '@discord-nestjs/core';
|
||||||
|
import {
|
||||||
|
ActionRowBuilder,
|
||||||
|
ButtonBuilder,
|
||||||
|
ButtonStyle,
|
||||||
|
CommandInteraction,
|
||||||
|
ComponentBuilder,
|
||||||
|
Events,
|
||||||
|
Interaction,
|
||||||
|
} from 'discord.js';
|
||||||
|
import { DiscordMessageService } from '../clients/discord/discord.message.service';
|
||||||
|
import { Constants } from '../utils/constants';
|
||||||
|
|
||||||
|
@Catch(Error)
|
||||||
|
export class CommandExecutionError implements DiscordExceptionFilter {
|
||||||
|
constructor(private readonly discordMessageService: DiscordMessageService) {}
|
||||||
|
|
||||||
|
async catch(
|
||||||
|
exception: Error,
|
||||||
|
metadata: DiscordArgumentMetadata<string, any>,
|
||||||
|
): Promise<void> {
|
||||||
|
console.log(metadata);
|
||||||
|
const interaction: CommandInteraction = metadata.eventArgs[0];
|
||||||
|
|
||||||
|
if (!interaction.isCommand()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(exception);
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setLabel('Report this issue')
|
||||||
|
.setStyle(ButtonStyle.Link)
|
||||||
|
.setURL(
|
||||||
|
Constants.Links.BugReport(
|
||||||
|
`[Bug]: ${exception.name} - ${exception.message}`,
|
||||||
|
).toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
interaction.reply({
|
||||||
|
embeds: [
|
||||||
|
this.discordMessageService.buildErrorMessage({
|
||||||
|
title: 'An unexpected exception occured',
|
||||||
|
description: `Oh no! This isn't supposed to happen. Something did not went right during the execution of your command.\n\nPlease check if there is any update available. If not, please check on ${Constants.Links.Issues} if this problem has already been reported. If not, please report this problem using the button below.\n\n**Debug Information** (please include in your report):\n\`\`\`${exception.stack}\`\`\``,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
components: [row],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,13 @@ export const Constants = {
|
|||||||
},
|
},
|
||||||
Links: {
|
Links: {
|
||||||
SourceCode: 'https://github.com/manuel-rw/jellyfin-discord-music-bot/',
|
SourceCode: 'https://github.com/manuel-rw/jellyfin-discord-music-bot/',
|
||||||
|
Issues: 'https://github.com/manuel-rw/jellyfin-discord-music-bot/issues/',
|
||||||
ReportIssue:
|
ReportIssue:
|
||||||
'https://github.com/manuel-rw/jellyfin-discord-music-bot/issues/new/choose',
|
'https://github.com/manuel-rw/jellyfin-discord-music-bot/issues/new/choose',
|
||||||
|
BugReport: (title) =>
|
||||||
|
new URL(
|
||||||
|
`https://github.com/manuel-rw/jellyfin-discord-music-bot/issues/new?assignees=&labels=&template=bug_report.md&title=${title}`,
|
||||||
|
),
|
||||||
},
|
},
|
||||||
Design: {
|
Design: {
|
||||||
InvisibleSpace: '\u1CBC',
|
InvisibleSpace: '\u1CBC',
|
||||||
|
Loading…
Reference in New Issue
Block a user