How do I handle this UserError?

I've been fiddling around with the subcommands plugin and I see that it throws an error ("No subcommand was matched with the provided arguments.") every time there is not a default defined and it doesnt match with a command. I am not trying to add a default, but I am trying to handle this error by adding a response message, e.g. "Did you mean to execute ...". I have tried using a listener to listen to Events.Error but it never gets fired. Is there any way I can handle this type of error?
2 Replies
big black brown man
Hello, thank you for the reply I have tried listening to MessageSubcommandError, MessageSubcommandNotFound and SubcommandMappingIsMissingMessageCommandHandler. This is the code I am using:
import { ApplyOptions } from "@sapphire/decorators";
import { Listener } from "@sapphire/framework";
import { SubcommandPluginEvents } from "@sapphire/plugin-subcommands";

@ApplyOptions<Listener.Options>({
event: SubcommandPluginEvents.MessageSubcommandNotFound
})
export class UserErrorListener extends Listener {
public run() {
console.log("Hello?")
}
}
import { ApplyOptions } from "@sapphire/decorators";
import { Listener } from "@sapphire/framework";
import { SubcommandPluginEvents } from "@sapphire/plugin-subcommands";

@ApplyOptions<Listener.Options>({
event: SubcommandPluginEvents.MessageSubcommandNotFound
})
export class UserErrorListener extends Listener {
public run() {
console.log("Hello?")
}
}
However, this is not printing to console. Is my listening class correct? After some more searching, the following code works, although very hardcoded and unusual:
@ApplyOptions<Listener.Options>({
event: Events.MessageCommandError,
})
export class CoreEvent extends Listener<typeof Events.MessageCommandError> {
public async run(error: Error, payload: unknown) {
if(!(error instanceof UserError && error.message === "No subcommand was matched with the provided arguments.")) return;
// logic..
}
}
@ApplyOptions<Listener.Options>({
event: Events.MessageCommandError,
})
export class CoreEvent extends Listener<typeof Events.MessageCommandError> {
public async run(error: Error, payload: unknown) {
if(!(error instanceof UserError && error.message === "No subcommand was matched with the provided arguments.")) return;
// logic..
}
}
Want results from more Discord servers?
Add your server