Preconditions Usage

i read docs but i didn't understand some point my file structure is this
13 Replies
fenish
fenish14mo ago
index.d.ts
declare module "@sapphire/framework" {
interface Preconditions {
OnlyMods: never;
}
}

export default undefined;
declare module "@sapphire/framework" {
interface Preconditions {
OnlyMods: never;
}
}

export default undefined;
moderator.ts
import { Precondition } from "@sapphire/framework";
import type {
CommandInteraction,
ContextMenuCommandInteraction,
Guild,
Message,
} from "discord.js";
import config from "../config.json";

export class OnlyModsPrecondition extends Precondition {
public override async messageRun(message: Message) {
if (!message.guild) return this.ok();
return this.checkMod(message.author.id, message.guild);
}

public override async chatInputRun(interaction: CommandInteraction) {
if (!interaction.guild) return this.ok();
return this.checkMod(interaction.user.id, interaction.guild);
}

public override async contextMenuRun(
interaction: ContextMenuCommandInteraction
) {
if (!interaction.guild) return this.ok();
return this.checkMod(interaction.user.id, interaction.guild);
}

private async checkMod(userID: string, guild: Guild) {
const member = await guild.members.cache.get(userID);
return member?.roles.cache.has(config.StaffRoleID)
? this.ok()
: this.error({ message: "Only moderators can use this command!" });
}
}
import { Precondition } from "@sapphire/framework";
import type {
CommandInteraction,
ContextMenuCommandInteraction,
Guild,
Message,
} from "discord.js";
import config from "../config.json";

export class OnlyModsPrecondition extends Precondition {
public override async messageRun(message: Message) {
if (!message.guild) return this.ok();
return this.checkMod(message.author.id, message.guild);
}

public override async chatInputRun(interaction: CommandInteraction) {
if (!interaction.guild) return this.ok();
return this.checkMod(interaction.user.id, interaction.guild);
}

public override async contextMenuRun(
interaction: ContextMenuCommandInteraction
) {
if (!interaction.guild) return this.ok();
return this.checkMod(interaction.user.id, interaction.guild);
}

private async checkMod(userID: string, guild: Guild) {
const member = await guild.members.cache.get(userID);
return member?.roles.cache.has(config.StaffRoleID)
? this.ok()
: this.error({ message: "Only moderators can use this command!" });
}
}
but it doesn't work
fenish
fenish14mo ago
Favna
Favna14mo ago
OnlyMods is not moderator
fenish
fenish14mo ago
oh should i use file name for that ?
Favna
Favna14mo ago
and make it not index.d.ts but just put it just in index.ts well the file name dictates the precondition name at runtime so if you dont match those, TS may accept it but it wont work in runtime
fenish
fenish14mo ago
ahh isee but it doesn't throw error it works but ah wait yea members can't see message is there something to do like error handling
Favna
Favna14mo ago
We have a guide page dedicated to handling precondition denials
fenish
fenish14mo ago
oh oaky
import {
Listener,
type ChatInputCommandDeniedPayload,
Events,
UserError,
} from "@sapphire/framework";

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

if (interaction.deferred || interaction.replied) {
return interaction.editReply({
content: error.message,
});
}

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

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

if (interaction.deferred || interaction.replied) {
return interaction.editReply({
content: error.message,
});
}

return interaction.reply({
content: error.message,
ephemeral: true,
});
}
}
i put this inside of "listeners" folder it doesn't send my error message is it because of my chatinput command or something like this or file structure, etc.
Favna
Favna14mo ago
name of the file? if it's chatInputCommandDenied.ts (case sensitive!!) then it should be fine
fenish
fenish14mo ago
should i name my files with name of classes? or is this special for this one and are there any guide for file naming or directory structure for this kind of files
Favna
Favna14mo ago
the name is always the name of the piece (unless set otherwise) and in the case of listeners the name of the piece is also the name of the event to bind it on (unless set otherwise) for events there is the events enum which is on the sapphire docs
fenish
fenish14mo ago
fenish
fenish14mo ago
like this i'll try it ah yea it works