From 2a8664eec8cf5c88176dea81655bffa9bbd26aad Mon Sep 17 00:00:00 2001 From: Oscar Cao Date: Mon, 30 Sep 2024 20:37:41 +0100 Subject: [PATCH 1/4] Changed verify.go to accept both slash and interaction based verification --- pkg/commands/verify.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/commands/verify.go b/pkg/commands/verify.go index 3720f94..f6d1ac4 100644 --- a/pkg/commands/verify.go +++ b/pkg/commands/verify.go @@ -41,8 +41,16 @@ func VerifyCommand(ctx context.Context, s *discordgo.Session, i *discordgo.Inter } // encode the userID, guildID, ( welcome channel, logging channel, and roles to give to verified user ) + var customID string + + if i.Type == discordgo.InteractionApplicationCommand { + customID = i.ApplicationCommandData().Name // slash command + } else { + customID = i.MessageComponentData().CustomID[2:] // interaction + } + encoded, err := utils.Encrypt( - fmt.Sprintf("%s.%s.%s", user.ID, guild.ID, i.MessageComponentData().CustomID[2:]), []byte(viper.GetString("api.secret")), + fmt.Sprintf("%s.%s.%s", user.ID, guild.ID, customID), []byte(viper.GetString("api.secret")), ) if err != nil { return &interactionError{err, "Failed to encrypt user info digest"} From aeb5f9b637751382f9a4d87a52ca8c96dc1667ea Mon Sep 17 00:00:00 2001 From: Oscar Cao Date: Wed, 2 Oct 2024 23:31:41 +0100 Subject: [PATCH 2/4] added utility to return current day, month, year as integer values --- pkg/utils/time.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 pkg/utils/time.go diff --git a/pkg/utils/time.go b/pkg/utils/time.go new file mode 100644 index 0000000..667f99b --- /dev/null +++ b/pkg/utils/time.go @@ -0,0 +1,18 @@ +package utils + +import ( + "time" +) + +// GetTime looks at current time and returns day, month, year as Integers +func GetTime() (int, int, int) { + currentTime := time.Now() + + parsedTime, _ := time.Parse("02-01-2006", currentTime.Format("02-01-2006")) + + day := parsedTime.Day() + month := int(parsedTime.Month()) + year := parsedTime.Year() + + return day, month, year +} From d374db4fb0ba735b7a7e241c2a4d176158710f85 Mon Sep 17 00:00:00 2001 From: Oscar Cao Date: Wed, 2 Oct 2024 23:40:31 +0100 Subject: [PATCH 3/4] added createSignUpsRole() which automatically creates signup roles as well as changes addRoles() to add the signup roles --- internal/api/verify.go | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/api/verify.go b/internal/api/verify.go index 0e272df..fef5aaa 100644 --- a/internal/api/verify.go +++ b/internal/api/verify.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" "net/http" + "strconv" "strings" "github.com/Strum355/log" @@ -133,6 +134,19 @@ func addRoles(s *discordgo.Session, gid, uid string, rid []string) error { if err := s.GuildMemberRoleAdd(gid, uid, roleID); err != nil { return err } + + signUpName, err := createSignUpsRole(s, gid) + if err != nil { + return err + } + signUpID := utils.GetRoleIDFromName(roles, signUpName) + if signUpID == "" { + return errors.New("no role called: " + signUpName) + } + if err := s.GuildMemberRoleAdd(gid, uid, signUpID); err != nil { + return err + } + for _, additionalRoleID := range rid { if err := s.GuildMemberRoleAdd(gid, uid, additionalRoleID); err != nil { return err @@ -160,3 +174,34 @@ func dataFromState(state string) (uid string, gid string, announce_cid string, l } return } + +func createSignUpsRole(s *discordgo.Session, gid string) (signUpName string, err error) { + roles, err := s.GuildRoles(gid) + if err != nil { + return "", err + } + + _, month, year := utils.GetTime() + + roleName := "Signups " + if month < 9 { + roleName += strconv.Itoa(year-1) + "/" + strconv.Itoa(year) + } else { + roleName += strconv.Itoa(year) + "/" + strconv.Itoa(year+1) + } + + roleID := utils.GetRoleIDFromName(roles, roleName) + color := 0 // Grey + permissions := int64(0) // No permissions + if roleID == "" { + _, err := s.GuildRoleCreate(gid, &discordgo.RoleParams{ + Name: roleName, + Color: &color, + Permissions: &permissions, + }) + if err != nil { + return "", err + } + } + return roleName, nil +} From b1a4c955b82f3a16229d6c04affc4ddf5349e73a Mon Sep 17 00:00:00 2001 From: Oscar Cao Date: Sun, 3 Nov 2024 19:49:33 +0000 Subject: [PATCH 4/4] refactored code to add signup roles to log in discord as well --- internal/api/verify.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/internal/api/verify.go b/internal/api/verify.go index fef5aaa..045b345 100644 --- a/internal/api/verify.go +++ b/internal/api/verify.go @@ -42,6 +42,25 @@ func createVerifyHandler(s *discordgo.Session) gin.HandlerFunc { utils.SendLogMessage(s, logging_cid, "Failed to get user `"+uid+"` from Discord API to add roles.") return } + allRoles, err := s.GuildRoles(gid) + if err != nil { + resultTemplate.Execute(c.Writer, AccessErrorResponse(http.StatusInternalServerError, "Failed to query roles", err)) + utils.SendLogMessage(s, logging_cid, "Failed to query discord API for roles.") + return + } + signUpName, err := createSignUpsRole(s, gid) + if err != nil { + resultTemplate.Execute(c.Writer, AccessErrorResponse(http.StatusInternalServerError, "Failed creating signup role", err)) + utils.SendLogMessage(s, logging_cid, "Failed to create signup roles.") + return + } + signUpID := utils.GetRoleIDFromName(allRoles, signUpName) + if signUpID == "" { + resultTemplate.Execute(c.Writer, AccessErrorResponse(http.StatusInternalServerError, "Failed getting role id", err)) + utils.SendLogMessage(s, logging_cid, "Failed to get signup role id.") + return + } + rid = append(rid, signUpID) if err := addRoles(s, gid, uid, rid); err != nil { resultTemplate.Execute(c.Writer, @@ -135,18 +154,6 @@ func addRoles(s *discordgo.Session, gid, uid string, rid []string) error { return err } - signUpName, err := createSignUpsRole(s, gid) - if err != nil { - return err - } - signUpID := utils.GetRoleIDFromName(roles, signUpName) - if signUpID == "" { - return errors.New("no role called: " + signUpName) - } - if err := s.GuildMemberRoleAdd(gid, uid, signUpID); err != nil { - return err - } - for _, additionalRoleID := range rid { if err := s.GuildMemberRoleAdd(gid, uid, additionalRoleID); err != nil { return err