-
Notifications
You must be signed in to change notification settings - Fork 8
/
commands_roles.go
195 lines (175 loc) · 4.67 KB
/
commands_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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
DiscordConsole is a software aiming to give you full control over accounts, bots and webhooks!
Copyright (C) 2020 Mnpn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"io"
"sort"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/jD91mZM2/gtable"
"github.com/jD91mZM2/stdutil"
)
func commandsRoles(session *discordgo.Session, cmd string, args []string, nargs int, w io.Writer) (returnVal string) {
switch cmd {
case "role":
if nargs < 1 {
stdutil.PrintErr("role list/add/rem/create/edit/delete", nil)
return
}
switch strings.ToLower(args[0]) {
case "list":
if loc.guild == nil {
stdutil.PrintErr(tl("invalid.guild"), nil)
return
}
roles, err := session.GuildRoles(loc.guild.ID)
if err != nil {
stdutil.PrintErr(tl("failed.roles"), err)
return
}
sort.Slice(roles, func(i, j int) bool {
return roles[i].Position > roles[j].Position
})
table := gtable.NewStringTable()
table.AddStrings("ID", "Name", "Permissions", "Color")
for _, role := range roles {
table.AddRow()
table.AddStrings(role.ID, role.Name, strconv.Itoa(role.Permissions), strconv.Itoa(role.Color))
}
writeln(w, table.String())
case "add":
fallthrough
case "rem":
if nargs < 3 {
stdutil.PrintErr("role add/rem <user id> <role id>", nil)
return
}
if loc.guild == nil {
stdutil.PrintErr(tl("invalid.guild"), nil)
return
}
var err error
if args[0] == "add" {
err = session.GuildMemberRoleAdd(loc.guild.ID, args[1], args[2])
} else {
err = session.GuildMemberRoleRemove(loc.guild.ID, args[1], args[2])
}
if err != nil {
stdutil.PrintErr(tl("failed.role.change"), err)
}
case "create":
if loc.guild == nil {
stdutil.PrintErr(tl("invalid.guild"), nil)
return
}
role, err := session.GuildRoleCreate(loc.guild.ID)
if err != nil {
stdutil.PrintErr(tl("failed.role.create"), err)
return
}
writeln(w, "Created role with ID "+role.ID)
lastUsedRole = role.ID
returnVal = role.ID
case "edit":
if nargs < 4 {
stdutil.PrintErr("role edit <role id> <flag> <value>", nil)
return
}
if loc.guild == nil {
stdutil.PrintErr(tl("invalid.guild"), nil)
return
}
value := strings.Join(args[3:], " ")
roles, err := session.GuildRoles(loc.guild.ID)
if err != nil {
stdutil.PrintErr(tl("failed.roles"), err)
return
}
var role *discordgo.Role
for _, r := range roles {
if r.ID == args[1] {
role = r
break
}
}
if role == nil {
stdutil.PrintErr(tl("invalid.role"), nil)
return
}
name := role.Name
color := int64(role.Color)
hoist := role.Hoist
perms := role.Permissions
mention := role.Mentionable
switch strings.ToLower(args[2]) {
case "name":
name = value
case "color":
value = strings.TrimPrefix(value, "#")
color, err = strconv.ParseInt(value, 16, 0)
if err != nil {
stdutil.PrintErr(tl("invalid.number"), nil)
return
}
case "separate":
hoist, err = parseBool(value)
if err != nil {
stdutil.PrintErr(err.Error(), nil)
return
}
case "perms":
perms, err = strconv.Atoi(value)
if err != nil {
stdutil.PrintErr(tl("invalid.number"), nil)
return
}
case "mention":
mention, err = parseBool(value)
if err != nil {
stdutil.PrintErr(err.Error(), nil)
return
}
default:
stdutil.PrintErr(tl("invalid.value")+": "+args[2], nil)
return
}
role, err = session.GuildRoleEdit(loc.guild.ID, args[1], name, int(color), hoist, perms, mention)
if err != nil {
stdutil.PrintErr(tl("failed.role.edit"), err)
return
}
lastUsedRole = role.ID
writeln(w, "Edited role "+role.ID)
case "delete":
if nargs < 2 {
stdutil.PrintErr("role delete <role id>", nil)
return
}
if loc.guild == nil {
stdutil.PrintErr(tl("invalid.guild"), nil)
return
}
err := session.GuildRoleDelete(loc.guild.ID, args[1])
if err != nil {
stdutil.PrintErr(tl("failed.role.delete"), err)
}
default:
stdutil.PrintErr("role list/add/rem/create/edit/delete", nil)
return
}
}
return
}