2021-05-07 16:35:09 +00:00
|
|
|
using System.Linq;
|
2019-04-19 18:48:37 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2020-06-12 18:29:50 +00:00
|
|
|
using Dapper;
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
using Myriad.Cache;
|
2020-12-23 01:19:02 +00:00
|
|
|
using Myriad.Extensions;
|
2020-12-22 12:15:26 +00:00
|
|
|
using Myriad.Rest;
|
|
|
|
using Myriad.Types;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2019-07-18 15:13:42 +00:00
|
|
|
using Serilog;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2019-04-21 13:33:22 +00:00
|
|
|
namespace PluralKit.Bot {
|
2019-06-21 11:49:58 +00:00
|
|
|
public class LogChannelService {
|
2020-06-12 18:29:50 +00:00
|
|
|
private readonly EmbedService _embed;
|
2020-06-13 17:36:43 +00:00
|
|
|
private readonly IDatabase _db;
|
2020-08-29 11:46:27 +00:00
|
|
|
private readonly ModelRepository _repo;
|
2020-06-12 18:29:50 +00:00
|
|
|
private readonly ILogger _logger;
|
2020-12-22 12:15:26 +00:00
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly DiscordApiClient _rest;
|
2020-12-23 01:19:02 +00:00
|
|
|
private readonly Bot _bot;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2020-12-23 01:19:02 +00:00
|
|
|
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest, Bot bot)
|
2019-04-19 18:48:37 +00:00
|
|
|
{
|
2019-10-27 22:01:20 +00:00
|
|
|
_embed = embed;
|
2020-06-12 18:29:50 +00:00
|
|
|
_db = db;
|
2020-08-29 11:46:27 +00:00
|
|
|
_repo = repo;
|
2020-12-22 12:15:26 +00:00
|
|
|
_cache = cache;
|
|
|
|
_rest = rest;
|
2020-12-23 01:19:02 +00:00
|
|
|
_bot = bot;
|
2019-07-18 15:13:42 +00:00
|
|
|
_logger = logger.ForContext<LogChannelService>();
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
public async ValueTask LogMessage(MessageContext ctx, ProxyMatch proxy, Message trigger, ulong hookMessage)
|
2019-11-03 18:15:50 +00:00
|
|
|
{
|
2021-05-07 16:35:09 +00:00
|
|
|
var logChannel = await GetAndCheckLogChannel(ctx, trigger);
|
|
|
|
if (logChannel == null) return;
|
2020-12-23 01:19:02 +00:00
|
|
|
|
|
|
|
var triggerChannel = _cache.GetChannel(trigger.ChannelId);
|
2020-06-12 18:29:50 +00:00
|
|
|
|
2021-05-07 16:35:09 +00:00
|
|
|
// Send embed!
|
|
|
|
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,
|
|
|
|
triggerChannel);
|
|
|
|
var url = $"https://discord.com/channels/{trigger.GuildId}/{trigger.ChannelId}/{hookMessage}";
|
|
|
|
await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async ValueTask LogEditedMessage(MessageContext ctx, PKMessage proxy, Message trigger, Message originalMessage, string newContent)
|
|
|
|
{
|
|
|
|
var logChannel = await GetAndCheckLogChannel(ctx, trigger, proxy);
|
|
|
|
if (logChannel == null) return;
|
|
|
|
|
|
|
|
var triggerChannel = _cache.GetChannel(proxy.Channel);
|
|
|
|
|
|
|
|
// Send embed!
|
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
var embed = _embed.CreateEditedMessageEmbed(await _repo.GetSystem(conn, ctx.SystemId.Value),
|
|
|
|
await _repo.GetMember(conn, proxy.Member), originalMessage.Id, trigger.Id, trigger.Author, newContent, originalMessage.Content,
|
|
|
|
triggerChannel);
|
|
|
|
var url = $"https://discord.com/channels/{proxy.Guild.Value}/{proxy.Channel}/{proxy.Mid}";
|
|
|
|
await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Channel?> GetAndCheckLogChannel(MessageContext ctx, Message trigger, PKMessage original = null)
|
|
|
|
{
|
|
|
|
var guildId = trigger.GuildId != null ? trigger.GuildId!.Value : original.Guild.Value;
|
|
|
|
var logChannelId = ctx.LogChannel;
|
|
|
|
var isBlacklisted = ctx.InLogBlacklist;
|
|
|
|
|
|
|
|
if (original != null)
|
|
|
|
{
|
|
|
|
// we're editing a message, get log channel info from the database
|
|
|
|
var guild = await _db.Execute(c => _repo.GetGuild(c, original.Guild.Value));
|
|
|
|
logChannelId = guild.LogChannel;
|
|
|
|
isBlacklisted = guild.Blacklist.Any(x => x == logChannelId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.SystemId == null || logChannelId == null || isBlacklisted) return null;
|
|
|
|
|
|
|
|
|
|
|
|
// Find log channel and check if valid
|
|
|
|
var logChannel = await FindLogChannel(guildId, logChannelId.Value);
|
|
|
|
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText) return null;
|
|
|
|
|
2020-06-12 18:29:50 +00:00
|
|
|
// Check bot permissions
|
2020-12-23 01:19:02 +00:00
|
|
|
var perms = _bot.PermissionsIn(logChannel.Id);
|
|
|
|
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
|
|
|
|
{
|
|
|
|
_logger.Information(
|
|
|
|
"Does not have permission to proxy log, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
|
|
|
|
ctx.LogChannel.Value, trigger.GuildId!.Value, perms);
|
2021-05-07 16:35:09 +00:00
|
|
|
return null;
|
2020-12-23 01:19:02 +00:00
|
|
|
}
|
2021-05-07 16:35:09 +00:00
|
|
|
|
|
|
|
return logChannel;
|
2020-06-12 18:29:50 +00:00
|
|
|
}
|
2019-12-28 11:00:52 +00:00
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
|
2020-06-12 18:29:50 +00:00
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
// TODO: fetch it directly on cache miss?
|
2020-12-22 15:55:13 +00:00
|
|
|
if (_cache.TryGetChannel(channelId, out var channel))
|
|
|
|
return channel;
|
2020-07-02 16:29:04 +00:00
|
|
|
|
2020-12-22 15:55:13 +00: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",
|
|
|
|
new {Guild = guildId});
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2020-12-22 15:55:13 +00:00
|
|
|
return null;
|
2020-05-01 14:36:21 +00:00
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
}
|