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.

events.md 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ---
  2. title: Working with Events
  3. ---
  4. Events in Discord.Net are consumed in a similar manner to the standard
  5. convention, with the exception that every event must be of the type
  6. `System.Threading.Tasks.Task`, and instead of using EventArgs, the
  7. event's parameters are passed directly into the handler.
  8. This allows for events to be handled in an async context directly,
  9. instead of relying on async void.
  10. ### Usage
  11. To receive data from an event, hook into it using C#'s delegate
  12. event pattern.
  13. You may opt either to hook an event to an anonymous function (lambda)
  14. or a named function.
  15. ### Safety
  16. All events are designed to be thread-safe, in that events are executed
  17. synchronously off the gateway task, in the same context as the gateway
  18. task.
  19. As a side effect, this makes it possible to deadlock the gateway task,
  20. and kill a connection. As a general rule of thumb, any task that takes
  21. longer than three seconds should **not** be awaited directly in the
  22. context of an event, but should be wrapped in a `Task.Run` or
  23. offloaded to another task.
  24. This also means that you should not await a task that requests data
  25. from Discord's gateway in the same context of an event. Since the
  26. gateway will wait on all invoked event handlers to finish before
  27. processing any additional data from the gateway, this will create
  28. a deadlock that will be impossible to recover from.
  29. Exceptions in commands will be swallowed by the gateway and logged out
  30. through the client's log method.
  31. ### Common Patterns
  32. As you may know, events in Discord.Net are only given a signature of
  33. `Func<T1, ..., Task>`. There is no room for predefined argument names,
  34. so you must either consult IntelliSense, or view the API documentation
  35. directly.
  36. That being said, there are a variety of common patterns that allow you
  37. to infer what the parameters in an event mean.
  38. #### Entity, Entity
  39. An event handler with a signature of `Func<Entity, Entity, Task>`
  40. typically means that the first object will be a clone of the entity
  41. _before_ a change was made, and the latter object will be an attached
  42. model of the entity _after_ the change was made.
  43. This pattern is typically only found on `EntityUpdated` events.
  44. #### Cacheable
  45. An event handler with a signature of `Func<Cacheable, Entity, Task>`
  46. means that the `before` state of the entity was not provided by the
  47. API, so it can either be pulled from the client's cache, or
  48. downloaded from the API.
  49. See the documentation for [Cacheable] for more information on this
  50. object.
  51. [Cacheable]: xref:Discord.Cacheable`2
  52. ### Samples
  53. [!code-csharp[Event Sample](samples/events.cs)]
  54. ### Tips
  55. Many events relating to a Message entity, e.g. `MessageUpdated`
  56. and `ReactionAdded` rely on the client's message cache, which is
  57. **not** enabled by default. Set the `MessageCacheSize` flag in
  58. [DiscordSocketConfig] to enable it.
  59. [DiscordSocketConfig]: xref:Discord.WebSocket.DiscordSocketConfig