2019-04-19 18:48:37 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
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 {
|
2019-04-22 15:10:18 +00:00
|
|
|
private EmbedService _embed;
|
2019-10-27 22:01:20 +00:00
|
|
|
private IDataStore _data;
|
2019-07-18 15:13:42 +00:00
|
|
|
private ILogger _logger;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
public LogChannelService(EmbedService embed, ILogger logger, IDataStore data)
|
2019-04-19 18:48:37 +00:00
|
|
|
{
|
2019-10-27 22:01:20 +00:00
|
|
|
_embed = embed;
|
|
|
|
_data = data;
|
2019-07-18 15:13:42 +00:00
|
|
|
_logger = logger.ForContext<LogChannelService>();
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
public async Task LogMessage(DiscordClient client, PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, DiscordChannel originalChannel, DiscordUser sender, string content, GuildConfig? guildCfg = null)
|
2019-11-03 18:15:50 +00:00
|
|
|
{
|
2019-12-28 11:00:52 +00:00
|
|
|
if (guildCfg == null)
|
|
|
|
guildCfg = await _data.GetOrCreateGuildConfig(originalChannel.GuildId);
|
|
|
|
|
2019-11-03 18:15:50 +00:00
|
|
|
// Bail if logging is disabled either globally or for this channel
|
2019-12-28 11:00:52 +00:00
|
|
|
if (guildCfg.Value.LogChannel == null) return;
|
|
|
|
if (guildCfg.Value.LogBlacklist.Contains(originalChannel.Id)) return;
|
2019-11-03 18:15:50 +00:00
|
|
|
|
|
|
|
// Bail if we can't find the channel
|
2020-05-01 14:36:21 +00:00
|
|
|
DiscordChannel channel;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
channel = await client.GetChannelAsync(guildCfg.Value.LogChannel.Value);
|
|
|
|
}
|
|
|
|
catch (NotFoundException)
|
|
|
|
{
|
|
|
|
// If it doesn't exist, remove it from the DB
|
|
|
|
await RemoveLogChannel(guildCfg.Value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail if it's not a text channel
|
|
|
|
if (channel.Type != ChannelType.Text) return;
|
2019-04-19 18:48:37 +00:00
|
|
|
|
2020-02-22 00:54:10 +00:00
|
|
|
// Bail if we don't have permission to send stuff here
|
2020-04-17 21:10:01 +00:00
|
|
|
var neededPermissions = Permissions.SendMessages | Permissions.EmbedLinks;
|
|
|
|
if ((channel.BotPermissions() & neededPermissions) != neededPermissions)
|
2020-02-22 00:54:10 +00:00
|
|
|
return;
|
|
|
|
|
2019-08-08 05:36:09 +00:00
|
|
|
var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, originalMsgId, sender, content, originalChannel);
|
2019-07-15 15:53:01 +00:00
|
|
|
|
|
|
|
var url = $"https://discordapp.com/channels/{originalChannel.GuildId}/{originalChannel.Id}/{messageId}";
|
2020-02-22 00:54:10 +00:00
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
await channel.SendMessageAsync(content: url, embed: embed);
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
2020-05-01 14:36:21 +00:00
|
|
|
|
|
|
|
private async Task RemoveLogChannel(GuildConfig cfg)
|
|
|
|
{
|
|
|
|
cfg.LogChannel = null;
|
|
|
|
await _data.SaveGuildConfig(cfg);
|
|
|
|
}
|
2019-04-19 18:48:37 +00:00
|
|
|
}
|
|
|
|
}
|