Skip to content

Commit e5dcb07

Browse files
authored
Feature/log interactions (#10)
2 parents fc97061 + 46afabd commit e5dcb07

File tree

9 files changed

+87
-4
lines changed

9 files changed

+87
-4
lines changed

commands/admin.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ var AdminCommand = discord.SlashCommandCreate{
129129
}
130130

131131
func AdminInfoHandler(e *handler.CommandEvent) error {
132+
utils.LogInteraction("admin info", e)
133+
132134
guild, inGuild := e.Guild()
133135
if !inGuild {
134136
return nil
@@ -156,6 +158,8 @@ func AdminInfoHandler(e *handler.CommandEvent) error {
156158
}
157159

158160
func AdminShowAllButtonHandler(e *handler.ComponentEvent) error {
161+
utils.LogInteraction("admin show all button", e)
162+
159163
return e.CreateMessage(discord.NewMessageCreateBuilder().
160164
SetContent(e.Message.Content).
161165
SetEmbeds(e.Message.Embeds...).
@@ -315,6 +319,8 @@ func AdminInfractionsHandler(e *handler.CommandEvent) error {
315319
}
316320

317321
func AdminGatekeepHandler(e *handler.CommandEvent) error {
322+
utils.LogInteraction("admin gatekeep", e)
323+
318324
data := e.SlashCommandInteractionData()
319325
guild, isGuild := e.Guild()
320326
if !isGuild {
@@ -373,6 +379,8 @@ func AdminGatekeepHandler(e *handler.CommandEvent) error {
373379
}
374380

375381
func AdminGatekeepMessageHandler(e *handler.CommandEvent) error {
382+
utils.LogInteraction("admin gatekeep message", e)
383+
376384
guild, isGuild := e.Guild()
377385
if !isGuild {
378386
return ErrEventNoGuildID
@@ -411,6 +419,8 @@ func messageModal(customID, title, contents string) discord.ModalCreate {
411419
}
412420

413421
func AdminGatekeepMessageButtonHandler(e *handler.ComponentEvent) error {
422+
utils.LogInteraction("admin gatekeep message button", e)
423+
414424
guild, isGuild := e.Guild()
415425
if !isGuild {
416426
return ErrEventNoGuildID
@@ -429,6 +439,8 @@ func AdminGatekeepMessageButtonHandler(e *handler.ComponentEvent) error {
429439
}
430440

431441
func AdminGatekeepMessageModalHandler(e *handler.ModalEvent) error {
442+
utils.LogInteraction("admin gatekeep message modal", e)
443+
432444
guild, inGuild := e.Guild()
433445
if !inGuild {
434446
return nil
@@ -463,6 +475,8 @@ func AdminGatekeepMessageModalHandler(e *handler.ModalEvent) error {
463475
}
464476

465477
func AdminJoinLeaveHandler(e *handler.CommandEvent) error {
478+
utils.LogInteraction("admin join-leave", e)
479+
466480
guild, inGuild := e.Guild()
467481
if !inGuild {
468482
return nil
@@ -514,6 +528,7 @@ func AdminJoinLeaveHandler(e *handler.CommandEvent) error {
514528
}
515529

516530
func AdminJoinMessageHandler(e *handler.CommandEvent) error {
531+
utils.LogInteraction("admin join-message", e)
517532
guild, isGuild := e.Guild()
518533
if !isGuild {
519534
return ErrEventNoGuildID
@@ -543,6 +558,8 @@ func AdminJoinMessageHandler(e *handler.CommandEvent) error {
543558
}
544559

545560
func AdminJoinMessageButtonHandler(e *handler.ComponentEvent) error {
561+
utils.LogInteraction("admin join-message button", e)
562+
546563
guild, isGuild := e.Guild()
547564
if !isGuild {
548565
return ErrEventNoGuildID
@@ -561,6 +578,8 @@ func AdminJoinMessageButtonHandler(e *handler.ComponentEvent) error {
561578
}
562579

563580
func AdminJoinMessageModalHandler(e *handler.ModalEvent) error {
581+
utils.LogInteraction("admin join-message modal", e)
582+
564583
guild, inGuild := e.Guild()
565584
if !inGuild {
566585
return nil
@@ -595,6 +614,8 @@ func AdminJoinMessageModalHandler(e *handler.ModalEvent) error {
595614
}
596615

597616
func AdminLeaveMessageHandler(e *handler.CommandEvent) error {
617+
utils.LogInteraction("admin leave-message", e)
618+
598619
guild, isGuild := e.Guild()
599620
if !isGuild {
600621
return ErrEventNoGuildID
@@ -624,6 +645,8 @@ func AdminLeaveMessageHandler(e *handler.CommandEvent) error {
624645
}
625646

626647
func AdminLeaveMessageButtonHandler(e *handler.ComponentEvent) error {
648+
utils.LogInteraction("admin leave-message button", e)
649+
627650
guild, isGuild := e.Guild()
628651
if !isGuild {
629652
return ErrEventNoGuildID
@@ -642,6 +665,8 @@ func AdminLeaveMessageButtonHandler(e *handler.ComponentEvent) error {
642665
}
643666

644667
func AdminLeaveMessageModalHandler(e *handler.ModalEvent) error {
668+
utils.LogInteraction("admin leave-message modal", e)
669+
645670
guild, inGuild := e.Guild()
646671
if !inGuild {
647672
return nil

commands/ban.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ var durationChoices = []discord.ApplicationCommandOptionChoiceString{
106106
}
107107

108108
func BanWithMessageHandler(e *handler.CommandEvent) error {
109+
utils.LogInteraction("ban with-message", e)
110+
109111
data := e.SlashCommandInteractionData()
110112
user := data.User("user")
111113
message := data.String("message")
@@ -114,6 +116,8 @@ func BanWithMessageHandler(e *handler.CommandEvent) error {
114116
}
115117

116118
func BanUntilHandler(e *handler.CommandEvent) error {
119+
utils.LogInteraction("ban until", e)
120+
117121
data := e.SlashCommandInteractionData()
118122
user := data.User("user")
119123
duration := data.String("duration")

commands/create_role_button.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var CreateRoleButtonCommand = discord.SlashCommandCreate{
6161
}
6262

6363
func CreateRoleButtonHandler(e *handler.CommandEvent) error {
64+
utils.LogInteraction("create-role-button", e)
65+
6466
if e.GuildID() == nil {
6567
slog.Warn("Received create role button command in DMs or guild ID is otherwise nil")
6668
return ErrEventNoGuildID

commands/gatekeep.go

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

3939
func ApproveUserCommandHandler(e *handler.CommandEvent) error {
40-
slog.Info("`Approve` user command called.",
41-
"guild_id", utils.Iif(e.GuildID() == nil, "<null>", e.GuildID().String()))
40+
utils.LogInteraction("approve", e)
41+
4242
guild, inGuild := e.Guild()
4343
if !inGuild {
4444
return nil
@@ -49,8 +49,8 @@ func ApproveUserCommandHandler(e *handler.CommandEvent) error {
4949
}
5050

5151
func ApproveSlashCommandHandler(e *handler.CommandEvent) error {
52-
slog.Info("`approve` slash command called.",
53-
"guild_id", utils.Iif(e.GuildID() == nil, "<null>", e.GuildID().String()))
52+
utils.LogInteraction("Approve", e)
53+
5454
guild, inGuild := e.Guild()
5555
if !inGuild {
5656
return nil
@@ -61,6 +61,7 @@ func ApproveSlashCommandHandler(e *handler.CommandEvent) error {
6161
}
6262

6363
func approvedInnerHandler(e *handler.CommandEvent, guild discord.Guild, member discord.ResolvedMember) error {
64+
slog.Debug("Entered approvedInnerHandler")
6465
_ = e.DeferCreateMessage(true)
6566
guildSettings, err := model.GetGuildSettings(guild.ID)
6667
if err != nil {

commands/infractions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ var WarnCommand = discord.SlashCommandCreate{
9191
}
9292

9393
func WarnHandler(e *handler.CommandEvent) error {
94+
utils.LogInteraction("warn", e)
95+
9496
ctx := context.Background()
9597
data := e.SlashCommandInteractionData()
9698

@@ -210,6 +212,8 @@ var UserInfractionsCommand = discord.SlashCommandCreate{
210212
}
211213

212214
func UserInfractionsHandler(e *handler.CommandEvent) error {
215+
utils.LogInteraction("warnings", e)
216+
213217
user := e.User()
214218
guild, ok := e.Guild()
215219
if !ok {
@@ -226,6 +230,7 @@ func UserInfractionsHandler(e *handler.CommandEvent) error {
226230
}
227231

228232
func UserInfractionButtonHandler(e *handler.ComponentEvent) error {
233+
utils.LogInteraction("warnings button", e)
229234
offsetStr := e.Variables["offset"]
230235
offset, err := strconv.Atoi(offsetStr)
231236
if err != nil {
@@ -329,6 +334,8 @@ var InfractionsCommand = discord.SlashCommandCreate{
329334

330335
// InfractionsListHandler handles the `/infractions list` command.
331336
func InfractionsListHandler(e *handler.CommandEvent) error {
337+
utils.LogInteraction("infractions list", e)
338+
332339
slog.Info("interaction `/infractions list` called.")
333340
data := e.SlashCommandInteractionData()
334341
user, hasUser := data.OptUser("user")
@@ -383,6 +390,8 @@ func InfractionsListHandler(e *handler.CommandEvent) error {
383390
}
384391

385392
func InfractionsRemoveHandler(e *handler.CommandEvent) error {
393+
utils.LogInteraction("infractions remove", e)
394+
386395
data := e.SlashCommandInteractionData()
387396
infID := data.String("infraction-id")
388397
guild, ok := e.Guild()
@@ -406,6 +415,8 @@ func InfractionsRemoveHandler(e *handler.CommandEvent) error {
406415
}
407416

408417
func InfractionsListComponentHandler(e *handler.ComponentEvent) error {
418+
utils.LogInteraction("infractions list component", e)
419+
409420
parentIx := e.Message.Interaction
410421
if parentIx == nil {
411422
return fmt.Errorf("no parent interaction found")

commands/kick.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/disgoorg/disgo/handler"
88
"github.com/disgoorg/disgo/rest"
99
"github.com/disgoorg/json"
10+
1011
"github.com/myrkvi/heimdallr/utils"
1112
)
1213

@@ -36,6 +37,8 @@ var KickCommand = discord.SlashCommandCreate{
3637
}
3738

3839
func KickWithMessageHandler(e *handler.CommandEvent) error {
40+
utils.LogInteraction("kick with-message", e)
41+
3942
data := e.SlashCommandInteractionData()
4043
guild, isGuild := e.Guild()
4144
if !isGuild {

commands/quote.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ var QuoteCommand = discord.SlashCommandCreate{
6060
}
6161

6262
func QuoteHandler(e *handler.CommandEvent) error {
63+
utils.LogInteraction("quote", e)
64+
6365
var guildID snowflake.ID
6466
if e.GuildID() != nil {
6567
guildID = *e.GuildID()

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"github.com/disgoorg/disgo/handler/middleware"
78
"log/slog"
89
"os"
910
"os/signal"
@@ -48,6 +49,9 @@ func main() {
4849
}
4950

5051
r := handler.New()
52+
53+
r.Use(middleware.Go)
54+
5155
r.Command("/ping", commands.PingHandler)
5256
r.Command("/quote", commands.QuoteHandler)
5357

utils/interaction_log.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"time"
7+
8+
"github.com/disgoorg/disgo/discord"
9+
)
10+
11+
func LogInteraction(interactionName string, interaction discord.Interaction) {
12+
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+
}
24+
25+
slog.Info(fmt.Sprintf("Interaction %s (%s) received", interactionName, type_),
26+
"user_id", interaction.User().ID,
27+
"guild_id", interaction.GuildID(),
28+
"channel_id", interaction.ChannelID(),
29+
"delay", delay,
30+
)
31+
}

0 commit comments

Comments
 (0)