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.8 kB

7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ## Using Preconditions
  16. To use a precondition, simply apply any valid precondition candidate to
  17. a command method signature as an attribute.
  18. ### Example - Using a Precondition
  19. [!code-csharp[Precondition usage](samples/preconditions/precondition_usage.cs)]
  20. ### ORing Preconditions
  21. When writing commands, you may want to allow some of them to be
  22. executed when only some of the precondition checks are passed.
  23. This is where the [Group] property of a precondition attribute comes in
  24. handy. By assigning two or more preconditions to a group, the command
  25. system will allow the command to be executed when one of the
  26. precondition passes.
  27. #### Example - ORing Preconditions
  28. [!code-csharp[OR Precondition](samples/preconditions/group_precondition.cs)]
  29. [Group]: xref:Discord.Commands.PreconditionAttribute.Group
  30. ## Bundled Preconditions
  31. @Discord.Commands ships with several bundled Preconditions for you
  32. to use.
  33. * @Discord.Commands.RequireContextAttribute
  34. * @Discord.Commands.RequireOwnerAttribute
  35. * @Discord.Commands.RequireBotPermissionAttribute
  36. * @Discord.Commands.RequireUserPermissionAttribute
  37. * @Discord.Commands.RequireNsfwAttribute
  38. ## Custom Preconditions
  39. To write your own Precondition, create a new class that inherits from
  40. either [PreconditionAttribute] or [ParameterPreconditionAttribute]
  41. depending on your use.
  42. In order for your Precondition to function, you will need to override
  43. the [CheckPermissionsAsync] method.
  44. If the context meets the required parameters, return
  45. [PreconditionResult.FromSuccess], otherwise return
  46. [PreconditionResult.FromError] and include an error message if
  47. necessary.
  48. > [!NOTE]
  49. > Visual Studio can help you implement missing members
  50. > from the abstract class by using the "Implement Abstract Class"
  51. > IntelliSense hint.
  52. ### Example - Creating a Custom Precondition
  53. [!code-csharp[Custom Precondition](samples/preconditions/require_owner.cs)]
  54. [CheckPermissionsAsync]: xref:Discord.Commands.PreconditionAttribute.CheckPermissionsAsync*
  55. [PreconditionResult.FromSuccess]: xref:Discord.Commands.PreconditionResult.FromSuccess*
  56. [PreconditionResult.FromError]: xref:Discord.Commands.PreconditionResult.FromError*