/
roles.go
84 lines (67 loc) · 1.71 KB
/
roles.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
package handlers
import (
"errors"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/unswpcsoc/pcsocgo/commands"
"github.com/unswpcsoc/pcsocgo/internal/utils"
)
type role struct {
nilCommand
names []string
desc string
rol string
}
func newRole(rol string) *role {
return &role{
names: []string{strings.ToLower(rol)},
desc: "Gives user the " + rol + " role.",
rol: strings.ToLower(rol),
}
}
func (r *role) Aliases() []string { return r.names }
func (r *role) Desc() string { return r.desc }
func (r *role) MsgHandle(ses *discordgo.Session, msg *discordgo.Message) (*commands.CommandSend, error) {
var err error
mem, err := ses.State.Member(msg.GuildID, msg.Author.ID)
if err != nil {
mem, err = ses.GuildMember(msg.GuildID, msg.Author.ID)
if err != nil {
return nil, err
}
}
// Check if user has role
roleID := ""
has := false
guildroles, err := ses.GuildRoles(msg.GuildID)
if err != nil {
return nil, err
}
for _, role := range guildroles {
if strings.ToLower(r.rol) == strings.ToLower(role.Name) {
roleID = role.ID
break
}
}
if len(roleID) == 0 {
return nil, errors.New("no such role " + utils.Code(r.rol) + " in guild: " + msg.GuildID + "\n")
}
for _, role := range mem.Roles {
if roleID == role {
has = true
}
}
// Add / remove the role accordingly
if has {
err = ses.GuildMemberRoleRemove(msg.GuildID, msg.Author.ID, roleID)
if err != nil {
return nil, err
}
return commands.NewSimpleSend(msg.ChannelID, mem.Mention()+" is no longer a "+r.rol), nil
}
err = ses.GuildMemberRoleAdd(msg.GuildID, msg.Author.ID, roleID)
if err != nil {
return nil, err
}
return commands.NewSimpleSend(msg.ChannelID, mem.Mention()+" is now a "+r.rol), nil
}