PluralKit/PluralKit.Bot/Services/LogChannelService.cs
Ske e176ccbab5 Several more database-y refactors
- DbConnectionFactory renamed to "Database", will now be the primary entry point for DB stuff
- Created IPKConnection interface mostly containing async extensions to IDbConnection, use this going forward
- Reworked the Connection/Command wrappers (that have performance/logging extensions)
- Probably more stuff that I forgot???
2020-06-13 18:31:20 +02:00

69 lines
2.6 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using Dapper;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot {
public class LogChannelService {
private readonly EmbedService _embed;
private readonly Database _db;
private readonly IDataStore _data;
private readonly ILogger _logger;
private readonly DiscordRestClient _rest;
public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, Database db, IDataStore data)
{
_embed = embed;
_rest = rest;
_db = db;
_data = data;
_logger = logger.ForContext<LogChannelService>();
}
public async ValueTask LogMessage(MessageContext ctx, ProxyMatch proxy, DiscordMessage trigger, ulong hookMessage)
{
if (ctx.SystemId == null || ctx.LogChannel == null || ctx.InLogBlacklist) return;
// Find log channel and check if valid
var logChannel = await FindLogChannel(trigger.Channel.GuildId, ctx.LogChannel.Value);
if (logChannel == null || logChannel.Type != ChannelType.Text) return;
// Check bot permissions
if (!trigger.Channel.BotHasAllPermissions(Permissions.SendMessages | Permissions.EmbedLinks)) return;
// Send embed!
await using var conn = await _db.Obtain();
var embed = _embed.CreateLoggedMessageEmbed(await _data.GetSystemById(ctx.SystemId.Value),
await _data.GetMemberById(proxy.Member.Id), hookMessage, trigger.Id, trigger.Author, proxy.Content,
trigger.Channel);
var url = $"https://discord.com/channels/{trigger.Channel.GuildId}/{trigger.ChannelId}/{hookMessage}";
await logChannel.SendMessageAsync(content: url, embed: embed);
}
private async Task<DiscordChannel> FindLogChannel(ulong guild, ulong channel)
{
try
{
return await _rest.GetChannelAsync(channel);
}
catch (NotFoundException)
{
// Channel doesn't exist, let's remove it from the database too
_logger.Warning("Attempted to fetch missing log channel {LogChannel}, removing from database", channel);
await using var conn = await _db.Obtain();
await conn.ExecuteAsync("update servers set log_channel = null where server = @Guild",
new {Guild = guild});
}
return null;
}
}
}