Pre messsage command run help

Hello, I wanted to run permission check before executing the command, returning the error message if failed otherwise continue with coommand. I can't get it to work and I can't understand why
13 Replies
čamdžić
čamdžićOP5mo ago
import { ApplyOptions } from '@sapphire/decorators';
import {
type Events,
Listener,
type PreMessageCommandRunPayload
} from '@sapphire/framework';
import { BotEmbed } from '../lib/builders/BotEmbed';

@ApplyOptions<Listener.Options>({
name: 'CorePreMessageCommandRun',
event: 'preMessageCommandRun'
})
export class BotListener extends Listener<typeof Events.PreMessageCommandRun> {
override run(payload: PreMessageCommandRunPayload) {
const { message, command } = payload;

const permissionLevelCheck = this.container.permissionLevel.check(
message.member,
command.permissionLevel
);

if (!permissionLevelCheck.hasPermission) {
const requiredRolesText =
permissionLevelCheck.missingRoleIds.length === 1
? 'Potrebna uloga'
: 'Potrebne uloge';

return message.reply({
embeds: [
BotEmbed.error(

)
]
});
}
}
}
import { ApplyOptions } from '@sapphire/decorators';
import {
type Events,
Listener,
type PreMessageCommandRunPayload
} from '@sapphire/framework';
import { BotEmbed } from '../lib/builders/BotEmbed';

@ApplyOptions<Listener.Options>({
name: 'CorePreMessageCommandRun',
event: 'preMessageCommandRun'
})
export class BotListener extends Listener<typeof Events.PreMessageCommandRun> {
override run(payload: PreMessageCommandRunPayload) {
const { message, command } = payload;

const permissionLevelCheck = this.container.permissionLevel.check(
message.member,
command.permissionLevel
);

if (!permissionLevelCheck.hasPermission) {
const requiredRolesText =
permissionLevelCheck.missingRoleIds.length === 1
? 'Potrebna uloga'
: 'Potrebne uloge';

return message.reply({
embeds: [
BotEmbed.error(

)
]
});
}
}
}
Here's my current code and file name is preMessageCommandRun
const client = new SapphireClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
],
loadApplicationCommandRegistriesStatusListeners: false,
loadMessageCommandListeners: true,
caseInsensitiveCommands: true,
disableMentionPrefix: true,
defaultPrefix: container.config.prefix,
cronTasks: {
defaultTimezone: 'Europe/Sarajevo'
}
});
const client = new SapphireClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
],
loadApplicationCommandRegistriesStatusListeners: false,
loadMessageCommandListeners: true,
caseInsensitiveCommands: true,
disableMentionPrefix: true,
defaultPrefix: container.config.prefix,
cronTasks: {
defaultTimezone: 'Europe/Sarajevo'
}
});
Client options ^^ This makes preconditions go away and also if the member has permission, real command logic never happens
Favna
Favna5mo ago
Why are you overwriting the core event and not using global preconditions https://sapphirejs.dev/docs/Guide/preconditions/global-preconditions ?
Sapphire Framework
Global preconditions | Sapphire
Sometimes you want a precondition that automatically runs for every command, without having to add it to each command.
čamdžić
čamdžićOP5mo ago
Sorry😭 Thanks They are executed in every command, right? I don’t have to specify them
Favna
Favna5mo ago
yes
čamdžić
čamdžićOP5mo ago
i don't really understand position property I did this:
@ApplyOptions<Precondition.Options>({
position: 20
})
export class BotPrecondition extends Precondition {
}
@ApplyOptions<Precondition.Options>({
position: 20
})
export class BotPrecondition extends Precondition {
}
and it works okay, but is 20 normal or what shoild I put
Favna
Favna5mo ago
it's abitrary if you only have 1 I think enabled is 0 so just anything above 0 can be steps of 10s, 1s, 100s, 5s, whatever
čamdžić
čamdžićOP5mo ago
is that working with subcommands? cuz it wont work for me so idk did i made a mistake or what happened
Favna
Favna5mo ago
The parent command should get blocked I think
čamdžić
čamdžićOP5mo ago
@Favna can I do something like this but for interaction handlers? I want a function to be called before interaction handler is
Favna
Favna5mo ago
no for that you will have to overwrite the core listener
čamdžić
čamdžićOP5mo ago
Which core listener There is no like PreInteractionHandlerRun Or I can’t see it
Favna
Favna5mo ago
But uhm Depending on what you want to do It might be better to just call a function in the parse method Or extend the interactionhandler class and do something that way (the latter opens many doors)

Did you find this page helpful?