Skip to content

Commit bf54e92

Browse files
authored
feature: Log with context (#14)
2 parents 481f6b7 + 49e61c0 commit bf54e92

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

commands/gatekeep.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var ApproveSlashCommand = discord.SlashCommandCreate{
3737
}
3838

3939
func ApproveUserCommandHandler(e *handler.CommandEvent) error {
40-
utils.LogInteraction("approve", e)
40+
utils.LogInteractionContext("approve", e, e.Ctx)
4141

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

5151
func ApproveSlashCommandHandler(e *handler.CommandEvent) error {
52-
utils.LogInteraction("Approve", e)
52+
utils.LogInteractionContext("Approve", e, e.Ctx)
5353

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

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

7070
guildSettings, err := model.GetGuildSettings(guild.ID)
7171
if err != nil {
72-
slog.Error("Failed to get guild settings.",
72+
slog.ErrorContext(e.Ctx, "Failed to get guild settings.",
7373
"guild_id", guild.ID,
7474
"err", err)
7575
_, err = e.CreateFollowupMessage(discord.NewMessageCreateBuilder().
@@ -133,6 +133,9 @@ func approvedInnerHandler(e *handler.CommandEvent, guild discord.Guild, member d
133133
}
134134
}
135135

136+
slog.InfoContext(e.Ctx, "user has been approved",
137+
"guild_id", guild.ID)
138+
136139
if guildSettings.GatekeepApprovedMessage == "" {
137140
slog.Info("No approved message set; not sending message.")
138141
_, err := e.CreateFollowupMessage(discord.NewMessageCreateBuilder().

utils/interaction_log.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"context"
45
"fmt"
56
"log/slog"
67
"time"
@@ -10,17 +11,7 @@ import (
1011

1112
func LogInteraction(interactionName string, interaction discord.Interaction) {
1213
delay := time.Since(interaction.ID().Time())
13-
type_ := "unknown"
14-
switch interaction.Type() {
15-
case discord.InteractionTypeApplicationCommand:
16-
type_ = "application command"
17-
case discord.InteractionTypeComponent:
18-
type_ = "message component"
19-
case discord.InteractionTypeModalSubmit:
20-
type_ = "modal submit"
21-
case discord.InteractionTypeAutocomplete:
22-
type_ = "autocomplete"
23-
}
14+
type_ := getInteractionName(interaction)
2415

2516
slog.Info(fmt.Sprintf("Interaction %s (%s) received", interactionName, type_),
2617
"user_id", interaction.User().ID,
@@ -29,3 +20,30 @@ func LogInteraction(interactionName string, interaction discord.Interaction) {
2920
"delay", delay,
3021
)
3122
}
23+
24+
func LogInteractionContext(interactionName string, interaction discord.Interaction, ctx context.Context) {
25+
delay := time.Since(interaction.ID().Time())
26+
type_ := getInteractionName(interaction)
27+
28+
slog.InfoContext(ctx, fmt.Sprintf("Interaction %s (%s) received", interactionName, type_),
29+
"user_id", interaction.User().ID,
30+
"guild_id", interaction.GuildID(),
31+
"channel_id", interaction.Channel().ID(),
32+
"delay", delay,
33+
)
34+
}
35+
36+
func getInteractionName(interaction discord.Interaction) string {
37+
switch interaction.Type() {
38+
case discord.InteractionTypeApplicationCommand:
39+
return "application command"
40+
case discord.InteractionTypeComponent:
41+
return "message component"
42+
case discord.InteractionTypeModalSubmit:
43+
return "modal submit"
44+
case discord.InteractionTypeAutocomplete:
45+
return "autocomplete"
46+
default:
47+
return "unknown"
48+
}
49+
}

0 commit comments

Comments
 (0)