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.

client-basics.md 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ---
  2. uid: FAQ.Basics.ClientBasics
  3. title: Basic Questions about Client
  4. ---
  5. # Client Basics Questions
  6. In the following section, you will find commonly asked questions and
  7. answers about common issues that you may face when utilizing the
  8. various clients offered by the library.
  9. ## My client keeps returning 401 upon logging in!
  10. > [!WARNING]
  11. > Userbot/selfbot (logging in with a user token) is no
  12. > longer supported with this library starting from 2.0, as
  13. > logging in under a user account may result in account termination.
  14. >
  15. > For more information, see issue [827] & [958], as well as the official
  16. > [Discord API Terms of Service].
  17. There are few possible reasons why this may occur.
  18. 1. You are not using the appropriate [TokenType]. If you are using a
  19. bot account created from the Discord Developer portal, you should
  20. be using `TokenType.Bot`.
  21. 2. You are not using the correct login credentials. Please keep in
  22. mind that a token is **different** from a *client secret*.
  23. [TokenType]: xref:Discord.TokenType
  24. [827]: https://github.com/discord-net/Discord.Net/issues/827
  25. [958]: https://github.com/discord-net/Discord.Net/issues/958
  26. [Discord API Terms of Service]: https://discord.com/developers/docs/legal
  27. ## How do I do X, Y, Z when my bot connects/logs on? Why do I get a `NullReferenceException` upon calling any client methods after connect?
  28. Your bot should **not** attempt to interact in any way with
  29. guilds/servers until the [Ready] event fires. When the bot
  30. connects, it first has to download guild information from
  31. Discord for you to get access to any server
  32. information; the client is not ready at this point.
  33. Technically, the [GuildAvailable] event fires once the data for a
  34. particular guild has downloaded; however, it is best to wait for all
  35. guilds to be downloaded. Once all downloads are complete, the [Ready]
  36. event is triggered, then you can proceed to do whatever you like.
  37. [Ready]: xref:Discord.WebSocket.DiscordSocketClient.Ready
  38. [GuildAvailable]: xref:Discord.WebSocket.BaseSocketClient.GuildAvailable
  39. ## How do I get a message's previous content when that message is edited?
  40. If you need to do anything with messages (e.g., checking Reactions,
  41. checking the content of edited/deleted messages), you must set the
  42. [MessageCacheSize] in your [DiscordSocketConfig] settings in order to
  43. use the cached message entity. Read more about it [here](xref:Guides.Concepts.Events#cacheable).
  44. 1. Message Cache must be enabled.
  45. 2. Hook the MessageUpdated event. This event provides a *before* and
  46. *after* object.
  47. 3. Only messages received *after* the bot comes online will be
  48. available in the cache.
  49. [MessageCacheSize]: xref:Discord.WebSocket.DiscordSocketConfig.MessageCacheSize
  50. [DiscordSocketConfig]: xref:Discord.WebSocket.DiscordSocketConfig
  51. [MessageUpdated]: xref:Discord.WebSocket.BaseSocketClient.MessageUpdated
  52. ## What is a shard/sharded client, and how is it different from the `DiscordSocketClient`?
  53. As your bot grows in popularity, it is recommended that you should section your bot off into separate processes.
  54. The [DiscordShardedClient] is essentially a class that allows you to easily create and manage multiple [DiscordSocketClient]
  55. instances, with each one serving a different amount of guilds.
  56. There are very few differences from the [DiscordSocketClient] class, and it is very straightforward
  57. to modify your existing code to use a [DiscordShardedClient] when necessary.
  58. 1. You can specify the total amount of shards, or shard ids, via [DiscordShardedClient]'s constructors.
  59. If the total shards are not specified then the library will get the recommended shard count via the
  60. [Get Gateway Bot](https://discord.com/developers/docs/topics/gateway#get-gateway-bot) route.
  61. 2. The [Connected], [Disconnected], [Ready], and [LatencyUpdated] events
  62. are replaced with [ShardConnected], [ShardDisconnected], [ShardReady], and [ShardLatencyUpdated].
  63. 3. Every event handler you apply/remove to the [DiscordShardedClient] is applied/removed to each shard.
  64. If you wish to control a specific shard's events, you can access an individual shard through the `Shards` property.
  65. If you do not wish to use the [DiscordShardedClient] and instead reuse the same [DiscordSocketClient] code and manually shard them,
  66. you can do so by specifying the [ShardId] for the [DiscordSocketConfig] and pass that to the [DiscordSocketClient]'s constructor.
  67. [DiscordSocketClient]: xref:Discord.WebSocket.DiscordSocketClient
  68. [DiscordShardedClient]: xref:Discord.WebSocket.DiscordShardedClient
  69. [Connected]: xref:Discord.WebSocket.DiscordSocketClient.Connected
  70. [Disconnected]: xref:Discord.WebSocket.DiscordSocketClient.Disconnected
  71. [LatencyUpdated]: xref:Discord.WebSocket.DiscordSocketClient.LatencyUpdated
  72. [ShardConnected]: xref:Discord.WebSocket.DiscordShardedClient.ShardConnected
  73. [ShardDisconnected]: xref:Discord.WebSocket.DiscordShardedClient.ShardDisconnected
  74. [ShardReady]: xref:Discord.WebSocket.DiscordShardedClient.ShardReady
  75. [ShardLatencyUpdated]: xref:Discord.WebSocket.DiscordShardedClient.ShardLatencyUpdated
  76. [ShardId]: xref:Discord.WebSocket.DiscordSocketConfig.ShardId