Fix crosspost ratelimits (#2100)
This commit is contained in:
21
src/Discord.Net.Rest/API/Common/Ratelimit.cs
Normal file
21
src/Discord.Net.Rest/API/Common/Ratelimit.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Discord.API
|
||||||
|
{
|
||||||
|
internal class Ratelimit
|
||||||
|
{
|
||||||
|
[JsonProperty("global")]
|
||||||
|
public bool Global { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("message")]
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("retry_after")]
|
||||||
|
public double RetryAfter { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -149,7 +149,7 @@ namespace Discord.Net.Rest
|
|||||||
HttpResponseMessage response = await _client.SendAsync(request, cancelToken).ConfigureAwait(false);
|
HttpResponseMessage response = await _client.SendAsync(request, cancelToken).ConfigureAwait(false);
|
||||||
|
|
||||||
var headers = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
|
var headers = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
|
||||||
var stream = !headerOnly ? await response.Content.ReadAsStreamAsync().ConfigureAwait(false) : null;
|
var stream = (!headerOnly || !response.IsSuccessStatusCode) ? await response.Content.ReadAsStreamAsync().ConfigureAwait(false) : null;
|
||||||
|
|
||||||
return new RestResponse(response.StatusCode, headers, stream);
|
return new RestResponse(response.StatusCode, headers, stream);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ namespace Discord.Net.Queue
|
|||||||
switch (response.StatusCode)
|
switch (response.StatusCode)
|
||||||
{
|
{
|
||||||
case (HttpStatusCode)429:
|
case (HttpStatusCode)429:
|
||||||
|
info.ReadRatelimitPayload(response.Stream);
|
||||||
if (info.IsGlobal)
|
if (info.IsGlobal)
|
||||||
{
|
{
|
||||||
#if DEBUG_LIMITS
|
#if DEBUG_LIMITS
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
|
using Discord.API;
|
||||||
|
using Discord.Net.Rest;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Discord.Net
|
namespace Discord.Net
|
||||||
{
|
{
|
||||||
@@ -25,7 +29,7 @@ namespace Discord.Net
|
|||||||
public DateTimeOffset? Reset { get; }
|
public DateTimeOffset? Reset { get; }
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public TimeSpan? ResetAfter { get; }
|
public TimeSpan? ResetAfter { get; private set; }
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public string Bucket { get; }
|
public string Bucket { get; }
|
||||||
@@ -56,5 +60,23 @@ namespace Discord.Net
|
|||||||
Lag = headers.TryGetValue("Date", out temp) &&
|
Lag = headers.TryGetValue("Date", out temp) &&
|
||||||
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
|
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void ReadRatelimitPayload(Stream response)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (response != null && response.Length != 0)
|
||||||
|
{
|
||||||
|
using (TextReader text = new StreamReader(response))
|
||||||
|
using (JsonReader reader = new JsonTextReader(text))
|
||||||
|
{
|
||||||
|
var ratelimit = Discord.Rest.DiscordRestClient.Serializer.Deserialize<Ratelimit>(reader);
|
||||||
|
|
||||||
|
ResetAfter = TimeSpan.FromSeconds(ratelimit.RetryAfter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user