2021-08-27 15:03:47 +00:00
|
|
|
using System.Collections.Generic;
|
2021-11-18 01:41:02 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-06-10 12:21:05 +00:00
|
|
|
|
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Extensions;
|
|
|
|
using Myriad.Gateway;
|
2021-11-18 18:11:02 +00:00
|
|
|
using Myriad.Types;
|
2021-06-10 12:21:05 +00:00
|
|
|
|
|
|
|
using Serilog.Core;
|
|
|
|
using Serilog.Events;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class SerilogGatewayEnricherFactory
|
|
|
|
{
|
|
|
|
private readonly Bot _bot;
|
|
|
|
private readonly IDiscordCache _cache;
|
|
|
|
|
|
|
|
public SerilogGatewayEnricherFactory(Bot bot, IDiscordCache cache)
|
|
|
|
{
|
|
|
|
_bot = bot;
|
|
|
|
_cache = cache;
|
|
|
|
}
|
|
|
|
|
2021-11-18 01:41:02 +00:00
|
|
|
public async Task<ILogEventEnricher> GetEnricher(Shard shard, IGatewayEvent evt)
|
2021-06-10 12:21:05 +00:00
|
|
|
{
|
|
|
|
var props = new List<LogEventProperty>
|
|
|
|
{
|
|
|
|
new("ShardId", new ScalarValue(shard.ShardId)),
|
|
|
|
};
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-06-10 12:21:05 +00:00
|
|
|
var (guild, channel) = GetGuildChannelId(evt);
|
|
|
|
var user = GetUserId(evt);
|
|
|
|
var message = GetMessageId(evt);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-06-10 12:21:05 +00:00
|
|
|
if (guild != null)
|
|
|
|
props.Add(new("GuildId", new ScalarValue(guild.Value)));
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-06-10 12:21:05 +00:00
|
|
|
if (channel != null)
|
|
|
|
{
|
2021-07-15 10:41:19 +00:00
|
|
|
props.Add(new("ChannelId", new ScalarValue(channel.Value)));
|
|
|
|
|
2021-11-18 18:11:02 +00:00
|
|
|
if (await _cache.TryGetChannel(channel.Value) != null)
|
2021-07-15 10:41:19 +00:00
|
|
|
{
|
2021-11-18 01:41:02 +00:00
|
|
|
var botPermissions = await _bot.PermissionsIn(channel.Value);
|
2021-07-15 10:41:19 +00:00
|
|
|
props.Add(new("BotPermissions", new ScalarValue(botPermissions)));
|
|
|
|
}
|
2021-06-10 12:21:05 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-06-10 12:21:05 +00:00
|
|
|
if (message != null)
|
|
|
|
props.Add(new("MessageId", new ScalarValue(message.Value)));
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-06-10 12:21:05 +00:00
|
|
|
if (user != null)
|
|
|
|
props.Add(new("UserId", new ScalarValue(user.Value)));
|
|
|
|
|
|
|
|
if (evt is MessageCreateEvent mce)
|
2021-11-18 01:41:02 +00:00
|
|
|
props.Add(new("UserPermissions", new ScalarValue(await _cache.PermissionsFor(mce))));
|
2021-06-10 12:21:05 +00:00
|
|
|
|
|
|
|
return new Inner(props);
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-06-10 12:21:05 +00:00
|
|
|
private (ulong?, ulong?) GetGuildChannelId(IGatewayEvent evt) => evt switch
|
|
|
|
{
|
|
|
|
ChannelCreateEvent e => (e.GuildId, e.Id),
|
|
|
|
ChannelUpdateEvent e => (e.GuildId, e.Id),
|
|
|
|
ChannelDeleteEvent e => (e.GuildId, e.Id),
|
|
|
|
MessageCreateEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
MessageUpdateEvent e => (e.GuildId.Value, e.ChannelId),
|
|
|
|
MessageDeleteEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
MessageDeleteBulkEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
MessageReactionAddEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
MessageReactionRemoveEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
MessageReactionRemoveAllEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
MessageReactionRemoveEmojiEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
InteractionCreateEvent e => (e.GuildId, e.ChannelId),
|
|
|
|
_ => (null, null)
|
|
|
|
};
|
|
|
|
|
|
|
|
private ulong? GetUserId(IGatewayEvent evt) => evt switch
|
|
|
|
{
|
|
|
|
MessageCreateEvent e => e.Author.Id,
|
|
|
|
MessageUpdateEvent e => e.Author.HasValue ? e.Author.Value.Id : null,
|
|
|
|
MessageReactionAddEvent e => e.UserId,
|
|
|
|
MessageReactionRemoveEvent e => e.UserId,
|
2021-07-28 05:55:06 +00:00
|
|
|
InteractionCreateEvent e => e.User?.Id ?? e.Member.User.Id,
|
2021-07-14 23:50:10 +00:00
|
|
|
_ => null,
|
2021-06-10 12:21:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private ulong? GetMessageId(IGatewayEvent evt) => evt switch
|
|
|
|
{
|
|
|
|
MessageCreateEvent e => e.Id,
|
|
|
|
MessageUpdateEvent e => e.Id,
|
|
|
|
MessageDeleteEvent e => e.Id,
|
|
|
|
MessageReactionAddEvent e => e.MessageId,
|
|
|
|
MessageReactionRemoveEvent e => e.MessageId,
|
|
|
|
MessageReactionRemoveAllEvent e => e.MessageId,
|
|
|
|
MessageReactionRemoveEmojiEvent e => e.MessageId,
|
|
|
|
InteractionCreateEvent e => e.Message?.Id,
|
2021-07-14 23:50:10 +00:00
|
|
|
_ => null,
|
2021-06-10 12:21:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private record Inner(List<LogEventProperty> Properties): ILogEventEnricher
|
|
|
|
{
|
|
|
|
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
|
|
|
|
{
|
2021-08-27 15:03:47 +00:00
|
|
|
foreach (var prop in Properties)
|
2021-06-10 12:21:05 +00:00
|
|
|
logEvent.AddPropertyIfAbsent(prop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|