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.

ratelimits.md 3.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Ratelimits
  2. Ratelimits are a core concept of the discord api, each verified library must follow the ratelimit guidelines. Labs introduces a ratelimit exposure system to help you follow these ratelimits in your own code.
  3. ### Using the ratelimit callback
  4. There is a new property within `RequestOptions` called RatelimitCallback. This callback is called when a request is made via the rest api. The callback is called with a `IRateLimitInfo` parameter:
  5. | Name | Type | Description |
  6. | ---------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
  7. | IsGlobal | bool | Whether or not this ratelimit info is global. |
  8. | Limit | int? | The number of requests that can be made. |
  9. | Remaining | int? | The number of remaining requests that can be made. |
  10. | RetryAfter | int? | The total time (in seconds) of when the current rate limit bucket will reset. Can have decimals to match previous millisecond ratelimit precision. |
  11. | Reset | DateTimeOffset? | The time at which the rate limit resets. |
  12. | ResetAfter | TimeSpan? | The absolute time when this ratelimit resets. |
  13. | Bucket | string | A unique string denoting the rate limit being encountered (non-inclusive of major parameters in the route path). |
  14. | Lag | TimeSpan? | The amount of lag for the request. This is used to denote the precise time of when the ratelimit expires. |
  15. | Endpoint | string | The endpoint that this ratelimit info came from. |
  16. Let's set up a ratelimit callback that will print out the ratelimit info to the console.
  17. ```cs
  18. public async Task MyRatelimitCallback(IRateLimitInfo info)
  19. {
  20. Console.WriteLine($"{info.IsGlobal} {info.Limit} {info.Remaining} {info.RetryAfter} {info.Reset} {info.ResetAfter} {info.Bucket} {info.Lag} {info.Endpoint}");
  21. }
  22. ```
  23. Let's use this callback in a send message function
  24. ```cs
  25. [Command("ping")]
  26. public async Task ping()
  27. {
  28. var options = new RequestOptions()
  29. {
  30. RatelimitCallback = MyRatelimitCallback
  31. };
  32. await Context.Channel.SendMessageAsync("Pong!", options: options);
  33. }
  34. ```
  35. Running this produces the following output:
  36. ```
  37. False 5 4 2021-09-09 3:48:14 AM +00:00 00:00:05 a06de0de4a08126315431cc0c55ee3dc 00:00:00.9891364 channels/848511736872828929/messages
  38. ```