/
user_permission.go
61 lines (48 loc) · 2.09 KB
/
user_permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package models
import (
"fmt"
"github.com/bwmarrin/discordgo"
. "github.com/instance-id/GoVerifier-dgo/data"
. "github.com/instance-id/GoVerifier-dgo/utils"
)
type PermissionDataAccessObject struct{}
type AvailableRoles struct {
Roles discordgo.Roles
}
var PermissionDAO *PermissionDataAccessObject
func (p *PermissionDataAccessObject) AddRoles(lc LocalContext, asset string) (bool, string) {
member, err := lc.Ctx.Ses.GuildMember(lc.Di.Discord.Guild, lc.Ctx.Msg.Author.ID)
hasError := LogErrorRet(fmt.Sprintf("Could not retrieve member: %s", err), err)
if hasError {
return hasError, fmt.Sprintf("Could not retrieve member: %s", err)
}
primaryRole := Dac.Discord.Roles["Verified"]
assetRole := Dac.Discord.Roles[asset]
hasPrimary := CheckRoleExists(member, primaryRole)
Log.Infof("PrimaryRole: %s, AssetRole: %s, GuildId: %s, AuthorID: %s ", primaryRole, assetRole, lc.Di.Discord.Guild, lc.Ctx.Msg.Author.ID)
if !hasPrimary {
err := lc.Ctx.Ses.GuildMemberRoleAdd(lc.Di.Discord.Guild, lc.Ctx.Msg.Author.ID, primaryRole)
LogErrorf(fmt.Sprintf("Could not add primary role to user: %s : %s", lc.Ctx.Msg.Author.Username, err), err)
hasError := LogErrorRet(fmt.Sprintf("Could not add primary role to user: %s : %s", lc.Ctx.Msg.Author.Username, err), err)
if hasError {
return hasError, fmt.Sprintf("Could not add primary role to user: %s", lc.Ctx.Msg.Author.Username)
}
}
err = lc.Ctx.Ses.GuildMemberRoleAdd(lc.Di.Discord.Guild, lc.Ctx.Msg.Author.ID, assetRole)
LogErrorRet(fmt.Sprintf("Could not add asset role to user: %s : %s", lc.Ctx.Msg.Author.ID, err), err)
hasError = LogErrorRet(fmt.Sprintf("Could not add asset role to user: %s : %s", lc.Ctx.Msg.Author.ID, err), err)
if hasError {
return hasError, fmt.Sprintf("Could not add asset role to user: %s", lc.Ctx.Msg.Author.Username)
}
return false, fmt.Sprintf("Permissions have been successfully applied to user: %s", lc.Ctx.Msg.Author.Username)
}
func CheckRoleExists(member *discordgo.Member, role string) bool {
return func() bool {
for _, v := range member.Roles {
if v == role {
return true
}
}
return false
}()
}