mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 01:14:12 +00:00
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.
This commit is contained in:
parent
2a15fc21c8
commit
75924cc096
2 changed files with 15 additions and 60 deletions
|
@ -1,6 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -15,75 +13,31 @@ namespace BirthdayBot.BackgroundServices
|
||||||
|
|
||||||
public async override Task OnTick()
|
public async override Task OnTick()
|
||||||
{
|
{
|
||||||
var counts = GetGuildCounts();
|
var count = BotInstance.DiscordClient.Guilds.Count;
|
||||||
|
var cacheCount = BotInstance.GuildCache.Count;
|
||||||
// Build this report
|
Log($"Currently in {count} guilds. Cached guild settings: {cacheCount}.");
|
||||||
int goodCount = 0;
|
await SendExternalStatistics(count);
|
||||||
int badCount = 0;
|
|
||||||
int goodShards = 0;
|
|
||||||
int badShards = 0;
|
|
||||||
foreach (var status in counts)
|
|
||||||
{
|
|
||||||
if (status.Item2.ShardConnected)
|
|
||||||
{
|
|
||||||
goodShards++;
|
|
||||||
goodCount += status.Item2.GuildCount;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
badShards++;
|
|
||||||
badCount += status.Item2.GuildCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Log($"{goodShards} shard(s) connected, serving {goodCount} guild(s).");
|
|
||||||
if (badShards != 0) Log($"{badShards} shard(s) unavailable, >={badCount} guild(s) will not count in the next report.");
|
|
||||||
|
|
||||||
// Report only connected shards (to not have fluctuating member numbers on initial startup)
|
|
||||||
await SendExternalStatistics(counts.Where(t => t.Item2.ShardConnected), counts.Count());
|
|
||||||
}
|
|
||||||
|
|
||||||
private struct ShardStatus
|
|
||||||
{
|
|
||||||
public bool ShardConnected;
|
|
||||||
public int GuildCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<(int, ShardStatus)> GetGuildCounts()
|
|
||||||
{
|
|
||||||
var results = new List<(int, ShardStatus)>();
|
|
||||||
var shards = BotInstance.DiscordClient.Shards;
|
|
||||||
foreach (var shard in shards)
|
|
||||||
{
|
|
||||||
results.Add((shard.ShardId, new ShardStatus()
|
|
||||||
{
|
|
||||||
ShardConnected = shard.ConnectionState == Discord.ConnectionState.Connected,
|
|
||||||
GuildCount = shard.Guilds.Count
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send statistical information to external services.
|
/// Send statistical information to external services.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
async Task SendExternalStatistics(IEnumerable<(int, ShardStatus)> shardStats, int totalShards)
|
async Task SendExternalStatistics(int count)
|
||||||
{
|
{
|
||||||
var dbotsToken = BotInstance.Config.DBotsToken;
|
var dbotsToken = BotInstance.Config.DBotsToken;
|
||||||
if (dbotsToken != null)
|
if (dbotsToken != null)
|
||||||
{
|
{
|
||||||
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
||||||
const string Body = "{{ \"shardCount\": {0}, \"shardId\": {1}, \"guildCount\": {2} }}";
|
const string Body = "{{ \"guildCount\": {0} }}";
|
||||||
var uri = new Uri(string.Format(dBotsApiUrl, BotInstance.DiscordClient.CurrentUser.Id));
|
var uri = new Uri(string.Format(dBotsApiUrl, BotInstance.DiscordClient.CurrentUser.Id));
|
||||||
foreach (var shard in shardStats)
|
|
||||||
{
|
|
||||||
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
||||||
post.Headers.Add("Authorization", dbotsToken);
|
post.Headers.Add("Authorization", dbotsToken);
|
||||||
var data = string.Format(Body, totalShards, shard.Item1, shard.Item2.GuildCount);
|
post.Content = new StringContent(string.Format(Body, count), Encoding.UTF8, "application/json");
|
||||||
post.Content = new StringContent(data, 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);
|
await _httpClient.SendAsync(post);
|
||||||
Log("Discord Bots: Reports sent successfully.");
|
Log("Discord Bots: Count sent successfully.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ namespace BirthdayBot
|
||||||
|
|
||||||
internal Configuration Config { get; }
|
internal Configuration Config { get; }
|
||||||
internal DiscordShardedClient DiscordClient { get; }
|
internal DiscordShardedClient DiscordClient { get; }
|
||||||
|
// TODO consider removal of the guild cache
|
||||||
internal ConcurrentDictionary<ulong, GuildStateInformation> GuildCache { get; }
|
internal ConcurrentDictionary<ulong, GuildStateInformation> GuildCache { get; }
|
||||||
internal DiscordWebhookClient LogWebhook { get; }
|
internal DiscordWebhookClient LogWebhook { get; }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue