Global precondition

I'm not able to answer a custom message with global precondition (global.ts) in preconditions folder
//@ts-ignore
import { AllFlowsPrecondition, Piece, Result } from "@sapphire/framework";
import {
GuildMemberRoleManager,
type CacheType,
type ChatInputCommandInteraction,
type ContextMenuCommandInteraction,
} from "discord.js";

export class UserPrecondition extends AllFlowsPrecondition {
public constructor(context: Piece.Context, options: AllFlowsPrecondition.Options) {
super(context, {
...options,
position: 20,
});
}

public override chatInputRun(interaction: ChatInputCommandInteraction) {
return this.chackUserRole(interaction);
}

public override contextMenuRun(interaction: ContextMenuCommandInteraction) {
return this.chackUserRole(interaction);
}

public override messageRun() {
return this.ok();
}

private async checkUserRole(
interaction: ChatInputCommandInteraction<CacheType> | ContextMenuCommandInteraction<CacheType>
) {
const roles = (interaction.member?.roles as GuildMemberRoleManager).cache;
// Role "Member"
if (roles.some((role: { id: string }) => role.id === "1165714233938427944")) {
return this.ok();
} else {
return this.error({ message: 'You must register on the website first!' });
}
}
}
//@ts-ignore
import { AllFlowsPrecondition, Piece, Result } from "@sapphire/framework";
import {
GuildMemberRoleManager,
type CacheType,
type ChatInputCommandInteraction,
type ContextMenuCommandInteraction,
} from "discord.js";

export class UserPrecondition extends AllFlowsPrecondition {
public constructor(context: Piece.Context, options: AllFlowsPrecondition.Options) {
super(context, {
...options,
position: 20,
});
}

public override chatInputRun(interaction: ChatInputCommandInteraction) {
return this.chackUserRole(interaction);
}

public override contextMenuRun(interaction: ContextMenuCommandInteraction) {
return this.chackUserRole(interaction);
}

public override messageRun() {
return this.ok();
}

private async checkUserRole(
interaction: ChatInputCommandInteraction<CacheType> | ContextMenuCommandInteraction<CacheType>
) {
const roles = (interaction.member?.roles as GuildMemberRoleManager).cache;
// Role "Member"
if (roles.some((role: { id: string }) => role.id === "1165714233938427944")) {
return this.ok();
} else {
return this.error({ message: 'You must register on the website first!' });
}
}
}
Solution:
it is chatInputCommandDenied and contextMenuCommandDenied not commandDenied since v3 of the framework: https://www.sapphirejs.dev/docs/Guide/getting-started/updating-from-v2-to-v3#sapphire-events-added
Sapphire Framework
Updating from v2 to v3 | Sapphire
A general tip concerning code duplication
Jump to solution
8 Replies
Mateleo
MateleoOP14mo ago
import { Events, Listener, type ChatInputCommandDeniedPayload, type UserError } from "@sapphire/framework";

export class ChatInputCommandDenied extends Listener<typeof Events.ChatInputCommandDenied> {
public run(error: UserError, { interaction }: ChatInputCommandDeniedPayload) {
const isSilent = Reflect.get(Object(error.context), "silent");

if (interaction.deferred || interaction.replied) {
return interaction.editReply({
content: isSilent ? "\u200b" : error.message,
});
}

return interaction.reply({
content: isSilent ? "\u200b" : error.message,
ephemeral: true,
});
}
}
import { Events, Listener, type ChatInputCommandDeniedPayload, type UserError } from "@sapphire/framework";

export class ChatInputCommandDenied extends Listener<typeof Events.ChatInputCommandDenied> {
public run(error: UserError, { interaction }: ChatInputCommandDeniedPayload) {
const isSilent = Reflect.get(Object(error.context), "silent");

if (interaction.deferred || interaction.replied) {
return interaction.editReply({
content: isSilent ? "\u200b" : error.message,
});
}

return interaction.reply({
content: isSilent ? "\u200b" : error.message,
ephemeral: true,
});
}
}
The error is "The application did not respond" I'm pretty sure it's because I'm not able to link those two files
Favna
Favna14mo ago
- name of the file of the listener? - are you using subcommands?
Mateleo
MateleoOP14mo ago
No subs commands, and the name is commandDenied.ts
No description
Mateleo
MateleoOP14mo ago
My goal is to reject every slashcommands based on a single role check The main issue is that no reply is send to the client, and "commandDenied" doesn't seems to be used Any idea?
Solution
Favna
Favna14mo ago
it is chatInputCommandDenied and contextMenuCommandDenied not commandDenied since v3 of the framework: https://www.sapphirejs.dev/docs/Guide/getting-started/updating-from-v2-to-v3#sapphire-events-added
Sapphire Framework
Updating from v2 to v3 | Sapphire
A general tip concerning code duplication
Mateleo
MateleoOP14mo ago
Il will check that
Mateleo
MateleoOP14mo ago
Perfect, renaming my listener resolved the issue
No description
Mateleo
MateleoOP14mo ago
Thank you very much 🙏
Want results from more Discord servers?
Add your server