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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. It is recommended to familiarize yourself with the entities in 1.0 before continuing.
  6. Feel free to look through the library's source directly, look through IntelliSense, or
  7. look through our hosted [API Documentation](xref:Discord).
  8. ## Entities
  9. Most API models function _similarly_ to 0.9, however their names have been changed.
  10. You should also keep in mind that we now separate different types of Channels and Users.
  11. Before proceeding, please read over @Terminology to understand the naming behind some objects.
  12. Below is a table that compares most common 0.9 entities to their 1.0 counterparts.
  13. >This should be used mostly for migration purposes. Please take some time to consider whether
  14. >or not you are using the right "tool for the job" when working with 1.0
  15. | 0.9 | 1.0 | Notice |
  16. | --- | --- | ------ |
  17. | Server | @Discord.WebSocket.SocketGuild |
  18. | Channel | @Discord.WebSocket.SocketGuildChannel | Applies only to channels that are members of a Guild |
  19. | Channel.IsPrivate | @Discord.WebSocket.SocketDMChannel
  20. | ChannelType.Text | @Discord.WebSocket.SocketTextChannel | This applies only to Text Channels in Guilds
  21. | ChannelType.Voice | @Discord.WebSocket.SocketVoiceChannel | This applies only to Voice Channels in Guilds
  22. | User | @Discord.WebSocket.SocketGuildUser | This applies only to users belonging to a Guild*
  23. | Profile | @Discord.WebSocket.SocketGuildUser
  24. | Message | @Discord.WebSocket.SocketUserMessage
  25. \* To retrieve an @Discord.WebSocket.SocketGuildUser, you must retrieve the user from an @Discord.WebSocket.SocketGuild.
  26. ## Event Registration
  27. Prior to 1.0, events were registered using the standard c# `Handler(EventArgs)` pattern. In 1.0,
  28. events are delegates, but are still registered the same.
  29. For example, let's look at [DiscordSocketClient.MessageReceived](xref:Discord.WebSocket.DiscordSocketClient#Discord_WebSocket_DiscordSocketClient_MessageReceived)
  30. To hook an event into MessageReceived, we now use the following code:
  31. [!code-csharp[Event Registration](guides/samples/migrating/event.cs)]
  32. > **All Event Handlers in 1.0 MUST return Task!**
  33. If your event handler is marked as `async`, it will automatically return `Task`. However,
  34. if you do not need to execute asynchronus code, do _not_ mark your handler as `async`, and instead,
  35. stick a `return Task.CompletedTask` at the bottom.
  36. [!code-csharp[Sync Event Registration](guides/samples/migrating/sync_event.cs)]
  37. **Event handlers no longer require a sender.** The only arguments your event handler needs to accept
  38. are the parameters used by the event. It is recommended to look at the event in IntelliSense or on the
  39. API docs before implementing it.
  40. ## Async
  41. Nearly everything in 1.0 is an async Task. You should always await any tasks you invoke.