PluralKit/PluralKit.Bot/Services/LogChannelService.cs

66 lines
2.3 KiB
C#
Raw Normal View History

2019-04-19 18:48:37 +00:00
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using PluralKit.Core;
2019-07-18 15:13:42 +00:00
using Serilog;
2019-04-19 18:48:37 +00:00
namespace PluralKit.Bot {
public class LogChannelService {
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
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
}
public async Task LogMessage(DiscordClient client, PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, DiscordChannel originalChannel, DiscordUser sender, string content, GuildConfig? guildCfg = null)
{
if (guildCfg == null)
guildCfg = await _data.GetOrCreateGuildConfig(originalChannel.GuildId);
// Bail if logging is disabled either globally or for this channel
if (guildCfg.Value.LogChannel == null) return;
if (guildCfg.Value.LogBlacklist.Contains(originalChannel.Id)) return;
// Bail if we can't find the channel
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
// Bail if we don't have permission to send stuff here
if (!channel.BotHasAllPermissions(Permissions.SendMessages | Permissions.EmbedLinks))
return;
var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, originalMsgId, sender, content, originalChannel);
var url = $"https://discord.com/channels/{originalChannel.GuildId}/{originalChannel.Id}/{messageId}";
await channel.SendMessageAsync(content: url, embed: embed);
2019-04-19 18:48:37 +00:00
}
private async Task RemoveLogChannel(GuildConfig cfg)
{
cfg.LogChannel = null;
await _data.SaveGuildConfig(cfg);
}
2019-04-19 18:48:37 +00:00
}
}