BirthdayBot/BackgroundServices/GuildStatistics.cs
Noi 75924cc096 Revert GuildStatistics to previous behavior
Sending in reports per shard is currently unnecessary.
Added GuildCache number display for further information. It may be worth
considering removing -that- in the future and load all database
information as needed.
2020-05-22 00:21:40 -07:00

44 lines
1.7 KiB
C#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace BirthdayBot.BackgroundServices
{
class GuildStatistics : BackgroundService
{
private static readonly HttpClient _httpClient = new HttpClient();
public GuildStatistics(BirthdayBot instance) : base(instance) { }
public async override Task OnTick()
{
var count = BotInstance.DiscordClient.Guilds.Count;
var cacheCount = BotInstance.GuildCache.Count;
Log($"Currently in {count} guilds. Cached guild settings: {cacheCount}.");
await SendExternalStatistics(count);
}
/// <summary>
/// Send statistical information to external services.
/// </summary>
async Task SendExternalStatistics(int count)
{
var dbotsToken = BotInstance.Config.DBotsToken;
if (dbotsToken != null)
{
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
const string Body = "{{ \"guildCount\": {0} }}";
var uri = new Uri(string.Format(dBotsApiUrl, BotInstance.DiscordClient.CurrentUser.Id));
var post = new HttpRequestMessage(HttpMethod.Post, uri);
post.Headers.Add("Authorization", dbotsToken);
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 _httpClient.SendAsync(post);
Log("Discord Bots: Count sent successfully.");
}
}
}
}