🤖
adam
  • Getting started
  • First Steps
    • Creating Your First Command
    • Grouping Commands in Modules
    • Middlewares
    • Argument Parsing
  • Errors
    • Error Handling 101
    • Error Types
    • Changing How Errors Are Handled
  • Localization
    • Getting Started
    • How to Localize
  • Restrictions
    • What Are Restrictions?
    • Creating Custom Restrictions
Powered by GitBook
On this page
  • What Errors to Return
  • Performing Time-Consuming Tasks

Was this helpful?

Export as PDF
  1. Restrictions

Creating Custom Restrictions

PreviousWhat Are Restrictions?

Last updated 3 years ago

Was this helpful?

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

Error Types