PluralKit/PluralKit.Bot/Services/LogChannelService.cs

38 lines
1.5 KiB
C#
Raw Normal View History

2019-04-19 18:48:37 +00:00
using System.Threading.Tasks;
using Dapper;
using Discord;
2019-07-18 15:13:42 +00:00
using Serilog;
2019-04-19 18:48:37 +00:00
namespace PluralKit.Bot {
public class LogChannelService {
2019-04-19 18:48:37 +00:00
private IDiscordClient _client;
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
2019-10-27 22:01:20 +00:00
public LogChannelService(IDiscordClient client, EmbedService embed, ILogger logger, IDataStore data)
2019-04-19 18:48:37 +00:00
{
2019-10-27 22:01:20 +00:00
_client = client;
_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(PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, IGuildChannel originalChannel, IUser sender, string content) {
var logChannel = await GetLogChannel(originalChannel.Guild);
if (logChannel == null) return;
2019-04-19 18:48:37 +00:00
var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, originalMsgId, sender, content, originalChannel);
var url = $"https://discordapp.com/channels/{originalChannel.GuildId}/{originalChannel.Id}/{messageId}";
await logChannel.SendMessageAsync(text: url, embed: embed);
2019-04-19 18:48:37 +00:00
}
2019-10-27 22:01:20 +00:00
private async Task<ITextChannel> GetLogChannel(IGuild guild)
{
var guildCfg = await _data.GetGuildConfig(guild.Id);
if (guildCfg.LogChannel == null) return null;
return await _client.GetChannelAsync(guildCfg.LogChannel.Value) as ITextChannel;
2019-04-19 18:48:37 +00:00
}
}
}