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 +} 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"} 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 +}