2020-06-25 07:14:05 +00:00
|
|
|
using System;
|
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-04-17 21:10:01 +00:00
|
|
|
using DSharpPlus;
|
|
|
|
using DSharpPlus.Entities;
|
2020-05-01 14:36:21 +00:00
|
|
|
using DSharpPlus.Exceptions;
|
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-06-12 18:29:50 +00:00
|
|
|
private readonly IDataStore _data;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly DiscordRestClient _rest;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2020-06-13 17:36:43 +00:00
|
|
|
public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, IDatabase db, IDataStore data)
|
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
|
|
|
_rest = rest;
|
|
|
|
_db = db;
|
2019-10-27 22:01:20 +00:00
|
|
|
_data = data;
|
2019-07-18 15:13:42 +00:00
|
|
|
_logger = logger.ForContext<LogChannelService>();
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
public async ValueTask LogMessage(MessageContext ctx, ProxyMatch proxy, DiscordMessage trigger, ulong hookMessage)
|
2019-11-03 18:15:50 +00:00
|
|
|
{
|
2020-06-12 21:13:21 +00:00
|
|
|
if (ctx.SystemId == null || ctx.LogChannel == null || ctx.InLogBlacklist) return;
|
2020-06-12 18:29:50 +00:00
|
|
|
|
|
|
|
// Find log channel and check if valid
|
2020-06-12 21:13:21 +00:00
|
|
|
var logChannel = await FindLogChannel(trigger.Channel.GuildId, ctx.LogChannel.Value);
|
2020-06-12 18:29:50 +00:00
|
|
|
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();
|
2020-06-13 17:42:04 +00:00
|
|
|
var embed = _embed.CreateLoggedMessageEmbed(await conn.QuerySystem(ctx.SystemId.Value),
|
|
|
|
await conn.QueryMember(proxy.Member.Id), hookMessage, trigger.Id, trigger.Author, proxy.Content,
|
2020-06-12 18:29:50 +00:00
|
|
|
trigger.Channel);
|
|
|
|
var url = $"https://discord.com/channels/{trigger.Channel.GuildId}/{trigger.ChannelId}/{hookMessage}";
|
2020-06-20 15:33:10 +00:00
|
|
|
await logChannel.SendMessageFixedAsync(content: url, embed: embed);
|
2020-06-12 18:29:50 +00:00
|
|
|
}
|
2019-12-28 11:00:52 +00:00
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
private async Task<DiscordChannel> FindLogChannel(ulong guild, ulong channel)
|
2020-06-12 18:29:50 +00:00
|
|
|
{
|
2020-05-01 14:36:21 +00:00
|
|
|
try
|
|
|
|
{
|
2020-06-12 21:13:21 +00:00
|
|
|
return await _rest.GetChannelAsync(channel);
|
2020-05-01 14:36:21 +00:00
|
|
|
}
|
2020-06-25 07:14:05 +00:00
|
|
|
catch (Exception e) when (e is NotFoundException || e is UnauthorizedException)
|
2020-05-01 14:36:21 +00:00
|
|
|
{
|
2020-06-25 07:14:05 +00:00
|
|
|
// Channel doesn't exist or we don't have permission to access it, let's remove it from the database too
|
2020-06-12 21:13:21 +00:00
|
|
|
_logger.Warning("Attempted to fetch missing log channel {LogChannel}, removing from database", channel);
|
2020-06-12 18:29:50 +00:00
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
await conn.ExecuteAsync("update servers set log_channel = null where server = @Guild",
|
|
|
|
new {Guild = guild});
|
2020-05-01 14:36:21 +00:00
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2020-06-12 18:29:50 +00:00
|
|
|
return null;
|
2020-05-01 14:36:21 +00:00
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
}
|