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 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ---
  2. uid: Guides.Commands.PostExecution
  3. title: Post-command Execution Handling
  4. ---
  5. # Preface
  6. When developing a command system or modules, you may want to consider
  7. building a post-execution handling system so you can have a finer
  8. control over commands. Discord.NET offers several different
  9. post-execution workflow for you to work with.
  10. If you recall, in the [Command Guide], we've shown the following
  11. example for executing and handling commands,
  12. [!code[Command Handler](samples/command_handler.cs)]
  13. You may notice that after we perform [ExecuteAsync], we store the
  14. result and print it to the chat. This is essentially the most
  15. basic post-execution handling. With this in mind, we could start doing
  16. things like the following,
  17. [!code[Basic Command Handler](samples/post-execution_basic.cs)]
  18. **But!** This may not always be preferred, because you are
  19. creating your post-execution logic *with* the essential command
  20. handler. This could lead to messy code and has another potential
  21. issue, working with `RunMode.Async`.
  22. If your command is marked with `RunMode.Async`, [ExecuteAsync] will
  23. return a successful [ExecuteResult] instead of whatever results
  24. the actual command may return. Because of the way `RunMode.Async`
  25. [works](xref:FAQ.Commands), handling within the command handler
  26. may not always achieve the desired effect.
  27. ## CommandExecuted Event
  28. Enter [CommandExecuted], an event that was introduced in
  29. Discord.NET 2.0. This event is raised **when the command is
  30. sucessfully executed** and is not prone to `RunMode.Async`'s
  31. [ExecuteAsync] drawbacks.
  32. [CommandExecuted]: xref:Discord.Commands.CommandService.CommandExecuted
  33. [ExecuteAsync]: xref:Discord.Commands.CommandService.ExecuteAsync*
  34. [ExecuteResult]: xref:Discord.Commands.ExecuteResult
  35. [Command Guide]: xref:Guides.Commands.Intro