From 227d68a2a4e640bf3b0766eb31b9149d86e4a329 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 31 Jan 2021 14:55:57 +0100 Subject: [PATCH] Convert event destructuring --- PluralKit.Bot/Init.cs | 3 +- PluralKit.Bot/Tracing/EventDestructuring.cs | 40 ++++++++++----------- PluralKit.Bot/Utils/DiscordUtils.cs | 12 ++----- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/PluralKit.Bot/Init.cs b/PluralKit.Bot/Init.cs index 630fd297..f4b7c6f5 100644 --- a/PluralKit.Bot/Init.cs +++ b/PluralKit.Bot/Init.cs @@ -120,7 +120,8 @@ namespace PluralKit.Bot builder.RegisterModule(new ConfigModule("Bot")); builder.RegisterModule(new LoggingModule("bot", cfg => { - cfg.Destructure.With(); + // TODO: do we need this? + // cfg.Destructure.With(); })); builder.RegisterModule(new MetricsModule()); builder.RegisterModule(); diff --git a/PluralKit.Bot/Tracing/EventDestructuring.cs b/PluralKit.Bot/Tracing/EventDestructuring.cs index fcc655bf..e685c43f 100644 --- a/PluralKit.Bot/Tracing/EventDestructuring.cs +++ b/PluralKit.Bot/Tracing/EventDestructuring.cs @@ -1,19 +1,19 @@ using System.Collections.Generic; -using DSharpPlus.Entities; -using DSharpPlus.EventArgs; +using Myriad.Gateway; using Serilog.Core; using Serilog.Events; namespace PluralKit.Bot { + // This class is unused and commented out in Init.cs - it's here from before the lib conversion. Is it needed?? public class EventDestructuring: IDestructuringPolicy { public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result) { - if (!(value is DiscordEventArgs dea)) + if (!(value is IGatewayEvent evt)) { result = null; return false; @@ -21,30 +21,30 @@ namespace PluralKit.Bot var props = new List { - new LogEventProperty("Type", new ScalarValue(dea.EventType())), + new("Type", new ScalarValue(evt.EventType())), }; - void AddMessage(DiscordMessage msg) + void AddMessage(ulong id, ulong channelId, ulong? guildId, ulong? author) { - 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))); + 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))); - if (msg.Author != null) - props.Add(new LogEventProperty("AuthorId", new ScalarValue(msg.Author.Id))); + if (author != null) + props.Add(new LogEventProperty("AuthorId", new ScalarValue(author))); } - 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) + 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) { - AddMessage(mra.Message); - props.Add(new LogEventProperty("ReactingUserId", new ScalarValue(mra.User.Id))); - props.Add(new LogEventProperty("Emoji", new ScalarValue(mra.Emoji.GetDiscordName()))); + 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))); } // Want shard last, just for visual reasons diff --git a/PluralKit.Bot/Utils/DiscordUtils.cs b/PluralKit.Bot/Utils/DiscordUtils.cs index 28b21ae0..c581c12b 100644 --- a/PluralKit.Bot/Utils/DiscordUtils.cs +++ b/PluralKit.Bot/Utils/DiscordUtils.cs @@ -2,16 +2,12 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Reflection; using System.Text.RegularExpressions; using System.Threading.Tasks; -using DSharpPlus; -using DSharpPlus.Entities; -using DSharpPlus.EventArgs; - using Myriad.Builders; using Myriad.Extensions; +using Myriad.Gateway; using Myriad.Rest; using Myriad.Rest.Types; using Myriad.Types; @@ -20,8 +16,6 @@ using NodaTime; using PluralKit.Core; -using Permissions = DSharpPlus.Permissions; - namespace PluralKit.Bot { public static class DiscordUtils @@ -190,7 +184,7 @@ namespace PluralKit.Bot return $"<{match.Value}>"; }); - public static string EventType(this DiscordEventArgs evt) => - evt.GetType().Name.Replace("EventArgs", ""); + public static string EventType(this IGatewayEvent evt) => + evt.GetType().Name.Replace("Event", ""); } }