2022-03-21 19:11:30 +00:00
|
|
|
|
using System.Text;
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
namespace BirthdayBot.BackgroundServices;
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reports user count statistics to external services on a shard by shard basis.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class ExternalStatisticsReporting : BackgroundService {
|
|
|
|
|
const int ProcessInterval = 1200 / ShardBackgroundWorker.Interval; // Process every ~20 minutes
|
2022-03-21 19:11:30 +00:00
|
|
|
|
const int ProcessOffset = 300 / ShardBackgroundWorker.Interval; // Begin processing ~5 minutes after shard start
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
private static readonly HttpClient _httpClient = new();
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
public ExternalStatisticsReporting(ShardInstance instance) : base(instance) { }
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
public override async Task OnTick(int tickCount, CancellationToken token) {
|
|
|
|
|
if (tickCount < ProcessOffset) return;
|
|
|
|
|
if (tickCount % ProcessInterval != 0) return;
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
var botId = ShardInstance.DiscordClient.CurrentUser.Id;
|
|
|
|
|
if (botId == 0) return;
|
|
|
|
|
var count = ShardInstance.DiscordClient.Guilds.Count;
|
|
|
|
|
|
|
|
|
|
var dbotsToken = ShardInstance.Config.DBotsToken;
|
|
|
|
|
if (dbotsToken != null) await SendDiscordBots(dbotsToken, count, botId, token);
|
|
|
|
|
}
|
2021-05-30 19:00:49 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
private async Task SendDiscordBots(string apiToken, int userCount, ulong botId, CancellationToken token) {
|
|
|
|
|
try {
|
|
|
|
|
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
|
|
|
|
const string Body = "{{ \"guildCount\": {0}, \"shardCount\": {1}, \"shardId\": {2} }}";
|
|
|
|
|
var uri = new Uri(string.Format(dBotsApiUrl, botId));
|
|
|
|
|
|
|
|
|
|
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
|
|
|
|
post.Headers.Add("Authorization", apiToken);
|
|
|
|
|
post.Content = new StringContent(string.Format(Body,
|
|
|
|
|
userCount, ShardInstance.Config.ShardTotal, ShardInstance.ShardId),
|
|
|
|
|
Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
await _httpClient.SendAsync(post, token);
|
|
|
|
|
Log("Discord Bots: Update successful.");
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Log("Discord Bots: Exception encountered during update: " + ex.Message);
|
2021-05-30 19:00:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|