Skip to content

Commit

Permalink
fix(precondition): use result for PreconditionContainerSingle (#535)
Browse files Browse the repository at this point in the history
* fix(precondition): use result for PreconditionContainerSingle

* fix: remove unnecessary async

* fix: import err directly
  • Loading branch information
feefs committed Oct 1, 2022
1 parent ae89de2 commit 05167f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/lib/errors/Identifiers.ts
Expand Up @@ -60,6 +60,8 @@ export const enum Identifiers {
PreconditionUserPermissionsNoPermissions = 'preconditionUserPermissionsNoPermissions',
PreconditionThreadOnly = 'preconditionThreadOnly',

PreconditionUnavailable = 'preconditionUnavailable',

PreconditionMissingMessageHandler = 'preconditionMissingMessageHandler',
PreconditionMissingChatInputHandler = 'preconditionMissingChatInputHandler',
PreconditionMissingContextMenuHandler = 'preconditionMissingContextMenuHandler'
Expand Down
39 changes: 24 additions & 15 deletions src/lib/utils/preconditions/PreconditionContainerSingle.ts
@@ -1,5 +1,8 @@
import { container } from '@sapphire/pieces';
import { err } from '@sapphire/result';
import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js';
import { Identifiers } from '../../errors/Identifiers';
import { UserError } from '../../errors/UserError';
import type { ChatInputCommand, ContextMenuCommand, MessageCommand } from '../../structures/Command';
import type { PreconditionContext, PreconditionKeys, Preconditions, SimplePreconditionKeys } from '../../structures/Precondition';
import type { IPreconditionContainer } from './IPreconditionContainer';
Expand Down Expand Up @@ -80,12 +83,14 @@ export class PreconditionContainerSingle implements IPreconditionContainer {
public messageRun(message: Message, command: MessageCommand, context: PreconditionContext = {}) {
const precondition = container.stores.get('preconditions').get(this.name);
if (precondition) {
if (precondition.messageRun) return precondition.messageRun(message, command, { ...context, ...this.context });
throw new Error(
`The precondition "${precondition.name}" is missing a "messageRun" handler, but it was requested for the "${command.name}" command.`
);
return precondition.messageRun
? precondition.messageRun(message, command, { ...context, ...this.context })
: precondition.error({
identifier: Identifiers.PreconditionMissingMessageHandler,
message: `The precondition "${precondition.name}" is missing a "messageRun" handler, but it was requested for the "${command.name}" command.`
});
}
throw new Error(`The precondition "${this.name}" is not available.`);
return err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` }));
}

/**
Expand All @@ -97,12 +102,14 @@ export class PreconditionContainerSingle implements IPreconditionContainer {
public chatInputRun(interaction: CommandInteraction, command: ChatInputCommand, context: PreconditionContext = {}) {
const precondition = container.stores.get('preconditions').get(this.name);
if (precondition) {
if (precondition.chatInputRun) return precondition.chatInputRun(interaction, command, { ...context, ...this.context });
throw new Error(
`The precondition "${precondition.name}" is missing a "chatInputRun" handler, but it was requested for the "${command.name}" command.`
);
return precondition.chatInputRun
? precondition.chatInputRun(interaction, command, { ...context, ...this.context })
: precondition.error({
identifier: Identifiers.PreconditionMissingChatInputHandler,
message: `The precondition "${precondition.name}" is missing a "chatInputRun" handler, but it was requested for the "${command.name}" command.`
});
}
throw new Error(`The precondition "${this.name}" is not available.`);
return err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` }));
}

/**
Expand All @@ -114,11 +121,13 @@ export class PreconditionContainerSingle implements IPreconditionContainer {
public contextMenuRun(interaction: ContextMenuInteraction, command: ContextMenuCommand, context: PreconditionContext = {}) {
const precondition = container.stores.get('preconditions').get(this.name);
if (precondition) {
if (precondition.contextMenuRun) return precondition.contextMenuRun(interaction, command, { ...context, ...this.context });
throw new Error(
`The precondition "${precondition.name}" is missing a "contextMenuRun" handler, but it was requested for the "${command.name}" command.`
);
return precondition.contextMenuRun
? precondition.contextMenuRun(interaction, command, { ...context, ...this.context })
: precondition.error({
identifier: Identifiers.PreconditionMissingContextMenuHandler,
message: `The precondition "${precondition.name}" is missing a "contextMenuRun" handler, but it was requested for the "${command.name}" command.`
});
}
throw new Error(`The precondition "${this.name}" is not available.`);
return err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` }));
}
}

0 comments on commit 05167f4

Please sign in to comment.