Skip to content

Commit

Permalink
feature: Logging various interactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
myrkvi committed Aug 21, 2024
1 parent 52cce47 commit 46afabd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 4 deletions.
25 changes: 25 additions & 0 deletions commands/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...).
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions commands/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions commands/create_role_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions commands/gatekeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<null>", e.GuildID().String()))
utils.LogInteraction("approve", e)

guild, inGuild := e.Guild()
if !inGuild {
return nil
Expand All @@ -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, "<null>", e.GuildID().String()))
utils.LogInteraction("Approve", e)

guild, inGuild := e.Guild()
if !inGuild {
return nil
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions commands/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ var WarnCommand = discord.SlashCommandCreate{
}

func WarnHandler(e *handler.CommandEvent) error {
utils.LogInteraction("warn", e)

ctx := context.Background()
data := e.SlashCommandInteractionData()

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions commands/kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/json"

"github.com/myrkvi/heimdallr/utils"
)

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions commands/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions utils/interaction_log.go
Original file line number Diff line number Diff line change
@@ -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(),

Check failure on line 28 in utils/interaction_log.go

View workflow job for this annotation

GitHub Actions / GolangCi-Lint

SA1019: interaction.ChannelID is deprecated: Use Interaction.Channel instead (staticcheck)
"delay", delay,
)
}

0 comments on commit 46afabd

Please sign in to comment.