no error event on subcommand precondition

what event fires when a Subcommand's precondition (on a subcmd, not the whole class) fails? i'm not getting it to SubcommandError or commandDenied
11 Replies
Oreo ā„¢
Oreo ā„¢ā€¢8mo ago
you dont want to use SubcommandError? i dont think there is any other way
log(n)
log(n)OPā€¢8mo ago
I want to use any event What I'm saying is it isn't firing
Oreo ā„¢
Oreo ā„¢ā€¢8mo ago
oh
log(n)
log(n)OPā€¢8mo ago
If it's ChatInputSubcommandError, that is The precondition returns the error response but then nothing happens
Oreo ā„¢
Oreo ā„¢ā€¢8mo ago
well can u show some code
log(n)
log(n)OPā€¢8mo ago
listener
import { ChatInputCommandDeniedPayload, Listener, UserError } from "@sapphire/framework";
import { ApplyOptions } from "@sapphire/decorators";
import { SubcommandPluginEvents } from "@sapphire/plugin-subcommands";

@ApplyOptions<Listener.Options>({
event: SubcommandPluginEvents.ChatInputSubcommandError,
})
export class ChatInputSubcommandError extends Listener<
typeof SubcommandPluginEvents.ChatInputSubcommandError
> {
public run(error: UserError, { interaction }: ChatInputCommandDeniedPayload) {
if (interaction.deferred || interaction.replied) {
return interaction.editReply({
content: error.message,
});
}

return interaction.reply({
content: error.message,
ephemeral: true,
});
}
}
import { ChatInputCommandDeniedPayload, Listener, UserError } from "@sapphire/framework";
import { ApplyOptions } from "@sapphire/decorators";
import { SubcommandPluginEvents } from "@sapphire/plugin-subcommands";

@ApplyOptions<Listener.Options>({
event: SubcommandPluginEvents.ChatInputSubcommandError,
})
export class ChatInputSubcommandError extends Listener<
typeof SubcommandPluginEvents.ChatInputSubcommandError
> {
public run(error: UserError, { interaction }: ChatInputCommandDeniedPayload) {
if (interaction.deferred || interaction.replied) {
return interaction.editReply({
content: error.message,
});
}

return interaction.reply({
content: error.message,
ephemeral: true,
});
}
}
andi know for a fact the precondition returns an error, i checked that and the command config
@ApplyOptions<Subcommand.Options>({
name: "cc",
subcommands: [
{ name: "info", chatInputRun: "chatInputInfo" },
{ name: "register", chatInputRun: "chatInputRegister", preconditions: ["OwnerOnly"] },
{ name: "unregister", chatInputRun: "chatInputUnregister", preconditions: ["OwnerOnly"] },
{
name: "fix_permissions",
chatInputRun: "chatInputFixPermissions",
preconditions: ["OwnerOnly"],
},
{ name: "archive", chatInputRun: "chatInputArchive", preconditions: ["MWAdminOnly"] },
{ name: "unarchive", chatInputRun: "chatInputUnarchive", preconditions: ["MWAdminOnly"] },
{ name: "transfer", chatInputRun: "chatInputTransfer", preconditions: ["MWAdminOnly"] },
{ name: "create", chatInputRun: "chatInputCreate", preconditions: ["MWAdminOnly"] },
],
})
export class CustomChannelCommand extends Subcommand {
public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
@ApplyOptions<Subcommand.Options>({
name: "cc",
subcommands: [
{ name: "info", chatInputRun: "chatInputInfo" },
{ name: "register", chatInputRun: "chatInputRegister", preconditions: ["OwnerOnly"] },
{ name: "unregister", chatInputRun: "chatInputUnregister", preconditions: ["OwnerOnly"] },
{
name: "fix_permissions",
chatInputRun: "chatInputFixPermissions",
preconditions: ["OwnerOnly"],
},
{ name: "archive", chatInputRun: "chatInputArchive", preconditions: ["MWAdminOnly"] },
{ name: "unarchive", chatInputRun: "chatInputUnarchive", preconditions: ["MWAdminOnly"] },
{ name: "transfer", chatInputRun: "chatInputTransfer", preconditions: ["MWAdminOnly"] },
{ name: "create", chatInputRun: "chatInputCreate", preconditions: ["MWAdminOnly"] },
],
})
export class CustomChannelCommand extends Subcommand {
public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
MWAdminOnly is the one behaving differently though, not OwnerOnly šŸ¤” precondition
import { AllFlowsPrecondition } from "@sapphire/framework";
import { CommandInteraction, ContextMenuCommandInteraction, Message } from "discord.js";
import { env } from "../lib/validatedEnv.js";

export class MWAdminOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) {
return this.checkAdmin(message.author.id);
}

public override async chatInputRun(interaction: CommandInteraction) {
return this.checkAdmin(interaction.user.id);
}

public override async contextMenuRun(interaction: ContextMenuCommandInteraction) {
return this.checkAdmin(interaction.user.id);
}

private async checkAdmin(userId: string) {
const member = await this.container.client.guilds.cache
.get(env.MAIN_GUILD_ID)!
.members.fetch(userId);

return member.roles.cache.has(env.ADMIN_ROLE_ID)
? this.ok()
: this.error({
message: "Only Midnight Wisteria administrators may use this command!",
});
}
}

