PreMessageCommandRun help

This is my pre event:
import { config } from '@/config.ts';
import { ApplyOptions } from '@sapphire/decorators';
import { Events, Listener, PreMessageCommandRunPayload, UserError } from '@sapphire/framework';

@ApplyOptions<Listener.Options>({
event: Events.PreMessageCommandRun
})
export default class MessageCommandMainGuildHandler extends Listener {
public async run(payload: PreMessageCommandRunPayload) {
if (!payload.message.guildId || payload.message.guildId !== config.mainGuildId)
return this.container.client.emit(
Events.MessageCommandDenied,
{
identifier: 'NotMainGuild',
context: payload.context,
message: 'Ứng dụng chỉ dành cho máy chủ chính.',
name: 'NotMainGuild'
} as UserError,
payload
);
return payload;
}
}
import { config } from '@/config.ts';
import { ApplyOptions } from '@sapphire/decorators';
import { Events, Listener, PreMessageCommandRunPayload, UserError } from '@sapphire/framework';

@ApplyOptions<Listener.Options>({
event: Events.PreMessageCommandRun
})
export default class MessageCommandMainGuildHandler extends Listener {
public async run(payload: PreMessageCommandRunPayload) {
if (!payload.message.guildId || payload.message.guildId !== config.mainGuildId)
return this.container.client.emit(
Events.MessageCommandDenied,
{
identifier: 'NotMainGuild',
context: payload.context,
message: 'Ứng dụng chỉ dành cho máy chủ chính.',
name: 'NotMainGuild'
} as UserError,
payload
);
return payload;
}
}
and the handler:
import { MessageCommandBasedDeniedHandler } from '@/utils/commandHandlers.ts';
import { ApplyOptions } from '@sapphire/decorators';
import { Events, MessageCommandDeniedPayload, Listener, UserError } from '@sapphire/framework';

@ApplyOptions<Listener.Options>({
event: Events.MessageCommandDenied
})
export class chatInputCommandDeniedHandler extends Listener {
public async run(error: UserError, payload: MessageCommandDeniedPayload) {
await MessageCommandBasedDeniedHandler(error, payload);
}
}
async function MessageCommandBasedDeniedHandler(error: ExtendedUserError, payload: MessageCommandDeniedPayload) {
const { message } = payload;
let content: string;
if (error.identifier === Identifiers.PreconditionUserPermissions) {
const missingPermissions = (error.context as any).missing;
content = `Bạn thiếu quyền \`${missingPermissions.join(', ')}\` để sử dụng lệnh này.`;
} else if (error.identifier === Identifiers.PreconditionClientPermissions) {
const missingPermissions = (error.context as any).missing;
content = `Ứng dụng không có quyền \`${missingPermissions.join(', ')}\` để thực hiện lệnh này.`;
} else if (error.identifier === Identifiers.PreconditionRunIn) {
content = `Bạn không thỏa mãn điều kiện \`${error.precondition?.name}\` để sử dụng lệnh này.`;
} else {
content = `Đã xảy ra lỗi khi thực hiện lệnh này.\n\n- Mã lỗi: ${error.identifier}`;
}
return message.reply({
content
});
}
import { MessageCommandBasedDeniedHandler } from '@/utils/commandHandlers.ts';
import { ApplyOptions } from '@sapphire/decorators';
import { Events, MessageCommandDeniedPayload, Listener, UserError } from '@sapphire/framework';

@ApplyOptions<Listener.Options>({
event: Events.MessageCommandDenied
})
export class chatInputCommandDeniedHandler extends Listener {
public async run(error: UserError, payload: MessageCommandDeniedPayload) {
await MessageCommandBasedDeniedHandler(error, payload);
}
}
async function MessageCommandBasedDeniedHandler(error: ExtendedUserError, payload: MessageCommandDeniedPayload) {
const { message } = payload;
let content: string;
if (error.identifier === Identifiers.PreconditionUserPermissions) {
const missingPermissions = (error.context as any).missing;
content = `Bạn thiếu quyền \`${missingPermissions.join(', ')}\` để sử dụng lệnh này.`;
} else if (error.identifier === Identifiers.PreconditionClientPermissions) {
const missingPermissions = (error.context as any).missing;
content = `Ứng dụng không có quyền \`${missingPermissions.join(', ')}\` để thực hiện lệnh này.`;
} else if (error.identifier === Identifiers.PreconditionRunIn) {
content = `Bạn không thỏa mãn điều kiện \`${error.precondition?.name}\` để sử dụng lệnh này.`;
} else {
content = `Đã xảy ra lỗi khi thực hiện lệnh này.\n\n- Mã lỗi: ${error.identifier}`;
}
return message.reply({
content
});
}
But when i ran the command, both executed
No description
Solution:
Sapphire Framework
Global preconditions | Sapphire
Sometimes you want a precondition that automatically runs for every command, without having to add it to each command.
GitHub
xiveventbuddy/src/lib/extensions/XIVEventBuddyComand.ts at main · ...
A user friendly first discord bot to host your XIV events - favware/xiveventbuddy
Jump to solution
7 Replies
ramфs350
ramфs350OP5d ago
@kyra >< @vladdy :<
Favna
Favna5d ago
No idea what gave you the idea to add an event to PreMessageCommandRun but 1. You're supposed to use preconditions https://www.sapphirejs.dev/docs/Guide/preconditions/what-are-preconditions 2. In JavaScript having multiple listeners on the same event will trigger all of them in parallel, not in sequence 3. You're not overwriting the core file because yours doesnt have the same name 4. No idea where you're getting ExtendedUserError from but you're never throwing it so the error will never be that type Also please do not ping people like that. We will get to your questions when we have time. Timezones exist if you didn't know and we cannot be active 24/7 We also have post guidelines that ask you to ping helpers, not individual people: https://i.imgur.com/fIVnJ1p.png
Sapphire Framework
What are preconditions and how do they work? | Sapphire
Preconditions are classes that will determine whether or not a command should be ran according to certain conditions.
Imgur
ramфs350
ramфs350OP4d ago
I'm sorry for all of that btw i want to make a handler for only execute command executed in my server But not using precondition
Favna
Favna4d ago
why not?
ramфs350
ramфs350OP4d ago
I have many commands, and i want to build this as a template
Solution
Favna
Favna4d ago
Sapphire Framework
Global preconditions | Sapphire
Sometimes you want a precondition that automatically runs for every command, without having to add it to each command.
GitHub
xiveventbuddy/src/lib/extensions/XIVEventBuddyComand.ts at main · ...
A user friendly first discord bot to host your XIV events - favware/xiveventbuddy
ramфs350
ramфs350OP4d ago
Ahh, ty

Did you find this page helpful?