More updates to event infrastructure
This commit is contained in:
55
PluralKit.Bot/Tracing/EventDestructuring.cs
Normal file
55
PluralKit.Bot/Tracing/EventDestructuring.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus.EventArgs;
|
||||
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class EventDestructuring: IDestructuringPolicy
|
||||
{
|
||||
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
|
||||
out LogEventPropertyValue result)
|
||||
{
|
||||
if (!(value is DiscordEventArgs dea))
|
||||
{
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var props = new List<LogEventProperty>
|
||||
{
|
||||
new LogEventProperty("Type", new ScalarValue(dea.EventType())),
|
||||
new LogEventProperty("Shard", new ScalarValue(dea.Client.ShardId))
|
||||
};
|
||||
|
||||
void AddMessage(DiscordMessage msg)
|
||||
{
|
||||
props.Add(new LogEventProperty("MessageId", new ScalarValue(msg.Id)));
|
||||
props.Add(new LogEventProperty("ChannelId", new ScalarValue(msg.ChannelId)));
|
||||
props.Add(new LogEventProperty("GuildId", new ScalarValue(msg.Channel.GuildId)));
|
||||
|
||||
if (msg.Author != null)
|
||||
props.Add(new LogEventProperty("AuthorId", new ScalarValue(msg.Author.Id)));
|
||||
}
|
||||
|
||||
if (value is MessageCreateEventArgs mc)
|
||||
AddMessage(mc.Message);
|
||||
else if (value is MessageUpdateEventArgs mu)
|
||||
AddMessage(mu.Message);
|
||||
else if (value is MessageDeleteEventArgs md)
|
||||
AddMessage(md.Message);
|
||||
else if (value is MessageReactionAddEventArgs mra)
|
||||
{
|
||||
AddMessage(mra.Message);
|
||||
props.Add(new LogEventProperty("ReactingUserId", new ScalarValue(mra.User.Id)));
|
||||
props.Add(new LogEventProperty("Emoji", new ScalarValue(mra.Emoji.GetDiscordName())));
|
||||
}
|
||||
|
||||
result = new StructureValue(props);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user