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.

migrating.md 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Migrating from 0.9
  2. **1.0.0 is the biggest breaking change the library has gone through, due to massive
  3. changes in the design of the library.**
  4. >A medium to advanced understanding is recommended when working with this library.
  5. One of the biggest major changes from `0.9.x` is the exclusive use of interfaces.
  6. For the most part, your usability will be very similar to the 0.9 approach of concrete
  7. classes. You **will** be required to cast some entities; this is outlined in a later
  8. section.
  9. It is recommended to familiarize yourself with the entities in 1.0 before continuing.
  10. Feel free to look through the library's source directly, look through IntelliSense, or
  11. look through our hosted [API Documentation](xref:Discord).
  12. ## Entities
  13. Most API models function _similarly_ to 0.9, however their names have been changed.
  14. You should also keep in mind that we now separate different types of Channels and Users.
  15. Take a look at inheritance section of @Terminology for an example of how inheritance and interfaces
  16. work in 1.0
  17. Below is a table that compares most common 0.9 entities to their 1.0 counterparts.
  18. >This should be used mostly for migration purposes. Please take some time to consider whether
  19. >or not you are using the right "tool for the job" when working with 1.0
  20. | 0.9 | 1.0 | Notice |
  21. | --- | --- | ------ |
  22. | Server | @Discord.IGuild |
  23. | Channel | @Discord.IGuildChannel | Applies only to channels that are members of a Guild |
  24. | Channel.IsPrivate | @Discord.IDMChannel
  25. | ChannelType.Text | @Discord.ITextChannel | This applies only to Text Channels in Guilds
  26. | ChannelType.Voice | @Discord.IVoiceChannel | This applies only to Voice Channels in Guilds
  27. | User | @Discord.IGuildUser | This applies only to users belonging to a Guild*
  28. | Profile | @Discord.ISelfUser
  29. | Message | @Discord.IUserMessage
  30. \* To retrieve an @Discord.IGuildUser, you must retrieve the user from an @Discord.IGuild.
  31. [IDiscordClient.GetUserAsync](xref:Discord.IDiscordClient#Discord_IDiscordClient_GetUserAsync_System_UInt64_)
  32. returns a @Discord.IUser, which only contains the information that Discord exposes for public users.
  33. ## Event Registration
  34. Prior to 1.0, events were registered using the standard c# `Handler(EventArgs)` pattern. In 1.0,
  35. events are delegates, but are still registered the same.
  36. For example, let's look at [DiscordSocketClient.MessageReceived](xref:Discord.WebSocket.DiscordSocketClient#Discord_WebSocket_DiscordSocketClient_MessageReceived)
  37. To hook an event into MessageReceived, we now use the following code:
  38. [!code-csharp[Event Registration](guides/samples/migrating/event.cs)]
  39. > **All Event Handlers in 1.0 MUST return Task!**
  40. If your event handler is marked as `async`, it will automatically return `Task`. However,
  41. if you do not need to execute asynchronus code, do _not_ mark your handler as `async`, and instead,
  42. stick a `return Task.CompletedTask` at the bottom.
  43. [!code-csharp[Sync Event Registration](guides/samples/migrating/sync_event.cs)]
  44. **Event handlers no longer require a sender.** The only arguments your event handler needs to accept
  45. are the parameters used by the event. It is recommended to look at the event in IntelliSense or on the
  46. API docs before implementing it.
  47. ## Async
  48. Nearly everything in 1.0 is an async Task. You should always await any tasks you invoke.
  49. However, when using WebSockets, you may find this both inconvienent, and unnecessary, as many of the
  50. WebSocket implementations of the interfaces keep their own local cache of objects, rendering the use
  51. of async redundant.
  52. **As of right now,** there are extension methods you can use, located in `Discord.WebSocket` that will
  53. provide java-esque, synchronus `GetXXX` methods to replace the asynchronus methods on WebSocket entities.
  54. This functionality may be changed at a later date, we are currently reviewing this implementation and
  55. alternative methods.
  56. For your reference, you may want to look through the extension classes located in @Discord.WebSocket