2021-05-07 17:35:09 +01:00
|
|
|
using System.Linq;
|
2019-04-19 20:48:37 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-02-12 15:16:19 +01:00
|
|
|
|
2020-06-12 20:29:50 +02:00
|
|
|
using Dapper;
|
|
|
|
|
2020-12-22 13:15:26 +01:00
|
|
|
using Myriad.Cache;
|
2020-12-23 02:19:02 +01:00
|
|
|
using Myriad.Extensions;
|
2020-12-22 13:15:26 +01:00
|
|
|
using Myriad.Rest;
|
|
|
|
using Myriad.Types;
|
2020-02-12 15:16:19 +01:00
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2019-07-18 17:13:42 +02:00
|
|
|
using Serilog;
|
2019-04-19 20:48:37 +02:00
|
|
|
|
2021-08-27 11:03:47 -04:00
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class LogChannelService
|
|
|
|
{
|
2020-06-12 20:29:50 +02:00
|
|
|
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;
|
2020-06-12 20:29:50 +02:00
|
|
|
private readonly ILogger _logger;
|
2020-12-22 13:15:26 +01:00
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly DiscordApiClient _rest;
|
2020-12-23 02:19:02 +01:00
|
|
|
private readonly Bot _bot;
|
2019-04-19 20:48:37 +02:00
|
|
|
|
2020-12-23 02:19:02 +01:00
|
|
|
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest, Bot bot)
|
2019-04-19 20:48:37 +02:00
|
|
|
{
|
2019-10-27 23:01:20 +01:00
|
|
|
_embed = embed;
|
2020-06-12 20:29:50 +02:00
|
|
|
_db = db;
|
2020-08-29 13:46:27 +02:00
|
|
|
_repo = repo;
|
2020-12-22 13:15:26 +01:00
|
|
|
_cache = cache;
|
|
|
|
_rest = rest;
|
2020-12-23 02:19:02 +01:00
|
|
|
_bot = bot;
|
2019-07-18 17:13:42 +02:00
|
|
|
_logger = logger.ForContext<LogChannelService>();
|
2019-04-19 20:48:37 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:41:51 -04:00
|
|
|
public async ValueTask LogMessage(MessageContext ctx, PKMessage proxiedMessage, Message trigger, Message hookMessage, string oldContent = null)
|
2019-11-03 19:15:50 +01:00
|
|
|
{
|
2021-08-04 00:41:51 -04:00
|
|
|
var logChannel = await GetAndCheckLogChannel(ctx, trigger, proxiedMessage);
|
|
|
|
if (logChannel == null)
|
|
|
|
return;
|
2020-12-23 02:19:02 +01:00
|
|
|
|
2021-08-04 00:41:51 -04:00
|
|
|
var triggerChannel = _cache.GetChannel(proxiedMessage.Channel);
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-09-29 21:51:38 -04:00
|
|
|
var system = await _repo.GetSystem(ctx.SystemId.Value);
|
|
|
|
var member = await _repo.GetMember(proxiedMessage.Member);
|
2021-05-07 17:35:09 +01:00
|
|
|
|
|
|
|
// Send embed!
|
2021-08-04 00:41:51 -04:00
|
|
|
var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name, oldContent);
|
|
|
|
var url = $"https://discord.com/channels/{proxiedMessage.Guild.Value}/{proxiedMessage.Channel}/{proxiedMessage.Mid}";
|
2021-08-27 11:03:47 -04:00
|
|
|
await _rest.CreateMessage(logChannel.Id, new() { Content = url, Embed = embed });
|
2021-05-07 17:35:09 +01:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:41:51 -04:00
|
|
|
private async Task<Channel?> GetAndCheckLogChannel(MessageContext ctx, Message trigger, PKMessage proxiedMessage)
|
2021-05-07 17:35:09 +01:00
|
|
|
{
|
2021-08-04 00:41:51 -04:00
|
|
|
if (proxiedMessage.Guild == null && proxiedMessage.Channel != trigger.ChannelId)
|
|
|
|
// a very old message is being edited outside of its original channel
|
|
|
|
// we can't know if we're in the correct guild, so skip fetching a log channel
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var guildId = proxiedMessage.Guild ?? trigger.GuildId.Value;
|
2021-05-07 17:35:09 +01:00
|
|
|
var logChannelId = ctx.LogChannel;
|
|
|
|
var isBlacklisted = ctx.InLogBlacklist;
|
|
|
|
|
2021-08-04 00:41:51 -04:00
|
|
|
if (proxiedMessage.Guild != trigger.GuildId)
|
2021-05-07 17:35:09 +01:00
|
|
|
{
|
2021-08-04 00:41:51 -04:00
|
|
|
// we're editing a message from a different server, get log channel info from the database
|
2021-09-29 21:51:38 -04:00
|
|
|
var guild = await _repo.GetGuild(proxiedMessage.Guild.Value);
|
2021-05-07 17:35:09 +01:00
|
|
|
logChannelId = guild.LogChannel;
|
|
|
|
isBlacklisted = guild.Blacklist.Any(x => x == logChannelId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.SystemId == null || logChannelId == null || isBlacklisted) return null;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-05-07 17:35:09 +01:00
|
|
|
// Find log channel and check if valid
|
|
|
|
var logChannel = await FindLogChannel(guildId, logChannelId.Value);
|
|
|
|
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText) return null;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2020-06-12 20:29:50 +02:00
|
|
|
// Check bot permissions
|
2020-12-23 02:19:02 +01:00
|
|
|
var perms = _bot.PermissionsIn(logChannel.Id);
|
|
|
|
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
|
|
|
|
{
|
|
|
|
_logger.Information(
|
2021-08-27 11:03:47 -04:00
|
|
|
"Does not have permission to log proxy, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
|
2020-12-23 02:19:02 +01:00
|
|
|
ctx.LogChannel.Value, trigger.GuildId!.Value, perms);
|
2021-05-07 17:35:09 +01:00
|
|
|
return null;
|
2020-12-23 02:19:02 +01:00
|
|
|
}
|
2021-05-07 17:35:09 +01:00
|
|
|
|
|
|
|
return logChannel;
|
2020-06-12 20:29:50 +02:00
|
|
|
}
|
2019-12-28 12:00:52 +01:00
|
|
|
|
2020-12-22 13:15:26 +01:00
|
|
|
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
|
2020-06-12 20:29:50 +02:00
|
|
|
{
|
2020-12-22 13:15:26 +01:00
|
|
|
// TODO: fetch it directly on cache miss?
|
2020-12-22 16:55:13 +01:00
|
|
|
if (_cache.TryGetChannel(channelId, out var channel))
|
|
|
|
return channel;
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2020-12-22 16:55:13 +01:00
|
|
|
// Channel doesn't exist or we don't have permission to access it, let's remove it from the database too
|
|
|
|
_logger.Warning("Attempted to fetch missing log channel {LogChannel} for guild {Guild}, removing from database", channelId, guildId);
|
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
await conn.ExecuteAsync("update servers set log_channel = null where id = @Guild",
|
2021-08-27 11:03:47 -04:00
|
|
|
new { Guild = guildId });
|
2019-04-19 20:48:37 +02:00
|
|
|
|
2020-12-22 16:55:13 +01:00
|
|
|
return null;
|
2020-05-01 16:36:21 +02:00
|
|
|
}
|
2019-04-19 20:48:37 +02:00
|
|
|
}
|
|
|
|
}
|