Clean up a few database pool uses

This commit is contained in:
Ske 2021-09-03 22:20:07 +02:00
parent 13260a4b0e
commit 9bbe9df16d
5 changed files with 29 additions and 25 deletions

View File

@ -57,8 +57,7 @@ namespace PluralKit.Bot
// since this can happen in DMs as well // since this can happen in DMs as well
if (evt.Emoji.Name == "\u274c") if (evt.Emoji.Name == "\u274c")
{ {
await using var conn = await _db.Obtain(); var commandMsg = await _db.Execute(c => _commandMessageService.GetCommandMessage(c, evt.MessageId));
var commandMsg = await _commandMessageService.GetCommandMessage(conn, evt.MessageId);
if (commandMsg != null) if (commandMsg != null)
{ {
await HandleCommandDeleteReaction(evt, commandMsg); await HandleCommandDeleteReaction(evt, commandMsg);
@ -77,8 +76,7 @@ namespace PluralKit.Bot
// Message deletion // Message deletion
case "\u274C": // Red X case "\u274C": // Red X
{ {
await using var conn = await _db.Obtain(); var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
var msg = await _repo.GetMessage(conn, evt.MessageId);
if (msg != null) if (msg != null)
await HandleProxyDeleteReaction(evt, msg); await HandleProxyDeleteReaction(evt, msg);
@ -87,8 +85,7 @@ namespace PluralKit.Bot
case "\u2753": // Red question mark case "\u2753": // Red question mark
case "\u2754": // White question mark case "\u2754": // White question mark
{ {
await using var conn = await _db.Obtain(); var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
var msg = await _repo.GetMessage(conn, evt.MessageId);
if (msg != null) if (msg != null)
await HandleQueryReaction(evt, msg); await HandleQueryReaction(evt, msg);
@ -101,8 +98,7 @@ namespace PluralKit.Bot
case "\u23F0": // Alarm clock case "\u23F0": // Alarm clock
case "\u2757": // Exclamation mark case "\u2757": // Exclamation mark
{ {
await using var conn = await _db.Obtain(); var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
var msg = await _repo.GetMessage(conn, evt.MessageId);
if (msg != null) if (msg != null)
await HandlePingReaction(evt, msg); await HandlePingReaction(evt, msg);
break; break;
@ -115,11 +111,10 @@ namespace PluralKit.Bot
if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages)) if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages))
return; return;
using var conn = await _db.Obtain(); var system = await _db.Execute(c => _repo.GetSystemByAccount(c, evt.UserId));
var system = await _repo.GetSystemByAccount(conn, evt.UserId);
// Can only delete your own message // Can only delete your own message
if (msg.System.Id != system.Id) return; if (msg.System.Id != system?.Id) return;
try try
{ {

View File

@ -56,14 +56,15 @@ namespace PluralKit.Bot
if (!ShouldProxy(channel, message, ctx)) if (!ShouldProxy(channel, message, ctx))
return false; 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); var rootChannel = _cache.GetRootChannel(message.ChannelId);
List<ProxyMember> members; List<ProxyMember> members;
// Fetch members and try to match to a specific member
using (_metrics.Measure.Timer.Time(BotMetrics.ProxyMembersQueryTime)) 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(); 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, if (!_matcher.TryMatch(ctx, members, out var match, message.Content, message.Attachments.Length > 0,
allowAutoproxy)) return false; allowAutoproxy)) return false;
@ -85,7 +86,7 @@ namespace PluralKit.Bot
var allowEmbeds = senderPermissions.HasFlag(PermissionSet.EmbedLinks); var allowEmbeds = senderPermissions.HasFlag(PermissionSet.EmbedLinks);
// Everything's in order, we can execute the proxy! // 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; return true;
} }
@ -122,7 +123,7 @@ namespace PluralKit.Bot
return true; 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) ProxyMatch match, bool allowEveryone, bool allowEmbeds)
{ {
// Create reply embed // Create reply embed
@ -161,7 +162,7 @@ namespace PluralKit.Bot
Embeds = embeds.ToArray(), Embeds = embeds.ToArray(),
AllowEveryone = allowEveryone, 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) private async Task<(string?, string?)> FetchReferencedMessageAuthorInfo(Message trigger, Message referenced)
@ -281,7 +282,7 @@ namespace PluralKit.Bot
private string FixSameNameInner(string name) private string FixSameNameInner(string name)
=> $"{name}\u17b5"; => $"{name}\u17b5";
private async Task HandleProxyExecutedActions(Shard shard, IPKConnection conn, MessageContext ctx, private async Task HandleProxyExecutedActions(Shard shard, MessageContext ctx,
Message triggerMessage, Message proxyMessage, Message triggerMessage, Message proxyMessage,
ProxyMatch match) ProxyMatch match)
{ {
@ -295,7 +296,11 @@ namespace PluralKit.Bot
Sender = triggerMessage.Author.Id 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(); Task LogMessageToChannel() => _logChannel.LogMessage(ctx, sentMessage, triggerMessage, proxyMessage).AsTask();

View File

@ -31,7 +31,7 @@ namespace PluralKit.Bot
await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, authorId)); await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, authorId));
} }
public async Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) public async Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId)
{ {
return await _repo.GetCommandMessage(conn, messageId); return await _repo.GetCommandMessage(conn, messageId);
} }

View File

@ -43,9 +43,13 @@ namespace PluralKit.Bot
var triggerChannel = _cache.GetChannel(proxiedMessage.Channel); var triggerChannel = _cache.GetChannel(proxiedMessage.Channel);
await using var conn = await _db.Obtain(); PKSystem system;
var system = await _repo.GetSystem(conn, ctx.SystemId.Value); PKMember member;
var member = await _repo.GetMember(conn, proxiedMessage.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! // Send embed!
var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name, oldContent); var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name, oldContent);

View File

@ -10,7 +10,7 @@ namespace PluralKit.Core
conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)", conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)",
new { Message = messageId, Author = authorId }); new { Message = messageId, Author = authorId });
public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) => public Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId) =>
conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message", conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",
new { Message = messageId }); new { Message = messageId });