PluralKit/PluralKit.Bot/Services/LogChannelService.cs

77 lines
3.2 KiB
C#
Raw Normal View History

2019-04-19 20:48:37 +02:00
using System.Threading.Tasks;
using Dapper;
2020-12-22 13:15:26 +01:00
using Myriad.Cache;
using Myriad.Rest;
using Myriad.Types;
using PluralKit.Core;
2019-07-18 17:13:42 +02:00
using Serilog;
2019-04-19 20:48:37 +02:00
namespace PluralKit.Bot {
public class LogChannelService {
private readonly EmbedService _embed;
2020-06-13 19:36:43 +02:00
private readonly IDatabase _db;
2020-08-29 13:46:27 +02:00
private readonly ModelRepository _repo;
private readonly ILogger _logger;
2020-12-22 13:15:26 +01:00
private readonly IDiscordCache _cache;
private readonly DiscordApiClient _rest;
2019-04-19 20:48:37 +02:00
2020-12-22 13:15:26 +01:00
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
2019-04-19 20:48:37 +02:00
{
2019-10-27 23:01:20 +01:00
_embed = embed;
_db = db;
2020-08-29 13:46:27 +02:00
_repo = repo;
2020-12-22 13:15:26 +01:00
_cache = cache;
_rest = rest;
2019-07-18 17:13:42 +02:00
_logger = logger.ForContext<LogChannelService>();
2019-04-19 20:48:37 +02:00
}
2020-12-22 13:15:26 +01:00
public async ValueTask LogMessage(MessageContext ctx, ProxyMatch proxy, Message trigger, ulong hookMessage)
{
if (ctx.SystemId == null || ctx.LogChannel == null || ctx.InLogBlacklist) return;
// Find log channel and check if valid
2020-12-22 13:15:26 +01:00
var logChannel = await FindLogChannel(trigger.GuildId!.Value, ctx.LogChannel.Value);
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText) return;
// Check bot permissions
2020-12-22 13:15:26 +01:00
// if (!logChannel.BotHasAllPermissions(Permissions.SendMessages | Permissions.EmbedLinks))
// {
// _logger.Information(
// "Does not have permission to proxy log, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
// ctx.LogChannel.Value, trigger.GuildId!.Value, trigger.Channel.BotPermissions());
// return;
// }
//
// Send embed!
2020-12-22 13:15:26 +01:00
// TODO: fix?
// await using var conn = await _db.Obtain();
// var embed = _embed.CreateLoggedMessageEmbed(await _repo.GetSystem(conn, ctx.SystemId.Value),
// await _repo.GetMember(conn, 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.SendMessageFixedAsync(content: url, embed: embed);
}
2020-12-22 13:15:26 +01:00
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
{
2020-12-22 13:15:26 +01:00
// TODO: fetch it directly on cache miss?
var channel = await _cache.GetChannel(channelId);
2020-12-22 13:15:26 +01:00
if (channel == null)
{
// Channel doesn't exist or we don't have permission to access it, let's remove it from the database too
2020-12-22 13:15:26 +01:00
_logger.Warning("Attempted to fetch missing log channel {LogChannel} for guild {Guild}, removing from database", channelId, guildId);
await using var conn = await _db.Obtain();
2020-06-26 15:06:46 +02:00
await conn.ExecuteAsync("update servers set log_channel = null where id = @Guild",
2020-12-22 13:15:26 +01:00
new {Guild = guildId});
}
2019-04-19 20:48:37 +02:00
2020-12-22 13:15:26 +01:00
return channel;
}
2019-04-19 20:48:37 +02:00
}
}