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 2.0 kB

7 years ago
7 years ago
7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ---
  2. uid: Guides.Commands.Preconditions
  3. title: Preconditions
  4. ---
  5. # Preconditions
  6. Preconditions 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 ships with several bundled Preconditions for you
  17. to use.
  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. If the context meets the required parameters, return
  30. [PreconditionResult.FromSuccess], otherwise return
  31. [PreconditionResult.FromError] and include an error message if
  32. necessary.
  33. > [!NOTE]
  34. > Visual Studio can help you implement missing members
  35. > from the abstract class by using the "Implement Abstract Class"
  36. > IntelliSense hint.
  37. ### Example - Creating a Custom Precondition
  38. [!code-csharp[Custom Precondition](samples/require_owner.cs)]
  39. [CheckPermissionsAsync]: xref:Discord.Commands.PreconditionAttribute.CheckPermissionsAsync*
  40. [PreconditionResult.FromSuccess]: xref:Discord.Commands.PreconditionResult.FromSuccess*
  41. [PreconditionResult.FromError]: xref:Discord.Commands.PreconditionResult.FromError*