Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(precondition): use result for PreconditionContainerSingle #535

Merged
merged 3 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/errors/Identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export const enum Identifiers {
PreconditionUserPermissionsNoPermissions = 'preconditionUserPermissionsNoPermissions',
PreconditionThreadOnly = 'preconditionThreadOnly',

PreconditionUnavailable = 'preconditionUnavailable',

PreconditionMissingMessageHandler = 'preconditionMissingMessageHandler',
PreconditionMissingChatInputHandler = 'preconditionMissingChatInputHandler',
PreconditionMissingContextMenuHandler = 'preconditionMissingContextMenuHandler'
Expand Down
45 changes: 30 additions & 15 deletions src/lib/utils/preconditions/PreconditionContainerSingle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { container } from '@sapphire/pieces';
import { Result } from '@sapphire/result';
kyranet marked this conversation as resolved.
Show resolved Hide resolved
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,16 @@ 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 Result.err(
new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` })
);
}

/**
Expand All @@ -97,12 +104,16 @@ 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 Result.err(
new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` })
);
}

/**
Expand All @@ -114,11 +125,15 @@ 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 Result.err(
new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` })
);
}
}