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 });}