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;
|
2021-11-27 02:10:56 +00:00
|
|
|
using Myriad.Rest.Types.Requests;
|
2020-12-22 12:15:26 +00:00
|
|
|
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
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class LogChannelService
|
2021-08-27 15:03:47 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly Bot _bot;
|
2022-09-06 09:52:37 +00:00
|
|
|
private readonly BotConfig _config;
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly EmbedService _embed;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ModelRepository _repo;
|
|
|
|
private readonly DiscordApiClient _rest;
|
|
|
|
|
|
|
|
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo,
|
2022-09-06 09:52:37 +00:00
|
|
|
IDiscordCache cache, DiscordApiClient rest, Bot bot, BotConfig config)
|
2021-08-27 15:03:47 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_embed = embed;
|
|
|
|
_db = db;
|
|
|
|
_repo = repo;
|
|
|
|
_cache = cache;
|
|
|
|
_rest = rest;
|
|
|
|
_bot = bot;
|
2022-09-06 09:52:37 +00:00
|
|
|
_config = config;
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger = logger.ForContext<LogChannelService>();
|
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
public async ValueTask LogMessage(PKMessage proxiedMessage, Message trigger, Message hookMessage, string oldContent = null)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-06-15 23:28:34 +00:00
|
|
|
var logChannelId = await GetAndCheckLogChannel(trigger, proxiedMessage);
|
2022-01-14 20:12:10 +00:00
|
|
|
if (logChannelId == null)
|
2021-11-27 02:10:56 +00:00
|
|
|
return;
|
2020-12-23 01:19:02 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var triggerChannel = await _cache.GetChannel(proxiedMessage.Channel);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-01-11 14:43:55 +00:00
|
|
|
var member = await _repo.GetMember(proxiedMessage.Member!.Value);
|
2022-06-15 23:28:34 +00:00
|
|
|
var system = await _repo.GetSystem(member.System);
|
2021-05-07 16:35:09 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Send embed!
|
|
|
|
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}";
|
2022-02-26 21:28:20 +00:00
|
|
|
await _rest.CreateMessage(logChannelId.Value, new MessageRequest { Content = url, Embeds = new[] { embed } });
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-05-07 16:35:09 +00:00
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
private async Task<ulong?> GetAndCheckLogChannel(Message trigger, PKMessage proxiedMessage)
|
2021-11-27 02:10:56 +00: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;
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
// get log channel info from the database
|
|
|
|
var guild = await _repo.GetGuild(guildId);
|
|
|
|
var logChannelId = guild.LogChannel;
|
|
|
|
var isBlacklisted = guild.LogBlacklist.Any(x => x == trigger.ChannelId);
|
2019-12-28 11:00:52 +00:00
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
// if (ctx.SystemId == null ||
|
|
|
|
// removed the above, there shouldn't be a way to get to this code path if you don't have a system registered
|
|
|
|
if (logChannelId == null || isBlacklisted) return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Find log channel and check if valid
|
|
|
|
var logChannel = await FindLogChannel(guildId, logChannelId.Value);
|
2023-03-24 15:18:23 +00:00
|
|
|
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText && logChannel.Type != Channel.ChannelType.GuildPublicThread && logChannel.Type != Channel.ChannelType.GuildPrivateThread) return null;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Check bot permissions
|
2022-03-10 01:06:53 +00:00
|
|
|
var perms = await GetPermissionsInLogChannel(logChannel);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
|
|
|
|
{
|
|
|
|
_logger.Information(
|
|
|
|
"Does not have permission to log proxy, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
|
2022-06-05 22:59:53 +00:00
|
|
|
logChannel.Id, guildId, perms);
|
2020-12-22 15:55:13 +00:00
|
|
|
return null;
|
2020-05-01 14:36:21 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-01-14 20:12:10 +00:00
|
|
|
return logChannel.Id;
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
// todo: move this somewhere else
|
|
|
|
private async Task<PermissionSet> GetPermissionsInLogChannel(Channel channel)
|
|
|
|
{
|
|
|
|
var guild = await _cache.TryGetGuild(channel.GuildId.Value);
|
|
|
|
if (guild == null)
|
|
|
|
guild = await _rest.GetGuild(channel.GuildId.Value);
|
|
|
|
|
|
|
|
var guildMember = await _cache.TryGetSelfMember(channel.GuildId.Value);
|
|
|
|
if (guildMember == null)
|
2022-09-06 09:52:37 +00:00
|
|
|
guildMember = await _rest.GetGuildMember(channel.GuildId.Value, _config.ClientId);
|
2022-03-10 01:06:53 +00:00
|
|
|
|
2022-09-06 09:52:37 +00:00
|
|
|
var perms = PermissionExtensions.PermissionsFor(guild, channel, _config.ClientId, guildMember);
|
2022-03-10 01:06:53 +00:00
|
|
|
return perms;
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
|
|
|
|
{
|
|
|
|
// TODO: fetch it directly on cache miss?
|
|
|
|
if (await _cache.TryGetChannel(channelId) is Channel channel)
|
|
|
|
return channel;
|
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
if (await _rest.GetChannelOrNull(channelId) is Channel restChannel)
|
|
|
|
return restChannel;
|
|
|
|
|
2021-11-27 02:10:56 +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 });
|
|
|
|
|
|
|
|
return null;
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
}
|