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

feat: add ExtendedPrecondition #320

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './lib/structures/ArgumentStore';
export * from './lib/structures/Command';
export * from './lib/structures/CommandStore';
export * from './lib/structures/ExtendedArgument';
export * from './lib/structures/ExtendedPrecondition';
export * from './lib/structures/Listener';
export * from './lib/structures/ListenerStore';
export * from './lib/structures/Precondition';
Expand Down
92 changes: 92 additions & 0 deletions src/lib/structures/ExtendedPrecondition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { PieceContext } from '@sapphire/pieces';
import type { Message } from 'discord.js';
import type { Command } from './Command';
import { Precondition, PreconditionKeys } from './Precondition';
import { isOk } from '../parsers/Result';

/**
* The extended precondition class. This class is abstract and is to be extended by subclasses which
* will implement the {@link ExtendedPrecondition#handle} method.
* Much like the {@link Precondition} class, this class handles blocking messages coming into the command handler.
* However, this class can be used to expand upon an existing
* precondition to perform operations that rely on a different check passing.
*
* @example
* ```typescript
* // TypeScript:
* import { ApplyOptions } from '@sapphire/decorators';
* import { ExtendedPrecondition, ExtendedPreconditionOptions } from '@sapphire/framework';
* import type { Message } from 'discord.js';
*
* // Just like with `Precondition`, you can use `export default` or `export =` too.
* (at)ApplyOptions<ExtendedPreconditionOptions>({
* name: 'ModOnly',
* baseArgument: 'GuildOnly'
* })
* export class UserPrecondition extends ExtendedPrecondition<'GuildOnly'> {
* public handle(message: Message) {
* // You now know that `message.member` exists, because the `GuildOnly` precondition was run before this.
* const isMod = message.member!.roles.cache.find((role) => role.name === 'Mod');
* return isMod
* ? this.ok()
* : this.error({ identifier: 'preconditionModOnly', message: 'Only moderators can run this command.' });
* }
* }
* ```
*
* @example
* ```javascript
* // JavaScript:
* const { ExtendedPrecondition } = require('@sapphire/framework');
*
* module.exports = class UserPrecondition extends ExtendedPrecondition {
* constructor(context) {
* super(context, { name: 'ModOnly', baseArgument: 'GuildOnly' });
* }
*
* handle(message) {
* // You now know that `message.member` exists, because the `GuildOnly` precondition was run before this.
* const isMod = message.member!.roles.cache.find((role) => role.name === 'Mod');
* return isMod
* ? this.ok()
* : this.error({ identifier: 'preconditionModOnly', message: 'Only moderators can run this command.' });
* }
* }
* ```
*/
export abstract class ExtendedPrecondition<
K extends PreconditionKeys,
O extends ExtendedPreconditionOptions<K> = ExtendedPreconditionOptions<K>
> extends Precondition<O> {
public basePrecondition: K;

public constructor(context: PieceContext, options: O) {
super(context, options);
this.basePrecondition = options.basePrecondition;
}

/**
* Represents the underlying precondition that is depended on
*/
public get base(): Precondition {
return this.container.stores.get('preconditions').get(this.basePrecondition)!;
}

public async run(message: Message, command: Command, context: Precondition.Context): Precondition.AsyncResult {
const result = await this.base.run(message, command, context);
// If the result was successful (i.e. is of type `Ok<unknown>`), pass its
// value to [[ExtendedPrecondition#handle]] for further parsing. Otherwise, return
// the error as is; it'll provide contextual information from the base precondition.
return isOk(result) ? this.handle(message, command, context) : result;
}

public abstract handle(message: Message, command: Command, context: Precondition.Context): Precondition.Result;
}

export interface ExtendedPreconditionOptions<K extends PreconditionKeys> extends Precondition.Options {
/**
* The name of the underlying precondition that is depended on.
* See {@link PreconditionKeys} for valid keys.
*/
basePrecondition: K;
}