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) } }