PluralKit/PluralKit.Bot/Tracing/EventDestructuring.cs

58 lines
2.3 KiB
C#
Raw Normal View History

2020-08-27 16:20:20 +00:00
using System.Collections.Generic;
2021-01-31 13:55:57 +00:00
using Myriad.Gateway;
2020-08-27 16:20:20 +00:00
using Serilog.Core;
using Serilog.Events;
namespace PluralKit.Bot
{
2021-01-31 13:55:57 +00:00
// This class is unused and commented out in Init.cs - it's here from before the lib conversion. Is it needed??
2020-08-27 16:20:20 +00:00
public class EventDestructuring: IDestructuringPolicy
{
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
out LogEventPropertyValue result)
{
2021-01-31 13:55:57 +00:00
if (!(value is IGatewayEvent evt))
2020-08-27 16:20:20 +00:00
{
result = null;
return false;
}
var props = new List<LogEventProperty>
{
2021-01-31 13:55:57 +00:00
new("Type", new ScalarValue(evt.EventType())),
2020-08-27 16:20:20 +00:00
};
2021-01-31 13:55:57 +00:00
void AddMessage(ulong id, ulong channelId, ulong? guildId, ulong? author)
2020-08-27 16:20:20 +00:00
{
2021-01-31 13:55:57 +00:00
props.Add(new LogEventProperty("MessageId", new ScalarValue(id)));
props.Add(new LogEventProperty("ChannelId", new ScalarValue(channelId)));
props.Add(new LogEventProperty("GuildId", new ScalarValue(guildId ?? 0)));
2020-08-27 16:20:20 +00:00
2021-01-31 13:55:57 +00:00
if (author != null)
props.Add(new LogEventProperty("AuthorId", new ScalarValue(author)));
2020-08-27 16:20:20 +00:00
}
2021-01-31 13:55:57 +00:00
if (value is MessageCreateEvent mc)
AddMessage(mc.Id, mc.ChannelId, mc.GuildId, mc.Author.Id);
else if (value is MessageUpdateEvent mu)
AddMessage(mu.Id, mu.ChannelId, mu.GuildId.Value, mu.Author.Value?.Id);
else if (value is MessageDeleteEvent md)
AddMessage(md.Id, md.ChannelId, md.GuildId, null);
else if (value is MessageReactionAddEvent mra)
2020-08-27 16:20:20 +00:00
{
2021-01-31 13:55:57 +00:00
AddMessage(mra.MessageId, mra.ChannelId, mra.GuildId, null);
props.Add(new LogEventProperty("ReactingUserId", new ScalarValue(mra.Emoji)));
props.Add(new LogEventProperty("Emoji", new ScalarValue(mra.Emoji.Name)));
2020-08-27 16:20:20 +00:00
}
// Want shard last, just for visual reasons
2020-11-15 12:53:31 +00:00
// TODO: D#+ update means we can't pull shard ID out of this, what do?
// props.Add(new LogEventProperty("Shard", new ScalarValue(dea.Client.ShardId)));
2020-08-27 16:20:20 +00:00
result = new StructureValue(props);
return true;
}
}
}