cleaner logging
This commit is contained in:
55
log.go
Normal file
55
log.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
2
main.go
2
main.go
@@ -66,7 +66,7 @@ func main() {
|
|||||||
r := handler.New()
|
r := handler.New()
|
||||||
r.DefaultContext(func() context.Context { return ctx })
|
r.DefaultContext(func() context.Context { return ctx })
|
||||||
r.Use(middleware.Go)
|
r.Use(middleware.Go)
|
||||||
r.Use(middleware.Logger)
|
r.Use(logMiddleware)
|
||||||
r.Route("/skill", func(r handler.Router) {
|
r.Route("/skill", func(r handler.Router) {
|
||||||
r.SlashCommand("/", skillHandler)
|
r.SlashCommand("/", skillHandler)
|
||||||
r.Autocomplete("/", skillAutocomplete)
|
r.Autocomplete("/", skillAutocomplete)
|
||||||
|
|||||||
Reference in New Issue
Block a user