PluralKit/PluralKit.Bot/Services/LogChannelService.cs
Ske d829630a35 Fix database connection pool contention (maybe)
Instead of acquiring a connection per service per request, we
acquire connections more often but at a more granular level, meaning
they're also disposed of more quickly instead of staying for a long time
in case of long-running commands or leaks.
2019-07-11 21:25:23 +02:00

57 lines
2.0 KiB
C#

using System.Data;
using System.Threading.Tasks;
using Dapper;
using Discord;
namespace PluralKit.Bot {
public class ServerDefinition {
public ulong Id { get; set; }
public ulong? LogChannel { get; set; }
}
public class LogChannelService {
private IDiscordClient _client;
private DbConnectionFactory _conn;
private EmbedService _embed;
public LogChannelService(IDiscordClient client, DbConnectionFactory conn, EmbedService embed)
{
this._client = client;
this._conn = conn;
this._embed = embed;
}
public async Task LogMessage(PKSystem system, PKMember member, IMessage message, IUser sender) {
var channel = await GetLogChannel((message.Channel as IGuildChannel).Guild);
if (channel == null) return;
var embed = _embed.CreateLoggedMessageEmbed(system, member, message, sender);
await channel.SendMessageAsync(text: message.GetJumpUrl(), embed: embed);
}
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
using (var conn = _conn.Obtain())
{
var server =
await conn.QueryFirstOrDefaultAsync<ServerDefinition>("select * from servers where id = @Id",
new {Id = guild.Id});
if (server?.LogChannel == null) return null;
return await _client.GetChannelAsync(server.LogChannel.Value) as ITextChannel;
}
}
public async Task SetLogChannel(IGuild guild, ITextChannel newLogChannel) {
var def = new ServerDefinition {
Id = guild.Id,
LogChannel = newLogChannel?.Id
};
using (var conn = _conn.Obtain())
{
await conn.QueryAsync(
"insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel",
def);
}
}
}
}