From f70ff5c0538a7c00089d27ac6013e7ba298024b4 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 30 Aug 2024 18:50:01 +1200 Subject: [PATCH 1/5] increased delay to 2500, renamed method --- .../Administration/DangerousCommands/CleanupCommands.cs | 4 ++-- .../Administration/DangerousCommands/CleanupService.cs | 2 +- .../DangerousCommands/_common/ICleanupService.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs index 2b413ca..ada76bd 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs @@ -62,9 +62,9 @@ public partial class Administration if (!response) return; - for (var i = startShardId; i < _creds.GetCreds().TotalShards; i++) + for (var shardId = startShardId; shardId < _creds.GetCreds().TotalShards; shardId++) { - await _svc.LeaveUnkeptServers(i); + await _svc.StartLeavingUnkeptServers(shardId); await Task.Delay(2250 * 1000); } diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs index 9d339d9..dd269bc 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupService.cs @@ -237,7 +237,7 @@ public sealed class CleanupService : ICleanupService, IReadyExecutor, IEService return await table.CountAsync(); } - public async Task LeaveUnkeptServers(int shardId) + public async Task StartLeavingUnkeptServers(int shardId) => await _pubSub.Pub(_keepTriggerKey, shardId); private ValueTask OnKeepReport(KeepReport report) diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs b/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs index 318197c..8988f32 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/_common/ICleanupService.cs @@ -5,5 +5,5 @@ public interface ICleanupService Task DeleteMissingGuildDataAsync(); Task KeepGuild(ulong guildId); Task GetKeptGuildCount(); - Task LeaveUnkeptServers(int shardId); + Task StartLeavingUnkeptServers(int shardId); } \ No newline at end of file From b017c5e8052bcb96dc35293ae78b17ff993724d2 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 30 Aug 2024 18:57:46 +1200 Subject: [PATCH 2/5] increased delay to 3k on leaveunkeptservers Last commit want meant to have the delay at 2500 but I forgot --- .../Modules/Administration/DangerousCommands/CleanupCommands.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs index ada76bd..e6ba515 100644 --- a/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs +++ b/src/EllieBot/Modules/Administration/DangerousCommands/CleanupCommands.cs @@ -65,7 +65,7 @@ public partial class Administration for (var shardId = startShardId; shardId < _creds.GetCreds().TotalShards; shardId++) { await _svc.StartLeavingUnkeptServers(shardId); - await Task.Delay(2250 * 1000); + await Task.Delay(3000 * 1000); } await ctx.OkAsync(); From 89ab9a2ceb48c9d7eb920cd1a644ee124db0ecc3 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 30 Aug 2024 19:05:16 +1200 Subject: [PATCH 3/5] fixed quoteshow and quoteid commands not working --- src/EllieBot/Modules/Utility/Quote/IQuoteService.cs | 2 +- src/EllieBot/Modules/Utility/Quote/QuoteService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EllieBot/Modules/Utility/Quote/IQuoteService.cs b/src/EllieBot/Modules/Utility/Quote/IQuoteService.cs index 20bec68..acd6b19 100644 --- a/src/EllieBot/Modules/Utility/Quote/IQuoteService.cs +++ b/src/EllieBot/Modules/Utility/Quote/IQuoteService.cs @@ -29,7 +29,7 @@ public interface IQuoteService Task> GetGuildQuotesAsync(ulong guildId); Task RemoveAllByKeyword(ulong guildId, string keyword); - Task GetQuoteByIdAsync(ulong guildId, kwum quoteId); + Task GetQuoteByIdAsync(ulong guildId, int quoteId); Task AddQuoteAsync( ulong guildId, diff --git a/src/EllieBot/Modules/Utility/Quote/QuoteService.cs b/src/EllieBot/Modules/Utility/Quote/QuoteService.cs index 8cf8d62..a518a01 100644 --- a/src/EllieBot/Modules/Utility/Quote/QuoteService.cs +++ b/src/EllieBot/Modules/Utility/Quote/QuoteService.cs @@ -121,7 +121,7 @@ public sealed class QuoteService : IQuoteService, IEService return count; } - public async Task GetQuoteByIdAsync(ulong guildId, kwum quoteId) + public async Task GetQuoteByIdAsync(ulong guildId, int quoteId) { await using var uow = _db.GetDbContext(); From 742d98a4c11eb3dbb271e9875e73b4caa3fe94d9 Mon Sep 17 00:00:00 2001 From: Toastie Date: Wed, 4 Sep 2024 22:02:50 +1200 Subject: [PATCH 4/5] Updated image library --- src/EllieBot/EllieBot.csproj | 6 +-- .../Administration/AutoPublishService.cs | 2 + .../Gambling/PlantPick/PlantPickService.cs | 42 +++++++++++-------- .../Modules/Gambling/Slot/SlotCommands.cs | 6 +-- .../Modules/Searches/Crypto/CryptoService.cs | 2 +- .../ImagesharpStockChartDrawingService.cs | 8 ++-- src/EllieBot/Modules/Xp/XpService.cs | 27 ++++++------ .../_common/JsonConverters/Rgba32Converter.cs | 2 +- .../_common/_Extensions/Rgba32Extensions.cs | 2 +- 9 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/EllieBot/EllieBot.csproj b/src/EllieBot/EllieBot.csproj index b67381c..910d7d1 100644 --- a/src/EllieBot/EllieBot.csproj +++ b/src/EllieBot/EllieBot.csproj @@ -69,9 +69,9 @@ - - - + + + diff --git a/src/EllieBot/Modules/Administration/AutoPublishService.cs b/src/EllieBot/Modules/Administration/AutoPublishService.cs index 8f29495..a0983d3 100644 --- a/src/EllieBot/Modules/Administration/AutoPublishService.cs +++ b/src/EllieBot/Modules/Administration/AutoPublishService.cs @@ -37,6 +37,8 @@ public class AutoPublishService : IExecNoCommand, IReadyExecutor, IEService }); } + // todo GUILDS + public async Task OnReadyAsync() { var creds = _creds.GetCreds(); diff --git a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs index bffd035..37aba9f 100644 --- a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs +++ b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs @@ -111,13 +111,17 @@ public class PlantPickService : IEService, IExecNoCommand { var curImg = await _images.GetCurrencyImageAsync(); + if (curImg is null) + return (new MemoryStream(), null); + if (string.IsNullOrWhiteSpace(pass)) { // determine the extension - using var load = _ = Image.Load(curImg, out var format); + using var load = Image.Load(curImg); + var format = load.Metadata.DecodedImageFormat; // return the image - return (curImg.ToStream(), format.FileExtensions.FirstOrDefault() ?? "png"); + return (curImg.ToStream(), format?.FileExtensions.FirstOrDefault() ?? "png"); } // get the image stream and extension @@ -134,16 +138,17 @@ public class PlantPickService : IEService, IExecNoCommand { // draw lower, it looks better pass = pass.TrimTo(10, true).ToLowerInvariant(); - using var img = Image.Load(curImg, out var format); + using var img = Image.Load(curImg); // choose font size based on the image height, so that it's visible var font = _fonts.NotoSans.CreateFont(img.Height / 12.0f, FontStyle.Bold); img.Mutate(x => { // measure the size of the text to be drawing - var size = TextMeasurer.Measure(pass, new TextOptions(font) - { - Origin = new PointF(0, 0) - }); + var size = TextMeasurer.MeasureSize(pass, + new TextOptions(font) + { + Origin = new PointF(0, 0) + }); // fill the background with black, add 5 pixels on each side to make it look better x.FillPolygon(Color.ParseHex("00000080"), @@ -156,6 +161,7 @@ public class PlantPickService : IEService, IExecNoCommand x.DrawText(pass, font, Color.White, new(0, 0)); }); // return image as a stream for easy sending + var format = img.Metadata.DecodedImageFormat; return (img.ToStream(format), format.FileExtensions.FirstOrDefault() ?? "png"); } @@ -256,7 +262,8 @@ public class PlantPickService : IEService, IExecNoCommand pass = pass?.Trim().TrimTo(10, true).ToUpperInvariant(); // gets all plants in this channel with the same password - var entries = uow.Set().AsQueryable() + var entries = uow.Set() + .AsQueryable() .Where(x => x.ChannelId == ch.Id && pass == x.Password) .ToList(); // sum how much currency that is, and get all of the message ids (so that i can delete them) @@ -368,15 +375,16 @@ public class PlantPickService : IEService, IExecNoCommand string pass) { await using var uow = _db.GetDbContext(); - uow.Set().Add(new() - { - Amount = amount, - GuildId = gid, - ChannelId = cid, - Password = pass, - UserId = uid, - MessageId = mid - }); + uow.Set() + .Add(new() + { + Amount = amount, + GuildId = gid, + ChannelId = cid, + Password = pass, + UserId = uid, + MessageId = mid + }); await uow.SaveChangesAsync(); } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs b/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs index a5c7465..18c294a 100644 --- a/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs +++ b/src/EllieBot/Modules/Gambling/Slot/SlotCommands.cs @@ -172,13 +172,13 @@ public partial class Gambling } var slotBg = await _images.GetSlotBgAsync(); - var bgImage = Image.Load(slotBg, out _); + var bgImage = Image.Load(slotBg); var numbers = new int[3]; result.Rolls.CopyTo(numbers, 0); Color fontColor = Config.Slots.CurrencyFontColor; - bgImage.Mutate(x => x.DrawText(new TextOptions(_fonts.DottyFont.CreateFont(65)) + bgImage.Mutate(x => x.DrawText(new RichTextOptions(_fonts.DottyFont.CreateFont(65)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, @@ -190,7 +190,7 @@ public partial class Gambling var bottomFont = _fonts.DottyFont.CreateFont(50); - bgImage.Mutate(x => x.DrawText(new TextOptions(bottomFont) + bgImage.Mutate(x => x.DrawText(new RichTextOptions(bottomFont) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, diff --git a/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs b/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs index f5271d6..0ffd422 100644 --- a/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs +++ b/src/EllieBot/Modules/Searches/Crypto/CryptoService.cs @@ -90,7 +90,7 @@ public class CryptoService : IEService img.Mutate(x => { - x.DrawLines(color, 2, points); + x.DrawLine(color, 2, points); }); return img; diff --git a/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs b/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs index 731fc78..95aa51f 100644 --- a/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs +++ b/src/EllieBot/Modules/Searches/Crypto/Drawing/ImagesharpStockChartDrawingService.cs @@ -94,7 +94,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi => image.Mutate(ctx => { foreach (var data in drawData) - ctx.DrawLines(data.IsGreen + ctx.DrawLine(data.IsGreen ? _greenBrush : _redBrush, 1, @@ -128,7 +128,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi { // draw guides foreach (var y in lines) - ctx.DrawLines(_lineGuideColor, 1, new PointF(0, y), new PointF(WIDTH, y)); + ctx.DrawLine(_lineGuideColor, 1, new PointF(0, y), new PointF(WIDTH, y)); // // draw min and max price on the chart // ctx.DrawText(min.ToString(CultureInfo.InvariantCulture), @@ -156,7 +156,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi image.Mutate(ctx => { - ctx.DrawLines(_sparklineColor, 2, points); + ctx.DrawLine(_sparklineColor, 2, points); }); return Task.FromResult(new("png", image.ToStream())); @@ -177,7 +177,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi var points = GetSparklinePointsInternal(series); image.Mutate(ctx => { - ctx.DrawLines(Color.ParseHex("00FFFFAA"), 1, points); + ctx.DrawLine(Color.ParseHex("00FFFFAA"), 1, points); }); return Task.FromResult(new("png", image.ToStream())); diff --git a/src/EllieBot/Modules/Xp/XpService.cs b/src/EllieBot/Modules/Xp/XpService.cs index f32c7a5..32ba10e 100644 --- a/src/EllieBot/Modules/Xp/XpService.cs +++ b/src/EllieBot/Modules/Xp/XpService.cs @@ -994,23 +994,23 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand throw new ArgumentNullException(nameof(bgBytes)); } - var outlinePen = new Pen(Color.Black, 1f); + var outlinePen = new SolidPen(Color.Black, 1f); - using var img = Image.Load(bgBytes, out var imageFormat); + using var img = Image.Load(bgBytes); if (template.User.Name.Show) { var fontSize = (int)(template.User.Name.FontSize * 0.9); var username = stats.User.ToString(); var usernameFont = _fonts.NotoSans.CreateFont(fontSize, FontStyle.Bold); - var size = TextMeasurer.Measure($"@{username}", new(usernameFont)); + var size = TextMeasurer.MeasureSize($"@{username}", new(usernameFont)); var scale = 400f / size.Width; if (scale < 1) usernameFont = _fonts.NotoSans.CreateFont(template.User.Name.FontSize * scale, FontStyle.Bold); img.Mutate(x => { - x.DrawText(new TextOptions(usernameFont) + x.DrawText(new RichTextOptions(usernameFont) { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, @@ -1031,7 +1031,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand var clubFont = _fonts.NotoSans.CreateFont(template.Club.Name.FontSize, FontStyle.Regular); - img.Mutate(x => x.DrawText(new TextOptions(clubFont) + img.Mutate(x => x.DrawText(new RichTextOptions(clubFont) { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, @@ -1051,7 +1051,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand int maxSize) { var font = fontFamily.CreateFont(fontSize, style); - var size = TextMeasurer.Measure(text, new(font)); + var size = TextMeasurer.MeasureSize(text, new(font)); var scale = maxSize / size.Width; if (scale < 1) font = fontFamily.CreateFont(fontSize * scale, style); @@ -1114,7 +1114,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (template.User.Xp.Global.Show) { img.Mutate(x => x.DrawText( - new TextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold)) + new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, @@ -1128,7 +1128,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (template.User.Xp.Guild.Show) { img.Mutate(x => x.DrawText( - new TextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold)) + new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold)) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, @@ -1152,7 +1152,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand new(awX, awY))); } - var rankPen = new Pen(Color.White, 1); + var rankPen = new SolidPen(Color.White, 1); //ranking if (template.User.GlobalRank.Show) { @@ -1166,7 +1166,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand 68); img.Mutate(x => x.DrawText( - new TextOptions(globalRankFont) + new RichTextOptions(globalRankFont) { Origin = new(template.User.GlobalRank.Pos.X, template.User.GlobalRank.Pos.Y) }, @@ -1188,7 +1188,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand 43); img.Mutate(x => x.DrawText( - new TextOptions(guildRankFont) + new RichTextOptions(guildRankFont) { Origin = new(template.User.GuildRank.Pos.X, template.User.GuildRank.Pos.Y) }, @@ -1231,7 +1231,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } using var toDraw = Image.Load(data); - if (toDraw.Size() != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) + if (toDraw.Size != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y)) toDraw.Mutate(x => x.Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y)); img.Mutate(x => x.DrawImage(toDraw, @@ -1257,6 +1257,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand if (outputSize.X != img.Width || outputSize.Y != img.Height) img.Mutate(x => x.Resize(template.OutputSize.X, template.OutputSize.Y)); + var imageFormat = img.Metadata.DecodedImageFormat; var output = ((Stream)await img.ToStreamAsync(imageFormat), imageFormat); return output; @@ -1393,7 +1394,7 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand } using var toDraw = Image.Load(data); - if (toDraw.Size() != new Size(template.Club.Icon.Size.X, template.Club.Icon.Size.Y)) + if (toDraw.Size != new Size(template.Club.Icon.Size.X, template.Club.Icon.Size.Y)) toDraw.Mutate(x => x.Resize(template.Club.Icon.Size.X, template.Club.Icon.Size.Y)); img.Mutate(x => x.DrawImage( diff --git a/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs b/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs index 906db1e..4b2a313 100644 --- a/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs +++ b/src/EllieBot/_common/JsonConverters/Rgba32Converter.cs @@ -7,7 +7,7 @@ namespace EllieBot.Common.JsonConverters; public class Rgba32Converter : JsonConverter { public override Rgba32 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - => Rgba32.ParseHex(reader.GetString()); + => Rgba32.ParseHex(reader.GetString()!); public override void Write(Utf8JsonWriter writer, Rgba32 value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToHex()); diff --git a/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs b/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs index a0cd408..6fb4c7c 100644 --- a/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs +++ b/src/EllieBot/_common/_Extensions/Rgba32Extensions.cs @@ -24,7 +24,7 @@ public static class Rgba32Extensions using var frame = imgArray[i].Frames.CloneFrame(frameNumber % imgArray[i].Frames.Count); var offset = xOffset; imgFrame.Mutate(x => x.DrawImage(frame, new Point(offset, 0), new GraphicsOptions())); - xOffset += imgArray[i].Bounds().Width; + xOffset += imgArray[i].Bounds.Width; } } From d58f1393ec996498186c1cbf524171d5d1d567b8 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 6 Sep 2024 17:11:01 +1200 Subject: [PATCH 5/5] 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)); }