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.

post-execution.md 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ---
  2. uid: Guides.IntFw.PostExecution
  3. title: Post-Command execution
  4. ---
  5. # Post-Execution Logic
  6. Interaction Service uses [IResult] to provide information about the state of command execution.
  7. These can be used to log internal exceptions or provide some insight to the command user.
  8. If you are running your commands using `RunMode.Sync` these command results can be retrieved from
  9. the return value of [InteractionService.ExecuteCommandAsync] method or by
  10. registering delegates to Interaction Service events.
  11. If you are using the `RunMode.Async` to run your commands,
  12. you must use the Interaction Service events to get the execution results. When using `RunMode.Async`,
  13. [InteractionService.ExecuteCommandAsync] will always return a successful result.
  14. [InteractionService.ExecuteCommandAsync]: xref: Discord.Interactions.InteractionService.ExecuteCommandAsync*
  15. ## Results
  16. Interaction Result come in a handful of different flavours:
  17. 1. [AutocompletionResult]: returned by Autocompleters
  18. 2. [ExecuteResult]: contains the result of method body execution process
  19. 3. [PreconditionGroupResult]: returned by Precondition groups
  20. 4. [PreconditionResult]: returned by preconditions
  21. 5. [RuntimeResult]: a user implementable result for returning user defined results
  22. 6. [SearchResult]: returned by command lookup map
  23. 7. [TypeConverterResult]: returned by TypeConverters
  24. > [!NOTE]
  25. > You can either use the [IResult.Error] property of an Interaction result or create type check for the
  26. > afformentioned result types to branch out your post-execution logic to handle different situations.
  27. [AutocompletionResult]: xref:Discord.AutocompleteResult
  28. [ExecuteResult]: xref:Discord.Interactions.ExecuteResult
  29. [PreconditionGroupResult]: xref:Discord.Interactions.PreconditionGroupResult
  30. [PreconditionResult]: xref:Discord.Interactions.PreconditionResult
  31. [SearchResult]: xref:Discord.Interactions.SearchResult
  32. [TypeConverterResult]: xref:Discord.Interactions.TypeConverterResult
  33. [IResult.Error]: xref:Discord.Interactions.IResult.Error*
  34. ## CommandExecuted Events
  35. Every time a command gets executed, Interaction Service raises a `CommandExecuted` event.
  36. These events can be used to create a post-execution pipeline.
  37. [!code-csharp[Error Review](samples/postexecution/error_review.cs)
  38. ## Log Event
  39. InteractionService regularly outputs information about the occuring events to keep the developer informed.
  40. ## Runtime Result
  41. Interaction commands allow you to return `Task<RuntimeResult>` to pass on additional information about the command execution
  42. process back to your post-execution logic.
  43. Custom [RuntimeResult] classes can be created by inheriting the base [RuntimeResult] class.
  44. If command execution process reaches the method body of the command and no exceptions are thrown during
  45. the execution of the method body, [RuntimeResult] returned by your command will be accessible by casting/type-checking the
  46. [IResult] parameter of the `CommandExecuted` event delegate.
  47. [RuntimeResult]: xref:Discord.Interactions.RuntimeResult
  48. [IResult]: xref:Discord.Interactions.IResult