declare module "@sapphire/framework" {
interface Preconditions {
MWAdminOnly: never;
}
}
import { AllFlowsPrecondition } from "@sapphire/framework";
import { CommandInteraction, ContextMenuCommandInteraction, Message } from "discord.js";
import { env } from "../lib/validatedEnv.js";

export class MWAdminOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) {
return this.checkAdmin(message.author.id);
}

public override async chatInputRun(interaction: CommandInteraction) {
return this.checkAdmin(interaction.user.id);
}

public override async contextMenuRun(interaction: ContextMenuCommandInteraction) {
return this.checkAdmin(interaction.user.id);
}

private async checkAdmin(userId: string) {
const member = await this.container.client.guilds.cache
.get(env.MAIN_GUILD_ID)!
.members.fetch(userId);

return member.roles.cache.has(env.ADMIN_ROLE_ID)
? this.ok()
: this.error({
message: "Only Midnight Wisteria administrators may use this command!",
});
}
}

declare module "@sapphire/framework" {
interface Preconditions {
MWAdminOnly: never;
}
}
OwnerOnly for comparison
import { AllFlowsPrecondition } from "@sapphire/framework";
import { CommandInteraction, ContextMenuCommandInteraction, Message } from "discord.js";
import { env } from "../lib/validatedEnv.js";

export class OwnerOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) {
return this.checkOwner(message.author.id);
}

public override async chatInputRun(interaction: CommandInteraction) {
return this.checkOwner(interaction.user.id);
}

public override async contextMenuRun(interaction: ContextMenuCommandInteraction) {
return this.checkOwner(interaction.user.id);
}

private async checkOwner(userId: string) {
return env.OWNER_ID === userId
? this.ok()
: this.error({ message: "Only the bot owner can use this command!" });
}
}

declare module "@sapphire/framework" {
interface Preconditions {
OwnerOnly: never;
}
}
import { AllFlowsPrecondition } from "@sapphire/framework";
import { CommandInteraction, ContextMenuCommandInteraction, Message } from "discord.js";
import { env } from "../lib/validatedEnv.js";

export class OwnerOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) {
return this.checkOwner(message.author.id);
}

public override async chatInputRun(interaction: CommandInteraction) {
return this.checkOwner(interaction.user.id);
}

public override async contextMenuRun(interaction: ContextMenuCommandInteraction) {
return this.checkOwner(interaction.user.id);
}

private async checkOwner(userId: string) {
return env.OWNER_ID === userId
? this.ok()
: this.error({ message: "Only the bot owner can use this command!" });
}
}

declare module "@sapphire/framework" {
interface Preconditions {
OwnerOnly: never;
}
}
yuansheng
yuanshengā€¢8mo ago
SubcommandPluginEvents.ChatInputSubcommandError does not emit denied precondition, SubcommandPluginEvents.ChatInputSubcommandDenied is what you are looking for
log(n)
log(n)OPā€¢8mo ago
oh that's a mess so -Error emits errors for decorators but -Denied emits errors for preconditions šŸ˜­
Oreo ā„¢
Oreo ā„¢ā€¢8mo ago
no? Error emits Errors and Denied emits Precondition deny payloads makes sense to me bc preconditions deny a command from functioning if it "fails"
Favna
Favnaā€¢8mo ago
Preconditions run before the command, they are PRE conditions. Decorators however with how they're implemented in TypeScript they run when the function runs and call function.apply to run the normal functionality after finishing the decorator but by that point the function itself is already running, it's no longer PRE function.
log(n)
log(n)OPā€¢8mo ago
ah makes sense
Want results from more Discord servers?
Add your server