Creating Custom Restrictions

Package restriction provides some defaults, but you can also create your own. Before you do that there are a few things to keep in mind.

What Errors to Return

The plugin package provides the RestrictionError type. intended to be used by restrictions. As mentioned in Error Types, there are two types of RestrictionErrors, fatal and non-fatal ones.

A fatal RestrictionError should be returned, if the user has no way of influencing the occurrence of the error by themselves. Simply ask yourself, "If this restriction fails, should the help command still list this command for the user?". If so, use a non-fatal RestrictionError.

If an internal error occurs during the execution of a restriction, it of course should be returned as-is.

Performing Time-Consuming Tasks

Restrictions should never perform time-consuming tasks, without ensuring that the results are cached. This is simply because help commands may check the restrictions of every command to determine whether the user may access them.

Therefore, it is recommended to either perform expensive tasks once and store the result using plugin.Context.Set, or access this kind of data through a cache-layer to prevent reoccurring delays.

The getters getting Discord data found in the plugin.Context are cached for the duration of the command's execution regardless of the state's cache settings, and are therefore safe to access.

Last updated

Was this helpful?