mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-21 21:54:36 +00:00
Exception-proofing external statistics
This commit is contained in:
parent
0eb63c26f5
commit
6efc436717
1 changed files with 16 additions and 10 deletions
|
@ -184,21 +184,27 @@ namespace BirthdayBot
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private async Task SendExternalStatistics(int count, ulong botId, CancellationToken token)
|
private async Task SendExternalStatistics(int count, ulong botId, CancellationToken token)
|
||||||
{
|
{
|
||||||
// TODO protect against exceptions
|
|
||||||
var dbotsToken = Config.DBotsToken;
|
var dbotsToken = Config.DBotsToken;
|
||||||
if (dbotsToken != null)
|
if (dbotsToken != null)
|
||||||
{
|
{
|
||||||
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
try
|
||||||
const string Body = "{{ \"guildCount\": {0} }}";
|
{
|
||||||
var uri = new Uri(string.Format(dBotsApiUrl, botId));
|
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
||||||
|
const string Body = "{{ \"guildCount\": {0} }}";
|
||||||
|
var uri = new Uri(string.Format(dBotsApiUrl, botId));
|
||||||
|
|
||||||
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
||||||
post.Headers.Add("Authorization", dbotsToken);
|
post.Headers.Add("Authorization", dbotsToken);
|
||||||
post.Content = new StringContent(string.Format(Body, count), Encoding.UTF8, "application/json");
|
post.Content = new StringContent(string.Format(Body, count), Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
await Task.Delay(80); // Discord Bots rate limit for this endpoint is 20 per second
|
await Task.Delay(80); // Discord Bots rate limit for this endpoint is 20 per second
|
||||||
await _httpClient.SendAsync(post, token);
|
await _httpClient.SendAsync(post, token);
|
||||||
Log("Discord Bots: Count sent successfully.");
|
Log("Discord Bots: Update successful.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log("Discord Bots: Exception encountered during update: " + ex.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
Loading…
Reference in a new issue