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

8 years ago
8 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. ## 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. ## Using Preconditions
  24. To use a precondition, simply apply any valid precondition candidate to
  25. a command method signature as an attribute.
  26. ### Example - Using a Precondition
  27. [!code-csharp[Precondition usage](samples/preconditions/precondition_usage.cs)]
  28. ## ORing Preconditions
  29. When writing commands, you may want to allow some of them to be
  30. executed when only some of the precondition checks are passed.
  31. This is where the [Group] property of a precondition attribute comes in
  32. handy. By assigning two or more preconditions to a group, the command
  33. system will allow the command to be executed when one of the
  34. precondition passes.
  35. ### Example - ORing Preconditions
  36. [!code-csharp[OR Precondition](samples/preconditions/group_precondition.cs)]
  37. [Group]: xref:Discord.Commands.PreconditionAttribute.Group
  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*