You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

preconditions.md 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ---
  2. uid: Guides.Commands.Preconditions
  3. title: Preconditions
  4. ---
  5. # Preconditions
  6. Precondition serve as a permissions system for your Commands. Keep in
  7. mind, however, that they are not limited to _just_ permissions and can
  8. be as complex as you want them to be.
  9. There are two types of Preconditions you can use:
  10. * [PreconditionAttribute] can be applied to Modules, Groups, or Commands.
  11. * [ParameterPreconditionAttribute] can be applied to Parameters.
  12. You may visit their respective API documentation to find out more.
  13. [PreconditionAttribute]: xref:Discord.Commands.PreconditionAttribute
  14. [ParameterPreconditionAttribute]: xref:Discord.Commands.ParameterPreconditionAttribute
  15. ## Bundled Preconditions
  16. @Discord.Commands ship with several bundled Preconditions; you may
  17. view their usages on their respective API pages.
  18. * @Discord.Commands.RequireContextAttribute
  19. * @Discord.Commands.RequireOwnerAttribute
  20. * @Discord.Commands.RequireBotPermissionAttribute
  21. * @Discord.Commands.RequireUserPermissionAttribute
  22. * @Discord.Commands.RequireNsfwAttribute
  23. ## Custom Preconditions
  24. To write your own Precondition, create a new class that inherits from
  25. either [PreconditionAttribute] or [ParameterPreconditionAttribute]
  26. depending on your use.
  27. In order for your Precondition to function, you will need to override
  28. the [CheckPermissionsAsync] method.
  29. Your IDE should provide an option to fill this in for you.
  30. If the context meets the required parameters, return
  31. [PreconditionResult.FromSuccess], otherwise return
  32. [PreconditionResult.FromError] and include an error message if
  33. necessary.
  34. [!code-csharp[Custom Precondition](samples/require_owner.cs)]
  35. [CheckPermissionsAsync]: xref:Discord.Commands.PreconditionAttribute.CheckPermissionsAsync*
  36. [PreconditionResult.FromSuccess]: xref:Discord.Commands.PreconditionResult.FromSuccess*
  37. [PreconditionResult.FromError]: xref:Discord.Commands.PreconditionResult.FromError*