request: preconditions
See original GitHub issueIs your feature request related to a problem? Please describe.
We need a built-in way to check whether a command may or may not be run.
Describe the solution you’d like
Since we cannot use Inhibitor
nor its design for copyright reasons, I have thought we could use some inspiration from Discord.NET: Preconditions.
The idea of this is that you do not run all conditions on all commands, instead, you make conditions that check only one thing and stick to it.
For example, a NSFW precondition might have this logic:
public run(message: Message) {
if (!message.channel.nsfw) throw new PreconditionError('You may not run this outside NSFW channels.');
}
The signature is at it follows:
run(message: Message, command: Command): unknown;
Promises will be supported and we may add an option so they’re run either in parallel or sequential.
Any error thrown that is not an instance of PreconditionError will result on preconditionError
to be called and stopping the command from being run.
Defining them in the commands would be similar to this:
{
preconditions: ['nsfw', 'moderator']
}
This system allows us to ditch more complex systems that come with more limitations, included but not limited to: permission levels.
This also simplifies checks, resulting on better fragmentation of the code, more options for the developers (they may add systems such as permission levels or permission nodes), and higher performance overall.
Describe alternatives you’ve considered
Add inhibitors and a copyright banner to comply with MIT license.
Additional context
N/a.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
I’ll probably provide an onAwake method in BasePiece which will be called when the pieces are loaded, as well as defining a base one to check if preconditions exist, unloading and then throwing an error (the loader will emit it to error, reload would bubble up the error to the command).
Reasoning behind the name is because Piece has onLoad and onUnload, which are called by the store whenever they’re loaded or unloaded (also including when overriding).
imo - you should also take note from discord.net’s preconditions and use a
Result
type instead of throwing - exceptions/errors generate a state machine which can harm performance. Instead,Result.fromSuccess/Result.fromError
is a great alternative.