From 11eabe2e3dfa87799014b47918a0c3ee1e27cd66 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 21 Jul 2019 17:16:04 +0200 Subject: [PATCH] Update D.NET version and add bulk deletion support --- PluralKit.Bot/Bot.cs | 4 +++- PluralKit.Bot/PluralKit.Bot.csproj | 6 +++--- PluralKit.Bot/Services/ProxyService.cs | 6 ++++++ PluralKit.Core/PluralKit.Core.csproj | 4 ++-- PluralKit.Core/Stores.cs | 12 ++++++++++++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 9bed8d01..bc20421e 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -83,7 +83,8 @@ namespace PluralKit.Bot .AddSingleton(_ => new DiscordShardedClient(new DiscordSocketConfig { - MessageCacheSize = 0 + MessageCacheSize = 0, + ExclusiveBulkDelete = true })) .AddSingleton() @@ -161,6 +162,7 @@ namespace PluralKit.Bot _client.MessageReceived += (msg) => { var _ = MessageReceived(msg).CatchException(HandleRuntimeError); return Task.CompletedTask; }; _client.ReactionAdded += (message, channel, reaction) => { var _ = _proxy.HandleReactionAddedAsync(message, channel, reaction).CatchException(HandleRuntimeError); return Task.CompletedTask; }; _client.MessageDeleted += (message, channel) => { var _ = _proxy.HandleMessageDeletedAsync(message, channel).CatchException(HandleRuntimeError); return Task.CompletedTask; }; + _client.MessagesBulkDeleted += (messages, channel) => { var _ = _proxy.HandleMessageBulkDeleteAsync(messages, channel).CatchException(HandleRuntimeError); return Task.CompletedTask; }; _client.Log += FrameworkLog; } diff --git a/PluralKit.Bot/PluralKit.Bot.csproj b/PluralKit.Bot/PluralKit.Bot.csproj index 75aed6c6..b40772c1 100644 --- a/PluralKit.Bot/PluralKit.Bot.csproj +++ b/PluralKit.Bot/PluralKit.Bot.csproj @@ -10,9 +10,9 @@ - - - + + + diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index bd6d1732..69599e30 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -271,6 +271,12 @@ namespace PluralKit.Bot await _messageStorage.Delete(message.Id); } + public async Task HandleMessageBulkDeleteAsync(IReadOnlyCollection> messages, IMessageChannel channel) + { + _logger.Information("Bulk deleting {Count} messages in channel {Channel}", messages.Count, channel.Id); + await _messageStorage.BulkDelete(messages.Select(m => m.Id).ToList()); + } + private string FixClyde(string name) { var match = Regex.Match(name, "clyde", RegexOptions.IgnoreCase); diff --git a/PluralKit.Core/PluralKit.Core.csproj b/PluralKit.Core/PluralKit.Core.csproj index 76055dcc..566aeace 100644 --- a/PluralKit.Core/PluralKit.Core.csproj +++ b/PluralKit.Core/PluralKit.Core.csproj @@ -17,8 +17,8 @@ - - + + diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 9a5ca0c0..038416e0 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -227,6 +227,18 @@ namespace PluralKit { _logger.Information("Deleted message {Message}", id); } + public async Task BulkDelete(IReadOnlyCollection ids) + { + using (var conn = await _conn.Obtain()) + { + // Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong + // Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway) + var foundCount = await conn.ExecuteAsync("delete from messages where mid = any(@Ids)", new {Ids = ids.Select(id => (long) id).ToArray()}); + if (foundCount > 0) + _logger.Information("Bulk deleted messages {Messages}, {FoundCount} found", ids, foundCount); + } + } + public async Task Count() { using (var conn = await _conn.Obtain())