From 9bbe9df16d1c2b15d4af7bdb037dbe92bf882d49 Mon Sep 17 00:00:00 2001 From: Ske Date: Fri, 3 Sep 2021 22:20:07 +0200 Subject: [PATCH] Clean up a few database pool uses --- PluralKit.Bot/Handlers/ReactionAdded.cs | 17 +++++--------- PluralKit.Bot/Proxy/ProxyService.cs | 23 +++++++++++-------- .../Services/CommandMessageService.cs | 2 +- PluralKit.Bot/Services/LogChannelService.cs | 10 +++++--- .../ModelRepository.CommandMessage.cs | 2 +- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/PluralKit.Bot/Handlers/ReactionAdded.cs b/PluralKit.Bot/Handlers/ReactionAdded.cs index 579bf876..86962911 100644 --- a/PluralKit.Bot/Handlers/ReactionAdded.cs +++ b/PluralKit.Bot/Handlers/ReactionAdded.cs @@ -57,8 +57,7 @@ namespace PluralKit.Bot // since this can happen in DMs as well if (evt.Emoji.Name == "\u274c") { - await using var conn = await _db.Obtain(); - var commandMsg = await _commandMessageService.GetCommandMessage(conn, evt.MessageId); + var commandMsg = await _db.Execute(c => _commandMessageService.GetCommandMessage(c, evt.MessageId)); if (commandMsg != null) { await HandleCommandDeleteReaction(evt, commandMsg); @@ -77,8 +76,7 @@ namespace PluralKit.Bot // Message deletion case "\u274C": // Red X { - await using var conn = await _db.Obtain(); - var msg = await _repo.GetMessage(conn, evt.MessageId); + var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId)); if (msg != null) await HandleProxyDeleteReaction(evt, msg); @@ -87,8 +85,7 @@ namespace PluralKit.Bot case "\u2753": // Red question mark case "\u2754": // White question mark { - await using var conn = await _db.Obtain(); - var msg = await _repo.GetMessage(conn, evt.MessageId); + var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId)); if (msg != null) await HandleQueryReaction(evt, msg); @@ -101,8 +98,7 @@ namespace PluralKit.Bot case "\u23F0": // Alarm clock case "\u2757": // Exclamation mark { - await using var conn = await _db.Obtain(); - var msg = await _repo.GetMessage(conn, evt.MessageId); + var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId)); if (msg != null) await HandlePingReaction(evt, msg); break; @@ -115,11 +111,10 @@ namespace PluralKit.Bot if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages)) return; - using var conn = await _db.Obtain(); - var system = await _repo.GetSystemByAccount(conn, evt.UserId); + var system = await _db.Execute(c => _repo.GetSystemByAccount(c, evt.UserId)); // Can only delete your own message - if (msg.System.Id != system.Id) return; + if (msg.System.Id != system?.Id) return; try { diff --git a/PluralKit.Bot/Proxy/ProxyService.cs b/PluralKit.Bot/Proxy/ProxyService.cs index eafd72d9..155a002e 100644 --- a/PluralKit.Bot/Proxy/ProxyService.cs +++ b/PluralKit.Bot/Proxy/ProxyService.cs @@ -55,15 +55,16 @@ namespace PluralKit.Bot { if (!ShouldProxy(channel, message, ctx)) return false; - - // Fetch members and try to match to a specific member - await using var conn = await _db.Obtain(); - + var rootChannel = _cache.GetRootChannel(message.ChannelId); List members; + // Fetch members and try to match to a specific member using (_metrics.Measure.Timer.Time(BotMetrics.ProxyMembersQueryTime)) + { + await using var conn = await _db.Obtain(); members = (await _repo.GetProxyMembers(conn, message.Author.Id, message.GuildId!.Value)).ToList(); + } if (!_matcher.TryMatch(ctx, members, out var match, message.Content, message.Attachments.Length > 0, allowAutoproxy)) return false; @@ -85,7 +86,7 @@ namespace PluralKit.Bot var allowEmbeds = senderPermissions.HasFlag(PermissionSet.EmbedLinks); // Everything's in order, we can execute the proxy! - await ExecuteProxy(shard, conn, message, ctx, match, allowEveryone, allowEmbeds); + await ExecuteProxy(shard, message, ctx, match, allowEveryone, allowEmbeds); return true; } @@ -122,7 +123,7 @@ namespace PluralKit.Bot return true; } - private async Task ExecuteProxy(Shard shard, IPKConnection conn, Message trigger, MessageContext ctx, + private async Task ExecuteProxy(Shard shard, Message trigger, MessageContext ctx, ProxyMatch match, bool allowEveryone, bool allowEmbeds) { // Create reply embed @@ -161,7 +162,7 @@ namespace PluralKit.Bot Embeds = embeds.ToArray(), AllowEveryone = allowEveryone, }); - await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match); + await HandleProxyExecutedActions(shard, ctx, trigger, proxyMessage, match); } private async Task<(string?, string?)> FetchReferencedMessageAuthorInfo(Message trigger, Message referenced) @@ -281,7 +282,7 @@ namespace PluralKit.Bot private string FixSameNameInner(string name) => $"{name}\u17b5"; - private async Task HandleProxyExecutedActions(Shard shard, IPKConnection conn, MessageContext ctx, + private async Task HandleProxyExecutedActions(Shard shard, MessageContext ctx, Message triggerMessage, Message proxyMessage, ProxyMatch match) { @@ -295,7 +296,11 @@ namespace PluralKit.Bot Sender = triggerMessage.Author.Id }; - Task SaveMessageInDatabase() => _repo.AddMessage(conn, sentMessage); + async Task SaveMessageInDatabase() + { + await using var conn = await _db.Obtain(); + await _repo.AddMessage(conn, sentMessage); + } Task LogMessageToChannel() => _logChannel.LogMessage(ctx, sentMessage, triggerMessage, proxyMessage).AsTask(); diff --git a/PluralKit.Bot/Services/CommandMessageService.cs b/PluralKit.Bot/Services/CommandMessageService.cs index 80a985ad..868a56f9 100644 --- a/PluralKit.Bot/Services/CommandMessageService.cs +++ b/PluralKit.Bot/Services/CommandMessageService.cs @@ -31,7 +31,7 @@ namespace PluralKit.Bot await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, authorId)); } - public async Task GetCommandMessage(IPKConnection conn, ulong messageId) + public async Task GetCommandMessage(IPKConnection conn, ulong messageId) { return await _repo.GetCommandMessage(conn, messageId); } diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 3648bc84..3037183e 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -43,9 +43,13 @@ namespace PluralKit.Bot var triggerChannel = _cache.GetChannel(proxiedMessage.Channel); - await using var conn = await _db.Obtain(); - var system = await _repo.GetSystem(conn, ctx.SystemId.Value); - var member = await _repo.GetMember(conn, proxiedMessage.Member); + PKSystem system; + PKMember member; + await using (var conn = await _db.Obtain()) + { + system = await _repo.GetSystem(conn, ctx.SystemId.Value); + member = await _repo.GetMember(conn, proxiedMessage.Member); + } // Send embed! var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name, oldContent); diff --git a/PluralKit.Core/Database/Repository/ModelRepository.CommandMessage.cs b/PluralKit.Core/Database/Repository/ModelRepository.CommandMessage.cs index 17743169..602c5684 100644 --- a/PluralKit.Core/Database/Repository/ModelRepository.CommandMessage.cs +++ b/PluralKit.Core/Database/Repository/ModelRepository.CommandMessage.cs @@ -10,7 +10,7 @@ namespace PluralKit.Core conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)", new { Message = messageId, Author = authorId }); - public Task GetCommandMessage(IPKConnection conn, ulong messageId) => + public Task GetCommandMessage(IPKConnection conn, ulong messageId) => conn.QuerySingleOrDefaultAsync("select * from command_messages where message_id = @Message", new { Message = messageId });