From 46afabd28962fd3150d01a93a406d0accf51f7c2 Mon Sep 17 00:00:00 2001 From: Vegard Berg Date: Wed, 21 Aug 2024 13:45:17 +0200 Subject: [PATCH] feature: Logging various interactions. --- commands/admin.go | 25 +++++++++++++++++++++++++ commands/ban.go | 4 ++++ commands/create_role_button.go | 2 ++ commands/gatekeep.go | 9 +++++---- commands/infractions.go | 11 +++++++++++ commands/kick.go | 3 +++ commands/quote.go | 2 ++ utils/interaction_log.go | 31 +++++++++++++++++++++++++++++++ 8 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 utils/interaction_log.go diff --git a/commands/admin.go b/commands/admin.go index cd2ed11..5017b4c 100644 --- a/commands/admin.go +++ b/commands/admin.go @@ -129,6 +129,8 @@ var AdminCommand = discord.SlashCommandCreate{ } func AdminInfoHandler(e *handler.CommandEvent) error { + utils.LogInteraction("admin info", e) + guild, inGuild := e.Guild() if !inGuild { return nil @@ -156,6 +158,8 @@ func AdminInfoHandler(e *handler.CommandEvent) error { } func AdminShowAllButtonHandler(e *handler.ComponentEvent) error { + utils.LogInteraction("admin show all button", e) + return e.CreateMessage(discord.NewMessageCreateBuilder(). SetContent(e.Message.Content). SetEmbeds(e.Message.Embeds...). @@ -315,6 +319,8 @@ func AdminInfractionsHandler(e *handler.CommandEvent) error { } func AdminGatekeepHandler(e *handler.CommandEvent) error { + utils.LogInteraction("admin gatekeep", e) + data := e.SlashCommandInteractionData() guild, isGuild := e.Guild() if !isGuild { @@ -373,6 +379,8 @@ func AdminGatekeepHandler(e *handler.CommandEvent) error { } func AdminGatekeepMessageHandler(e *handler.CommandEvent) error { + utils.LogInteraction("admin gatekeep message", e) + guild, isGuild := e.Guild() if !isGuild { return ErrEventNoGuildID @@ -411,6 +419,8 @@ func messageModal(customID, title, contents string) discord.ModalCreate { } func AdminGatekeepMessageButtonHandler(e *handler.ComponentEvent) error { + utils.LogInteraction("admin gatekeep message button", e) + guild, isGuild := e.Guild() if !isGuild { return ErrEventNoGuildID @@ -429,6 +439,8 @@ func AdminGatekeepMessageButtonHandler(e *handler.ComponentEvent) error { } func AdminGatekeepMessageModalHandler(e *handler.ModalEvent) error { + utils.LogInteraction("admin gatekeep message modal", e) + guild, inGuild := e.Guild() if !inGuild { return nil @@ -463,6 +475,8 @@ func AdminGatekeepMessageModalHandler(e *handler.ModalEvent) error { } func AdminJoinLeaveHandler(e *handler.CommandEvent) error { + utils.LogInteraction("admin join-leave", e) + guild, inGuild := e.Guild() if !inGuild { return nil @@ -514,6 +528,7 @@ func AdminJoinLeaveHandler(e *handler.CommandEvent) error { } func AdminJoinMessageHandler(e *handler.CommandEvent) error { + utils.LogInteraction("admin join-message", e) guild, isGuild := e.Guild() if !isGuild { return ErrEventNoGuildID @@ -543,6 +558,8 @@ func AdminJoinMessageHandler(e *handler.CommandEvent) error { } func AdminJoinMessageButtonHandler(e *handler.ComponentEvent) error { + utils.LogInteraction("admin join-message button", e) + guild, isGuild := e.Guild() if !isGuild { return ErrEventNoGuildID @@ -561,6 +578,8 @@ func AdminJoinMessageButtonHandler(e *handler.ComponentEvent) error { } func AdminJoinMessageModalHandler(e *handler.ModalEvent) error { + utils.LogInteraction("admin join-message modal", e) + guild, inGuild := e.Guild() if !inGuild { return nil @@ -595,6 +614,8 @@ func AdminJoinMessageModalHandler(e *handler.ModalEvent) error { } func AdminLeaveMessageHandler(e *handler.CommandEvent) error { + utils.LogInteraction("admin leave-message", e) + guild, isGuild := e.Guild() if !isGuild { return ErrEventNoGuildID @@ -624,6 +645,8 @@ func AdminLeaveMessageHandler(e *handler.CommandEvent) error { } func AdminLeaveMessageButtonHandler(e *handler.ComponentEvent) error { + utils.LogInteraction("admin leave-message button", e) + guild, isGuild := e.Guild() if !isGuild { return ErrEventNoGuildID @@ -642,6 +665,8 @@ func AdminLeaveMessageButtonHandler(e *handler.ComponentEvent) error { } func AdminLeaveMessageModalHandler(e *handler.ModalEvent) error { + utils.LogInteraction("admin leave-message modal", e) + guild, inGuild := e.Guild() if !inGuild { return nil diff --git a/commands/ban.go b/commands/ban.go index b2acb22..33ae47a 100644 --- a/commands/ban.go +++ b/commands/ban.go @@ -106,6 +106,8 @@ var durationChoices = []discord.ApplicationCommandOptionChoiceString{ } func BanWithMessageHandler(e *handler.CommandEvent) error { + utils.LogInteraction("ban with-message", e) + data := e.SlashCommandInteractionData() user := data.User("user") message := data.String("message") @@ -114,6 +116,8 @@ func BanWithMessageHandler(e *handler.CommandEvent) error { } func BanUntilHandler(e *handler.CommandEvent) error { + utils.LogInteraction("ban until", e) + data := e.SlashCommandInteractionData() user := data.User("user") duration := data.String("duration") diff --git a/commands/create_role_button.go b/commands/create_role_button.go index a219925..e5d04b8 100644 --- a/commands/create_role_button.go +++ b/commands/create_role_button.go @@ -61,6 +61,8 @@ var CreateRoleButtonCommand = discord.SlashCommandCreate{ } func CreateRoleButtonHandler(e *handler.CommandEvent) error { + utils.LogInteraction("create-role-button", e) + if e.GuildID() == nil { slog.Warn("Received create role button command in DMs or guild ID is otherwise nil") return ErrEventNoGuildID diff --git a/commands/gatekeep.go b/commands/gatekeep.go index 0c29c29..30996f1 100644 --- a/commands/gatekeep.go +++ b/commands/gatekeep.go @@ -37,8 +37,8 @@ var ApproveSlashCommand = discord.SlashCommandCreate{ } func ApproveUserCommandHandler(e *handler.CommandEvent) error { - slog.Info("`Approve` user command called.", - "guild_id", utils.Iif(e.GuildID() == nil, "", e.GuildID().String())) + utils.LogInteraction("approve", e) + guild, inGuild := e.Guild() if !inGuild { return nil @@ -49,8 +49,8 @@ func ApproveUserCommandHandler(e *handler.CommandEvent) error { } func ApproveSlashCommandHandler(e *handler.CommandEvent) error { - slog.Info("`approve` slash command called.", - "guild_id", utils.Iif(e.GuildID() == nil, "", e.GuildID().String())) + utils.LogInteraction("Approve", e) + guild, inGuild := e.Guild() if !inGuild { return nil @@ -61,6 +61,7 @@ func ApproveSlashCommandHandler(e *handler.CommandEvent) error { } func approvedInnerHandler(e *handler.CommandEvent, guild discord.Guild, member discord.ResolvedMember) error { + slog.Debug("Entered approvedInnerHandler") _ = e.DeferCreateMessage(true) guildSettings, err := model.GetGuildSettings(guild.ID) if err != nil { diff --git a/commands/infractions.go b/commands/infractions.go index f36736f..55b12ca 100644 --- a/commands/infractions.go +++ b/commands/infractions.go @@ -91,6 +91,8 @@ var WarnCommand = discord.SlashCommandCreate{ } func WarnHandler(e *handler.CommandEvent) error { + utils.LogInteraction("warn", e) + ctx := context.Background() data := e.SlashCommandInteractionData() @@ -210,6 +212,8 @@ var UserInfractionsCommand = discord.SlashCommandCreate{ } func UserInfractionsHandler(e *handler.CommandEvent) error { + utils.LogInteraction("warnings", e) + user := e.User() guild, ok := e.Guild() if !ok { @@ -226,6 +230,7 @@ func UserInfractionsHandler(e *handler.CommandEvent) error { } func UserInfractionButtonHandler(e *handler.ComponentEvent) error { + utils.LogInteraction("warnings button", e) offsetStr := e.Variables["offset"] offset, err := strconv.Atoi(offsetStr) if err != nil { @@ -329,6 +334,8 @@ var InfractionsCommand = discord.SlashCommandCreate{ // InfractionsListHandler handles the `/infractions list` command. func InfractionsListHandler(e *handler.CommandEvent) error { + utils.LogInteraction("infractions list", e) + slog.Info("interaction `/infractions list` called.") data := e.SlashCommandInteractionData() user, hasUser := data.OptUser("user") @@ -383,6 +390,8 @@ func InfractionsListHandler(e *handler.CommandEvent) error { } func InfractionsRemoveHandler(e *handler.CommandEvent) error { + utils.LogInteraction("infractions remove", e) + data := e.SlashCommandInteractionData() infID := data.String("infraction-id") guild, ok := e.Guild() @@ -406,6 +415,8 @@ func InfractionsRemoveHandler(e *handler.CommandEvent) error { } func InfractionsListComponentHandler(e *handler.ComponentEvent) error { + utils.LogInteraction("infractions list component", e) + parentIx := e.Message.Interaction if parentIx == nil { return fmt.Errorf("no parent interaction found") diff --git a/commands/kick.go b/commands/kick.go index b962823..9fcdcae 100644 --- a/commands/kick.go +++ b/commands/kick.go @@ -7,6 +7,7 @@ import ( "github.com/disgoorg/disgo/handler" "github.com/disgoorg/disgo/rest" "github.com/disgoorg/json" + "github.com/myrkvi/heimdallr/utils" ) @@ -36,6 +37,8 @@ var KickCommand = discord.SlashCommandCreate{ } func KickWithMessageHandler(e *handler.CommandEvent) error { + utils.LogInteraction("kick with-message", e) + data := e.SlashCommandInteractionData() guild, isGuild := e.Guild() if !isGuild { diff --git a/commands/quote.go b/commands/quote.go index 39d55ce..f6cf460 100644 --- a/commands/quote.go +++ b/commands/quote.go @@ -60,6 +60,8 @@ var QuoteCommand = discord.SlashCommandCreate{ } func QuoteHandler(e *handler.CommandEvent) error { + utils.LogInteraction("quote", e) + var guildID snowflake.ID if e.GuildID() != nil { guildID = *e.GuildID() diff --git a/utils/interaction_log.go b/utils/interaction_log.go new file mode 100644 index 0000000..5f06a50 --- /dev/null +++ b/utils/interaction_log.go @@ -0,0 +1,31 @@ +package utils + +import ( + "fmt" + "log/slog" + "time" + + "github.com/disgoorg/disgo/discord" +) + +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" + } + + slog.Info(fmt.Sprintf("Interaction %s (%s) received", interactionName, type_), + "user_id", interaction.User().ID, + "guild_id", interaction.GuildID(), + "channel_id", interaction.ChannelID(), + "delay", delay, + ) +}