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

Changed verify.go to accept both slash and interaction based verification #7

Open
wants to merge 3 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
45 changes: 45 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 @@ -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
Expand Down Expand Up @@ -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 "
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pull this from a viper config in future.

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"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can pull the format from a viper config in future.


day := parsedTime.Day()
month := int(parsedTime.Month())
year := parsedTime.Year()

return day, month, year
}