Fixed voice and text channel counting

Updated changelog
This commit is contained in:
Toastie 2024-09-06 17:11:01 +12:00
parent 742d98a4c1
commit d58f1393ec
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 32 additions and 43 deletions
src/EllieBot/_common/Services/Impl

View file

@ -55,76 +55,60 @@ public sealed class StatsService : IStatsService, IReadyExecutor, IEService
_client.ChannelCreated += c =>
{
_ = Task.Run(() =>
{
if (c is ITextChannel)
Interlocked.Increment(ref textChannels);
else if (c is IVoiceChannel)
Interlocked.Increment(ref voiceChannels);
});
if (c is IVoiceChannel)
Interlocked.Increment(ref voiceChannels);
else if (c is ITextChannel)
Interlocked.Increment(ref textChannels);
return Task.CompletedTask;
};
_client.ChannelDestroyed += c =>
{
_ = Task.Run(() =>
{
if (c is ITextChannel)
Interlocked.Decrement(ref textChannels);
else if (c is IVoiceChannel)
Interlocked.Decrement(ref voiceChannels);
});
if (c is IVoiceChannel)
Interlocked.Decrement(ref voiceChannels);
else if (c is ITextChannel)
Interlocked.Decrement(ref textChannels);
return Task.CompletedTask;
};
_client.GuildAvailable += g =>
{
_ = Task.Run(() =>
{
var tc = g.Channels.Count(cx => cx is ITextChannel);
var vc = g.Channels.Count - tc;
Interlocked.Add(ref textChannels, tc);
Interlocked.Add(ref voiceChannels, vc);
});
var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
var vc = g.Channels.Count(cx => cx is IVoiceChannel);
Interlocked.Add(ref textChannels, tc);
Interlocked.Add(ref voiceChannels, vc);
return Task.CompletedTask;
};
_client.JoinedGuild += g =>
{
_ = Task.Run(() =>
{
var tc = g.Channels.Count(cx => cx is ITextChannel);
var vc = g.Channels.Count - tc;
Interlocked.Add(ref textChannels, tc);
Interlocked.Add(ref voiceChannels, vc);
});
var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
var vc = g.Channels.Count(cx => cx is IVoiceChannel);
Interlocked.Add(ref textChannels, tc);
Interlocked.Add(ref voiceChannels, vc);
return Task.CompletedTask;
};
_client.GuildUnavailable += g =>
{
_ = Task.Run(() =>
{
var tc = g.Channels.Count(cx => cx is ITextChannel);
var vc = g.Channels.Count - tc;
Interlocked.Add(ref textChannels, -tc);
Interlocked.Add(ref voiceChannels, -vc);
});
var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
var vc = g.Channels.Count(cx => cx is IVoiceChannel);
Interlocked.Add(ref textChannels, -tc);
Interlocked.Add(ref voiceChannels, -vc);
return Task.CompletedTask;
};
_client.LeftGuild += g =>
{
_ = Task.Run(() =>
{
var tc = g.Channels.Count(cx => cx is ITextChannel);
var vc = g.Channels.Count - tc;
Interlocked.Add(ref textChannels, -tc);
Interlocked.Add(ref voiceChannels, -vc);
});
var tc = g.Channels.Count(cx => cx is ITextChannel and not IVoiceChannel);
var vc = g.Channels.Count(cx => cx is IVoiceChannel);
Interlocked.Add(ref textChannels, -tc);
Interlocked.Add(ref voiceChannels, -vc);
return Task.CompletedTask;
};
@ -133,7 +117,7 @@ public sealed class StatsService : IStatsService, IReadyExecutor, IEService
private void InitializeChannelCount()
{
var guilds = _client.Guilds;
textChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is ITextChannel));
textChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is ITextChannel and not IVoiceChannel));
voiceChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is IVoiceChannel));
}