From d58f1393ec996498186c1cbf524171d5d1d567b8 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 6 Sep 2024 17:11:01 +1200 Subject: [PATCH] Fixed voice and text channel counting Updated changelog --- CHANGELOG.md | 5 ++ .../_common/Services/Impl/StatsService.cs | 70 +++++++------------ 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8a8b01..73e815f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ Mostly based on [keepachangelog](https://keepachangelog.com/en/1.1.0/) except da ### Added +- Added `.leaveunkeptservers` which will make the bot leave all servers on all shards whose owners didn't run `.keep` command. + - This is a dangerous and irreversible command, don't use it. Meant for use on the public bot. + ### Changed - `.quote` commands cleaned up and improved @@ -21,6 +24,8 @@ Mostly based on [keepachangelog](https://keepachangelog.com/en/1.1.0/) except da - Fixed `.xpcurrew` breaking xp gain if user gains 0 xp from being in a voice channel - Fixed a bug in `.gatari` command +- Fixed some waifu related strings +- Fixed `.quoteshow` and `.quoteid` commands ## [5.1.7] - 09.08.2024 diff --git a/src/EllieBot/_common/Services/Impl/StatsService.cs b/src/EllieBot/_common/Services/Impl/StatsService.cs index ef7ea80..2c91a67 100644 --- a/src/EllieBot/_common/Services/Impl/StatsService.cs +++ b/src/EllieBot/_common/Services/Impl/StatsService.cs @@ -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)); }