question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

request: preconditions

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kyranetcommented, Aug 23, 2020

A minor concern: what happens if a precondition is specified which doesn’t exist? E.g., I write “bot_owner” instead of “botOwner”.

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).

1reaction
Tylertron1998commented, Aug 23, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request preconditions | Cloud Storage
This page discusses request preconditions, which you use to prevent requests from applying to a resource when the resource is in an unexpected...
Read more >
HTTP conditional requests - MDN Web Docs - Mozilla
HTTP conditional requests are requests that are executed differently, depending on the value of specific headers. These headers define a ...
Read more >
Evaluating request preconditions using Request objects
The Request object provides methods for evaluating preconditions and for selecting the optimal response variant based on the request headers. 이 태스크 정보....
Read more >
Request (Java(TM) EE 7 Specification APIs)
Evaluate request preconditions for a resource that does not currently exist. ... Evaluate request preconditions based on the passed in value.
Read more >
Preconditions
Preconditions allow controlling policy rule execution by building ... requests based on resource and user information, preconditions can be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found