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

Refactored Sign ups #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"html/template"
"net/http"
"strconv"
"strings"

"github.com/Strum355/log"
Expand Down Expand Up @@ -41,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,
Expand Down Expand Up @@ -133,6 +153,7 @@ func addRoles(s *discordgo.Session, gid, uid string, rid []string) error {
if err := s.GuildMemberRoleAdd(gid, uid, roleID); err != nil {
return err
}

for _, additionalRoleID := range rid {
if err := s.GuildMemberRoleAdd(gid, uid, additionalRoleID); err != nil {
return err
Expand Down Expand Up @@ -160,3 +181,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
}
10 changes: 9 additions & 1 deletion pkg/commands/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/time.go
Original file line number Diff line number Diff line change
@@ -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
}