From 5909c36582046459a3b9c5eeadd65b84b0fd816a Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Sat, 17 Jan 2026 16:44:38 -0500 Subject: [PATCH] cleaner logging --- log.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 log.go diff --git a/log.go b/log.go new file mode 100644 index 0000000..46dd89c --- /dev/null +++ b/log.go @@ -0,0 +1,55 @@ +package main + +import ( + "log/slog" + + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/handler" +) + +func logMiddleware(next handler.Handler) handler.Handler { + return func(e *handler.InteractionEvent) error { + var msg string + attrs := make([]slog.Attr, 0, 8) + attrs = append(attrs, + slog.Uint64("interaction", uint64(e.Interaction.ID())), + slog.Uint64("user", uint64(e.Interaction.User().ID)), + ) + if guild := e.Interaction.GuildID(); guild != nil { + attrs = append(attrs, slog.String("guild", guild.String())) + } + switch i := e.Interaction.(type) { + case discord.ApplicationCommandInteraction: + msg = "command" + attrs = append(attrs, + slog.String("name", i.Data.CommandName()), + slog.Int("type", int(i.Data.Type())), + ) + switch data := i.Data.(type) { + case discord.SlashCommandInteractionData: + attrs = append(attrs, slog.String("path", data.CommandPath())) + } + + case discord.AutocompleteInteraction: + msg = "autocomplete" + attrs = append(attrs, + slog.String("name", i.Data.CommandName), + slog.String("path", i.Data.CommandPath()), + slog.String("focus", i.Data.Focused().Name), + ) + + case discord.ComponentInteraction: + msg = "component" + attrs = append(attrs, + slog.Int("type", int(i.Data.Type())), + slog.String("custom", i.Data.CustomID()), + ) + + default: + slog.WarnContext(e.Ctx, "unknown interaction", slog.Any("event", e)) + return nil + } + slog.LogAttrs(e.Ctx, slog.LevelInfo, msg, attrs...) + return next(e) + } +} diff --git a/main.go b/main.go index bc26e24..d4940c1 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ func main() { r := handler.New() r.DefaultContext(func() context.Context { return ctx }) r.Use(middleware.Go) - r.Use(middleware.Logger) + r.Use(logMiddleware) r.Route("/skill", func(r handler.Router) { r.SlashCommand("/", skillHandler) r.Autocomplete("/", skillAutocomplete)