2020-04-02 18:27:55 +00:00
|
|
|
|
using System;
|
2020-05-22 06:53:51 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BirthdayBot.BackgroundServices
|
|
|
|
|
{
|
|
|
|
|
class GuildStatistics : BackgroundService
|
|
|
|
|
{
|
2020-05-22 06:53:51 +00:00
|
|
|
|
private static readonly HttpClient _httpClient = new HttpClient();
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2020-05-22 06:53:51 +00:00
|
|
|
|
public GuildStatistics(BirthdayBot instance) : base(instance) { }
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
|
|
|
|
public async override Task OnTick()
|
|
|
|
|
{
|
2020-05-22 07:21:40 +00:00
|
|
|
|
var count = BotInstance.DiscordClient.Guilds.Count;
|
|
|
|
|
var cacheCount = BotInstance.GuildCache.Count;
|
|
|
|
|
Log($"Currently in {count} guilds. Cached guild settings: {cacheCount}.");
|
|
|
|
|
await SendExternalStatistics(count);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Send statistical information to external services.
|
|
|
|
|
/// </summary>
|
2020-05-22 07:21:40 +00:00
|
|
|
|
async Task SendExternalStatistics(int count)
|
2020-04-02 18:27:55 +00:00
|
|
|
|
{
|
2020-05-22 06:53:51 +00:00
|
|
|
|
var dbotsToken = BotInstance.Config.DBotsToken;
|
|
|
|
|
if (dbotsToken != null)
|
2020-04-02 18:27:55 +00:00
|
|
|
|
{
|
2020-05-22 06:53:51 +00:00
|
|
|
|
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
2020-05-22 07:21:40 +00:00
|
|
|
|
const string Body = "{{ \"guildCount\": {0} }}";
|
2020-05-22 06:53:51 +00:00
|
|
|
|
var uri = new Uri(string.Format(dBotsApiUrl, BotInstance.DiscordClient.CurrentUser.Id));
|
2020-05-22 07:21:40 +00:00
|
|
|
|
|
|
|
|
|
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.");
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|