Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log audit log to text channel #5

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions commands/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ var AdminCommand = discord.SlashCommandCreate{
Required: false,
ChannelTypes: []discord.ChannelType{discord.ChannelTypeGuildText},
},
discord.ApplicationCommandOptionChannel{
Name: "audit-log-channel",
Description: "The channel to set as the audit log channel",
Required: false,
ChannelTypes: []discord.ChannelType{discord.ChannelTypeGuildText},
},
},
},

Expand Down Expand Up @@ -164,9 +170,19 @@ func AdminShowAllButtonHandler(e *handler.ComponentEvent) error {
}

func modChannelInfo(settings *model.GuildSettings) string {
modChannelInfo := "> This is the channel in which notifications and other information for moderators and administrators are sent."
return fmt.Sprintf("**Moderator channel:** <#%d>\n%s",
settings.ModeratorChannel, modChannelInfo)
modChannelInfoHelp := "> This is the channel in which notifications and other information for moderators and administrators are sent."
modChannelInfo := fmt.Sprintf("**Moderator channel:** %s\n%s",
utils.Iif(settings.ModeratorChannel == 0, "not set",
fmt.Sprintf("<#%d>", settings.ModeratorChannel)),
modChannelInfoHelp)

auditLogChannelInfoHelp := "> This is the channel in which audit logs are sent."
auditLogChannelInfo := fmt.Sprintf("**Audit log channel:** %s\n%s",
utils.Iif(settings.AuditLogChannel == 0, "not set",
fmt.Sprintf("<#%d>", settings.AuditLogChannel)),
auditLogChannelInfoHelp)

return fmt.Sprintf("%s\n\n%s", modChannelInfo, auditLogChannelInfo)
}

func infractionInfo(settings *model.GuildSettings) string {
Expand Down Expand Up @@ -235,28 +251,40 @@ func AdminModChannelHandler(e *handler.CommandEvent) error {
return ErrEventNoGuildID
}

channel, hasChannel := data.OptChannel("channel")
modChannel, hasModChannel := data.OptChannel("channel")
auditLogChannel, hasAuditLogChannel := data.OptChannel("audit-log-channel")

settings, err := model.GetGuildSettings(guild.ID)
if err != nil {
return err
}

if !hasChannel {
if !hasModChannel && !hasAuditLogChannel {
return e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(modChannelInfo(settings)).
SetEphemeral(true).
SetAllowedMentions(&discord.AllowedMentions{}).
Build())
}

settings.ModeratorChannel = channel.ID
msg := ""

if hasModChannel {
settings.ModeratorChannel = modChannel.ID
msg += fmt.Sprintf("Moderator channel set to <#%d>\n", modChannel.ID)
}
if hasAuditLogChannel {
settings.AuditLogChannel = auditLogChannel.ID
msg += fmt.Sprintf("Audit log channel set to <#%d>\n", auditLogChannel.ID)
}

err = model.SetGuildSettings(settings)
if err != nil {
return err
}

return e.CreateMessage(discord.NewMessageCreateBuilder().
SetContentf("Moderator channel set to <#%d>", channel.ID).
SetContent(msg).
SetEphemeral(true).
SetAllowedMentions(&discord.AllowedMentions{}).
Build())
Expand Down
6 changes: 3 additions & 3 deletions commands/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func UserInfractionsHandler(e *handler.CommandEvent) error {
}

func UserInfractionButtonHandler(e *handler.ComponentEvent) error {
offsetStr := e.Variables["offset"]
offsetStr := e.Vars["offset"]
offset, err := strconv.Atoi(offsetStr)
if err != nil {
return fmt.Errorf("failed to parse offset: %w", err)
Expand Down Expand Up @@ -415,11 +415,11 @@ func InfractionsListComponentHandler(e *handler.ComponentEvent) error {
if !isGuild {
return ErrEventNoGuildID
}
offset, err := strconv.Atoi(e.Variables["offset"])
offset, err := strconv.Atoi(e.Vars["offset"])
if err != nil {
return fmt.Errorf("failed to parse offset: %w", err)
}
userID, err := snowflake.Parse(e.Variables["userID"])
userID, err := snowflake.Parse(e.Vars["userID"])
if err != nil {
return fmt.Errorf("failed to parse user id: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions components/role_assign_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func RoleAssignButtonHandler(e *handler.ComponentEvent) error {
"user",
e.User().ID,
"roleID",
e.Variables["roleID"],
e.Vars["roleID"],
)

roleID, err := snowflake.Parse(e.Variables["roleID"])
roleID, err := snowflake.Parse(e.Vars["roleID"])
if err != nil {
slog.Warn("Failed to parse roleID", "roleID", e.Variables["roleID"], "err", err)
slog.Warn("Failed to parse roleID", "roleID", e.Vars["roleID"], "err", err)
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.22.0

require (
github.com/cbroglie/mustache v1.4.0
github.com/disgoorg/disgo v0.17.2
github.com/disgoorg/disgo v0.18.10-0.20240811153006-637a92bd7191
github.com/disgoorg/json v1.1.0
github.com/disgoorg/snowflake/v2 v2.0.1
github.com/disgoorg/snowflake/v2 v2.0.3
github.com/glebarez/sqlite v1.10.0
github.com/spf13/viper v1.18.2
github.com/sqids/sqids-go v0.4.1
Expand All @@ -19,7 +19,7 @@ require (
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand All @@ -40,12 +40,12 @@ require (
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/disgo v0.17.2 h1:RxiLq8guMtk+9tArFwve02iya2APQ9yZVtV30ySKNtw=
github.com/disgoorg/disgo v0.17.2/go.mod h1:8r3h9fXSz7BbACxLPsPbtB6LX8gaQFUETgPKV/0gAKQ=
github.com/disgoorg/disgo v0.18.9 h1:Ey1HYrCvdFtxNj8QQE+13wgj7/JTBvf4rXOQcvnBftA=
github.com/disgoorg/disgo v0.18.9/go.mod h1:TN4tM8VaOaZebO+Ek3686RIATCL32uqPGSNdUYP8yVI=
github.com/disgoorg/disgo v0.18.10-0.20240811153006-637a92bd7191 h1:+h2mcpl5OyNfvUtSdtnRPPPsIzzYx6iPcEGqmjhL1i8=
github.com/disgoorg/disgo v0.18.10-0.20240811153006-637a92bd7191/go.mod h1:TN4tM8VaOaZebO+Ek3686RIATCL32uqPGSNdUYP8yVI=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/disgoorg/snowflake/v2 v2.0.3 h1:3B+PpFjr7j4ad7oeJu4RlQ+nYOTadsKapJIzgvSI2Ro=
github.com/disgoorg/snowflake/v2 v2.0.3/go.mod h1:W6r7NUA7DwfZLwr00km6G4UnZ0zcoLBRufhkFWgAc4c=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand All @@ -28,6 +34,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
Expand Down Expand Up @@ -90,6 +98,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
Expand All @@ -99,10 +109,15 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
Loading