"ChatInputCommandError" listener not firing for subcommands

ChatInputCommandError.ts:
import { ChatInputCommandErrorPayload, Events, Listener } from '@sapphire/framework';
import generalErrorEmbed from '../lib/embed-presets/GeneralError.js';

/**
* Emitted after a chat input command runs unsuccesfully.
*/
export class ChatInputCommandErrorListener extends Listener<typeof Events.ChatInputCommandError> {
public constructor(context: Listener.LoaderContext, options: Listener.Options) {
super(context, {
...options,
event: 'chatInputCommandError'
});
}

/**
* Is called whenever an error is thrown in a `chatInputRun` method.
*
* @param error - The error thrown
* @param payload - The contextual payload
*/
public async run(error: unknown, payload: ChatInputCommandErrorPayload) {
(async () => {
if (!payload.interaction.deferred) await payload.interaction.deferReply({ ephemeral: true });

await payload.interaction.editReply({
embeds: [generalErrorEmbed]
});
})().catch(this.container.logger.error);

this.container.logger.error(`Encountered an error with the "chatInputRun" method of command "${payload.command.name}"`, error);
}
}
import { ChatInputCommandErrorPayload, Events, Listener } from '@sapphire/framework';
import generalErrorEmbed from '../lib/embed-presets/GeneralError.js';

/**
* Emitted after a chat input command runs unsuccesfully.
*/
export class ChatInputCommandErrorListener extends Listener<typeof Events.ChatInputCommandError> {
public constructor(context: Listener.LoaderContext, options: Listener.Options) {
super(context, {
...options,
event: 'chatInputCommandError'
});
}

/**
* Is called whenever an error is thrown in a `chatInputRun` method.
*
* @param error - The error thrown
* @param payload - The contextual payload
*/
public async run(error: unknown, payload: ChatInputCommandErrorPayload) {
(async () => {
if (!payload.interaction.deferred) await payload.interaction.deferReply({ ephemeral: true });

await payload.interaction.editReply({
embeds: [generalErrorEmbed]
});
})().catch(this.container.logger.error);

this.container.logger.error(`Encountered an error with the "chatInputRun" method of command "${payload.command.name}"`, error);
}
}
Solution:
SubcommandPluginEvents
Jump to solution
4 Replies
Teixeira
TeixeiraOP10mo ago
It successfully catches errors from "normal" commands, used this one to test it out:
import { Command } from '@sapphire/framework';

export class TestCommand extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

/**
* Registers this slash command.
*
* @param registry - The {@link Command.Registry} instance associated with this command.
*/
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder
.setName('test') //
.setDescription('TBD')
);
}

/**
* Handler for executions of this command.
*
* @param interaction - The {@link Command.ChatInputCommandInteraction} object received.
*/
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
throw new Error('Test');
}
}
import { Command } from '@sapphire/framework';

export class TestCommand extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

/**
* Registers this slash command.
*
* @param registry - The {@link Command.Registry} instance associated with this command.
*/
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder
.setName('test') //
.setDescription('TBD')
);
}

/**
* Handler for executions of this command.
*
* @param interaction - The {@link Command.ChatInputCommandInteraction} object received.
*/
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
throw new Error('Test');
}
}
The command with a subcommand:
import { Subcommand } from '@sapphire/plugin-subcommands';
import {
ActionRowBuilder,
ChannelType,
ModalActionRowComponentBuilder,
ModalBuilder,
TextInputBuilder,
TextInputStyle,
channelMention,
inlineCode
} from 'discord.js';
import CustomEmbed, { CustomEmbedType } from '../lib/CustomEmbed.js';
import Utils from '../lib/Utils.js';
import { PaginatedMenuResponse } from '../lib/PaginatedMenuResponse.js';

/**
* Adds usable quotes.
*/
export class QuotesSubcommandGroup extends Subcommand {
public constructor(context: Subcommand.LoaderContext, options: Subcommand.Options) {
super(context, {
...options,
name: 'quotes',
subcommands: [
{
name: 'add',
chatInputRun: 'chatInputAdd'
},
]
});
}

public override registerApplicationCommands(registry: Subcommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder
.setName('quotes') //
.setDescription('Manages the list of quotes.')
.setDefaultMemberPermissions('0')
.addSubcommand((subCommand) => {
return subCommand //
.setName('add')
.setDescription('Add quotes to the list.');
})
{
idHints: ['1217906420926644305'],
guildIds: this.container.environment.GUILD_IDS.split(',')
}
);
}

public async chatInputAdd(interaction: Subcommand.ChatInputCommandInteraction) {
throw new Error("Test");
}
}
import { Subcommand } from '@sapphire/plugin-subcommands';
import {
ActionRowBuilder,
ChannelType,
ModalActionRowComponentBuilder,
ModalBuilder,
TextInputBuilder,
TextInputStyle,
channelMention,
inlineCode
} from 'discord.js';
import CustomEmbed, { CustomEmbedType } from '../lib/CustomEmbed.js';
import Utils from '../lib/Utils.js';
import { PaginatedMenuResponse } from '../lib/PaginatedMenuResponse.js';

/**
* Adds usable quotes.
*/
export class QuotesSubcommandGroup extends Subcommand {
public constructor(context: Subcommand.LoaderContext, options: Subcommand.Options) {
super(context, {
...options,
name: 'quotes',
subcommands: [
{
name: 'add',
chatInputRun: 'chatInputAdd'
},
]
});
}

public override registerApplicationCommands(registry: Subcommand.Registry) {
registry.registerChatInputCommand(
(builder) =>
builder
.setName('quotes') //
.setDescription('Manages the list of quotes.')
.setDefaultMemberPermissions('0')
.addSubcommand((subCommand) => {
return subCommand //
.setName('add')
.setDescription('Add quotes to the list.');
})
{
idHints: ['1217906420926644305'],
guildIds: this.container.environment.GUILD_IDS.split(',')
}
);
}

public async chatInputAdd(interaction: Subcommand.ChatInputCommandInteraction) {
throw new Error("Test");
}
}
Oreo ™
Oreo ™10mo ago
There is a separate event for subcommands
Solution
Oreo ™
Oreo ™10mo ago
SubcommandPluginEvents
Teixeira
TeixeiraOP10mo ago
Got it, thank you!
Want results from more Discord servers?
Add your server