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.

entities.md 2.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ---
  2. title: Entities
  3. ---
  4. >[!NOTE]
  5. This article is written with the Socket variants of entities in mind,
  6. not the general interfaces or Rest/Rpc entities.
  7. Discord.Net provides a versatile entity system for navigating the
  8. Discord API.
  9. ### Inheritance
  10. Due to the nature of the Discord API, some entities are designed with
  11. multiple variants, for example, `SocketUser` and `SocketGuildUser`.
  12. All models will contain the most detailed version of an entity
  13. possible, even if the type is less detailed.
  14. For example, in the case of the `MessageReceived` event, a
  15. `SocketMessage` is passed in with a channel property of type
  16. `SocketMessageChannel`. All messages come from channels capable of
  17. messaging, so this is the only variant of a channel that can cover
  18. every single case.
  19. But that doesn't mean a message _can't_ come from a
  20. `SocketTextChannel`, which is a message channel in a guild. To
  21. retrieve information about a guild from a message entity, you will
  22. need to cast its channel object to a `SocketTextChannel`.
  23. ### Navigation
  24. All socket entities have navigation properties on them, which allow
  25. you to easily navigate to an entity's parent or children. As explained
  26. above, you will sometimes need to cast to a more detailed version of
  27. an entity to navigate to its parent.
  28. All socket entities have a `Discord` property, which will allow you
  29. to access the parent `DiscordSocketClient`.
  30. ### Accessing Entities
  31. The most basic forms of entities, `SocketGuild`, `SocketUser`, and
  32. `SocketChannel` can be pulled from the DiscordSocketClient's global
  33. cache, and can be retrieved using the respective `GetXXX` method on
  34. DiscordSocketClient.
  35. >[!TIP]
  36. It is **vital** that you use the proper IDs for an entity when using
  37. a GetXXX method. It is recommended that you enable Discord's
  38. _developer mode_ to allow easy access to entity IDs, found in
  39. Settings > Appearance > Advanced
  40. More detailed versions of entities can be pulled from the basic
  41. entities, e.g. `SocketGuild.GetUser`, which returns a
  42. `SocketGuildUser`, or `SocketGuild.GetChannel`, which returns a
  43. `SocketGuildChannel`. Again, you may need to cast these objects to get
  44. a variant of the type that you need.
  45. ### Samples
  46. [!code-csharp[Entity Sample](samples/entities.cs)]
  47. ### Tips
  48. Avoid using boxing-casts to coerce entities into a variant, use the
  49. `as` keyword, and a null-conditional operator.
  50. This allows you to write safer code, and avoid InvalidCastExceptions.
  51. For example, `(message.Author as SocketGuildUser)?.Nickname`.