Skip to content

Commit

Permalink
feature: Log with context (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
myrkvi authored Aug 22, 2024
2 parents 481f6b7 + 49e61c0 commit bf54e92
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
11 changes: 7 additions & 4 deletions commands/gatekeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var ApproveSlashCommand = discord.SlashCommandCreate{
}

func ApproveUserCommandHandler(e *handler.CommandEvent) error {
utils.LogInteraction("approve", e)
utils.LogInteractionContext("approve", e, e.Ctx)

guild, inGuild := e.Guild()
if !inGuild {
Expand All @@ -49,7 +49,7 @@ func ApproveUserCommandHandler(e *handler.CommandEvent) error {
}

func ApproveSlashCommandHandler(e *handler.CommandEvent) error {
utils.LogInteraction("Approve", e)
utils.LogInteractionContext("Approve", e, e.Ctx)

guild, inGuild := e.Guild()
if !inGuild {
Expand All @@ -61,15 +61,15 @@ func ApproveSlashCommandHandler(e *handler.CommandEvent) error {
}

func approvedInnerHandler(e *handler.CommandEvent, guild discord.Guild, member discord.ResolvedMember) error {
slog.Info("Entered approvedInnerHandler")
slog.InfoContext(e.Ctx, "Entered approvedInnerHandler")
err := e.DeferCreateMessage(true)
if err != nil {
slog.Error("Failed to defer message.", "err", err)
}

guildSettings, err := model.GetGuildSettings(guild.ID)
if err != nil {
slog.Error("Failed to get guild settings.",
slog.ErrorContext(e.Ctx, "Failed to get guild settings.",
"guild_id", guild.ID,
"err", err)
_, err = e.CreateFollowupMessage(discord.NewMessageCreateBuilder().
Expand Down Expand Up @@ -133,6 +133,9 @@ func approvedInnerHandler(e *handler.CommandEvent, guild discord.Guild, member d
}
}

slog.InfoContext(e.Ctx, "user has been approved",
"guild_id", guild.ID)

if guildSettings.GatekeepApprovedMessage == "" {
slog.Info("No approved message set; not sending message.")
_, err := e.CreateFollowupMessage(discord.NewMessageCreateBuilder().
Expand Down
40 changes: 29 additions & 11 deletions utils/interaction_log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"fmt"
"log/slog"
"time"
Expand All @@ -10,17 +11,7 @@ import (

func LogInteraction(interactionName string, interaction discord.Interaction) {
delay := time.Since(interaction.ID().Time())
type_ := "unknown"
switch interaction.Type() {
case discord.InteractionTypeApplicationCommand:
type_ = "application command"
case discord.InteractionTypeComponent:
type_ = "message component"
case discord.InteractionTypeModalSubmit:
type_ = "modal submit"
case discord.InteractionTypeAutocomplete:
type_ = "autocomplete"
}
type_ := getInteractionName(interaction)

slog.Info(fmt.Sprintf("Interaction %s (%s) received", interactionName, type_),
"user_id", interaction.User().ID,
Expand All @@ -29,3 +20,30 @@ func LogInteraction(interactionName string, interaction discord.Interaction) {
"delay", delay,
)
}

func LogInteractionContext(interactionName string, interaction discord.Interaction, ctx context.Context) {
delay := time.Since(interaction.ID().Time())
type_ := getInteractionName(interaction)

slog.InfoContext(ctx, fmt.Sprintf("Interaction %s (%s) received", interactionName, type_),
"user_id", interaction.User().ID,
"guild_id", interaction.GuildID(),
"channel_id", interaction.Channel().ID(),
"delay", delay,
)
}

func getInteractionName(interaction discord.Interaction) string {
switch interaction.Type() {
case discord.InteractionTypeApplicationCommand:
return "application command"
case discord.InteractionTypeComponent:
return "message component"
case discord.InteractionTypeModalSubmit:
return "modal submit"
case discord.InteractionTypeAutocomplete:
return "autocomplete"
default:
return "unknown"
}
}

0 comments on commit bf54e92

Please sign in to comment.