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.

RateLimitInfo.cs 1.4 kB

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Discord.Net
  4. {
  5. internal struct RateLimitInfo
  6. {
  7. public bool IsGlobal { get; }
  8. public int? Limit { get; }
  9. public int? Remaining { get; }
  10. public int? RetryAfter { get; }
  11. public DateTimeOffset? Reset { get; }
  12. public TimeSpan? Lag { get; }
  13. internal RateLimitInfo(Dictionary<string, string> headers)
  14. {
  15. IsGlobal = headers.TryGetValue("X-RateLimit-Global", out string temp) &&
  16. bool.TryParse(temp, out var isGlobal) ? isGlobal : false;
  17. Limit = headers.TryGetValue("X-RateLimit-Limit", out temp) &&
  18. int.TryParse(temp, out var limit) ? limit : (int?)null;
  19. Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) &&
  20. int.TryParse(temp, out var remaining) ? remaining : (int?)null;
  21. Reset = headers.TryGetValue("X-RateLimit-Reset", out temp) &&
  22. int.TryParse(temp, out var reset) ? DateTimeUtils.FromUnixSeconds(reset) : (DateTimeOffset?)null;
  23. RetryAfter = headers.TryGetValue("Retry-After", out temp) &&
  24. int.TryParse(temp, out var retryAfter) ? retryAfter : (int?)null;
  25. Lag = headers.TryGetValue("Date", out temp) &&
  26. DateTimeOffset.TryParse(temp, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
  27. }
  28. }
  29. }