This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rolesUtils.go
139 lines (129 loc) · 3.84 KB
/
rolesUtils.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"errors"
"log"
"github.com/bwmarrin/discordgo"
)
func getRoleID(guildID string, roleName string) (string, error) {
guild, err := session.Guild(guildID)
if err != nil {
log.Println("getRoleID session.Guild(" + guildID + ") " + err.Error())
return "", err
}
roles := guild.Roles
for _, role := range roles {
if role.Name == roleName {
return role.ID, nil
}
}
return "", errors.New("no " + roleName + " role available")
}
func hasRole(member *discordgo.Member, roleName, guildID string) bool {
//z jakiegos powodu w strukturze member GuildID jest puste...
adminRole, err := getRoleID(guildID, roleName)
if err != nil {
log.Println("hasRole getRoleID(" + guildID + ", " + roleName + ") " + err.Error())
return false
}
for _, role := range member.Roles {
if role == adminRole {
return true
}
}
return false
}
func hasPermission(member *discordgo.Member, guildID string, permission int) bool {
for _, roleID := range member.Roles {
role, err := session.State.Role(guildID, roleID)
if err != nil {
log.Println("hasPermisson session.State.Role(" + guildID + ", " + roleID + ") " + err.Error())
return false
}
if role.Permissions&permission != 0 {
return true
}
}
return false
}
func hasAdminPermissions(member *discordgo.Member, guildID string) bool {
if hasRole(member, getAdminRoleForGuild(guildID), guildID) || hasPermission(member, guildID, 8) { // 8 - administrator
return true
}
return false
}
func getAdminRoleForGuild(guildID string) string {
var serverConfig ServerConfig
err := DbMap.SelectOne(&serverConfig, "SELECT * FROM ServerConfig")
if err != nil {
log.Println("getAdminRoleForGuild(" + guildID + ") " + err.Error())
return ""
}
return serverConfig.AdminRole
}
func getSavedRoles(guildId, memberId string) ([]string, error) {
var memberRoles []MemberRole
_, err := DbMap.Select(&memberRoles, "SELECT * FROM MemberRoles WHERE guild_id = ? AND member_id = ?", guildId, memberId)
if err != nil {
return nil, err
}
var ret []string
for _, role := range memberRoles {
ret = append(ret, role.RoleId)
}
return ret, nil
}
func updateMemberSavedRoles(member *discordgo.Member, guildId string) {
savedRoles, err := getSavedRoles(guildId, member.User.ID)
if err != nil {
log.Println("updateMemberSavedRoles error getting saved roles " + err.Error())
return
}
for _, memberRole := range member.Roles {
found := false
for i, savedRole := range savedRoles {
if savedRole == memberRole {
found = true
savedRoles[i] = ""
break
}
}
if !found {
memberrole := MemberRole{GuildId: guildId, RoleId: memberRole, MemberId: member.User.ID}
err = DbMap.Insert(&memberrole)
if err != nil {
log.Println("updateAllMembersSavedRoles error saving new role info " + err.Error())
continue
}
}
}
for _, savedRole := range savedRoles {
if savedRole != "" {
_, err = DbMap.Exec("DELETE FROM MemberRoles WHERE guild_id = ? AND role_id = ? AND member_id = ?", guildId, savedRole, member.User.ID)
if err != nil {
log.Println("updateAllMembersSavedRoles error deleting info about member role " + err.Error())
continue
}
}
}
}
func restoreMemberRoles(member *discordgo.Member, guildId string) {
var memberRoles []MemberRole
_, err := DbMap.Select(&memberRoles, "SELECT * FROM MemberRoles WHERE guild_id = ? AND member_id = ?", guildId, member.User.ID)
if err != nil {
log.Println("restoreMemberRoles error getting saved roles " + err.Error())
return
}
for _, role := range memberRoles {
err = session.GuildMemberRoleAdd(guildId, member.User.ID, role.RoleId)
if err != nil {
log.Println("restoreMemberRoles error restoring member role " + err.Error())
continue
}
}
}
func updateAllMembersSavedRoles(guildId string) {
guildMembers := getAllMembers(guildId)
for _, member := range guildMembers {
updateMemberSavedRoles(member, guildId)
}
}