forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.go
361 lines (312 loc) · 9.53 KB
/
role.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package disgord
import (
"encoding/json"
"errors"
"net/http"
"github.com/andersfylling/disgord/constant"
"github.com/andersfylling/disgord/endpoint"
"github.com/andersfylling/disgord/httd"
)
// NewRole ...
func NewRole() *Role {
return &Role{}
}
// Role https://discordapp.com/developers/docs/topics/permissions#role-object
type Role struct {
Lockable `json:"-"`
ID Snowflake `json:"id"`
Name string `json:"name"`
Color uint `json:"color"`
Hoist bool `json:"hoist"`
Position uint `json:"position"`
Permissions uint64 `json:"permissions"`
Managed bool `json:"managed"`
Mentionable bool `json:"mentionable"`
guildID Snowflake
}
// Mention gives a formatted version of the role such that it can be parsed by Discord clients
func (r *Role) Mention() string {
return "<@&" + r.ID.String() + ">"
}
// SetGuildID link role to a guild before running session.SaveToDiscord(*Role)
func (r *Role) SetGuildID(id Snowflake) {
r.ID = id
}
// DeepCopy see interface at struct.go#DeepCopier
func (r *Role) DeepCopy() (copy interface{}) {
copy = NewRole()
r.CopyOverTo(copy)
return
}
// CopyOverTo see interface at struct.go#Copier
func (r *Role) CopyOverTo(other interface{}) (err error) {
var ok bool
var role *Role
if role, ok = other.(*Role); !ok {
return newErrorUnsupportedType("given interface{} was not a *Role")
}
if constant.LockedMethods {
r.RLock()
role.Lock()
}
role.ID = r.ID
role.Name = r.Name
role.Color = r.Color
role.Hoist = r.Hoist
role.Position = r.Position
role.Permissions = r.Permissions
role.Managed = r.Managed
role.Mentionable = r.Mentionable
role.guildID = r.guildID
if constant.LockedMethods {
r.RUnlock()
role.Unlock()
}
return
}
func (r *Role) saveToDiscord(session Session, changes discordSaver) (err error) {
if r.guildID.Empty() {
err = newErrorMissingSnowflake("role has no guildID")
return
}
var role *Role
if r.ID.Empty() {
// create role
params := CreateGuildRoleParams{
Name: r.Name,
Permissions: r.Permissions,
Color: r.Color,
Hoist: r.Hoist,
Mentionable: r.Mentionable,
}
role, err = session.CreateGuildRole(r.guildID, ¶ms)
if err != nil {
return
}
err = role.CopyOverTo(r)
return err
}
return errors.New("updating discord objects are not yet implemented - only saving new ones")
//
//// modify/update role
//params := ModifyGuildRoleParams{}
//params.SetName(r.Name)
//params.SetPermissions(r.Permissions)
//params.SetColor(r.Color)
//params.SetHoist(r.Hoist)
//params.SetMentionable(r.Mentionable)
//role, err = session.ModifyGuildRole(r.guildID, r.ID, ¶ms)
//if err != nil {
// return
//}
//if role.Position != r.Position {
// // update the position
// params := []ModifyGuildRolePositionsParams{{
// ID: r.ID,
// Position: r.Position,
// }}
// _, err = session.ModifyGuildRolePositions(r.guildID, params)
// if err != nil {
// return
// }
// role.Position = r.Position
//}
//
//return
}
func (r *Role) deleteFromDiscord(session Session) (err error) {
if r.ID.Empty() {
err = newErrorMissingSnowflake("role has no ID")
return
}
if r.guildID.Empty() {
err = newErrorMissingSnowflake("role has no guildID")
return
}
err = session.DeleteGuildRole(r.guildID, r.ID)
return
}
//func (r *Role) Clear() {
//}
// GetGuildRoles [REST] Returns a list of role objects for the guild.
// Method GET
// Endpoint /guilds/{guild.id}/roles
// Rate limiter /guilds/{guild.id}/roles
// Discord documentation https://discordapp.com/developers/docs/resources/guild#get-guild-roles
// Reviewed 2018-08-18
// Comment -
func GetGuildRoles(client httd.Getter, guildID Snowflake) (ret []*Role, err error) {
details := &httd.Request{
Ratelimiter: ratelimitGuildRoles(guildID),
Endpoint: "/guilds/" + guildID.String() + "/roles",
}
_, body, err := client.Get(details)
if err != nil {
return
}
err = unmarshal(body, &ret)
if err != nil {
return
}
// add guild id to roles
for _, role := range ret {
role.guildID = guildID
}
return
}
// CreateGuildRoleParams ...
// https://discordapp.com/developers/docs/resources/guild#create-guild-role-json-params
type CreateGuildRoleParams struct {
Name string `json:"name,omitempty"`
Permissions uint64 `json:"permissions,omitempty"`
Color uint `json:"color,omitempty"`
Hoist bool `json:"hoist,omitempty"`
Mentionable bool `json:"mentionable,omitempty"`
}
// CreateGuildRole [REST] Create a new role for the guild. Requires the 'MANAGE_ROLES' permission.
// Returns the new role object on success. Fires a Guild Role Create Gateway event.
// Method POST
// Endpoint /guilds/{guild.id}/roles
// Rate limiter /guilds/{guild.id}/roles
// Discord documentation https://discordapp.com/developers/docs/resources/guild#create-guild-role
// Reviewed 2018-08-18
// Comment All JSON params are optional.
func CreateGuildRole(client httd.Poster, id Snowflake, params *CreateGuildRoleParams) (ret *Role, err error) {
_, body, err := client.Post(&httd.Request{
Ratelimiter: ratelimitGuildRoles(id),
Endpoint: endpoint.GuildRoles(id),
Body: params,
ContentType: httd.ContentTypeJSON,
})
if err != nil {
return
}
err = unmarshal(body, &ret)
if err != nil {
return
}
// add guild id to roles
ret.guildID = id
return
}
// ModifyGuildRolePositionsParams ...
// https://discordapp.com/developers/docs/resources/guild#modify-guild-role-positions-json-params
type ModifyGuildRolePositionsParams struct {
ID Snowflake `json:"id"`
Position uint `json:"position"`
}
// ModifyGuildRolePositions [REST] Modify the positions of a set of role objects for the guild.
// Requires the 'MANAGE_ROLES' permission. Returns a list of all of the guild's role objects on success.
// Fires multiple Guild Role Update Gateway events.
// Method PATCH
// Endpoint /guilds/{guild.id}/roles
// Rate limiter /guilds/{guild.id}/roles
// Discord documentation https://discordapp.com/developers/docs/resources/guild#modify-guild-role-positions
// Reviewed 2018-08-18
// Comment -
func ModifyGuildRolePositions(client httd.Patcher, guildID Snowflake, params []ModifyGuildRolePositionsParams) (ret []*Role, err error) {
details := &httd.Request{
Ratelimiter: ratelimitGuildRoles(guildID),
Endpoint: endpoint.GuildRoles(guildID),
Body: params,
ContentType: httd.ContentTypeJSON,
}
_, body, err := client.Patch(details)
if err != nil {
return
}
err = unmarshal(body, &ret)
if err != nil {
return
}
// add guild id to roles
for _, role := range ret {
role.guildID = guildID
}
return
}
// ModifyGuildRoleParams JSON params for func ModifyGuildRole
type ModifyGuildRoleParams struct {
data map[string]interface{}
}
func (p *ModifyGuildRoleParams) init() {
if p.data != nil {
return
}
p.data = map[string]interface{}{}
}
func (p *ModifyGuildRoleParams) SetName(name string) {
p.init()
p.data["name"] = name
}
func (p *ModifyGuildRoleParams) SetPermissions(permissions uint64) {
p.init()
p.data["permissions"] = permissions
}
func (p *ModifyGuildRoleParams) SetColor(color uint) {
p.init()
p.data["color"] = color
}
func (p *ModifyGuildRoleParams) SetHoist(hoist bool) {
p.init()
p.data["hoist"] = hoist
}
func (p *ModifyGuildRoleParams) SetMentionable(mentionable bool) {
p.init()
p.data["mentionable"] = mentionable
}
func (p *ModifyGuildRoleParams) MarshalJSON() ([]byte, error) {
if len(p.data) == 0 {
return []byte(`{}`), nil
}
return httd.Marshal(p.data)
}
var _ json.Marshaler = (*ModifyGuildRoleParams)(nil)
// ModifyGuildRole [REST] Modify a guild role. Requires the 'MANAGE_ROLES' permission.
// Returns the updated role on success. Fires a Guild Role Update Gateway event.
// Method PATCH
// Endpoint /guilds/{guild.id}/roles/{role.id}
// Rate limiter /guilds/{guild.id}/roles
// Discord documentation https://discordapp.com/developers/docs/resources/guild#modify-guild-role
// Reviewed 2018-08-18
// Comment -
func ModifyGuildRole(client httd.Patcher, guildID, roleID Snowflake, params *ModifyGuildRoleParams) (ret *Role, err error) {
_, body, err := client.Patch(&httd.Request{
Ratelimiter: ratelimitGuildRoles(guildID),
Endpoint: endpoint.GuildRole(guildID, roleID),
Body: params,
ContentType: httd.ContentTypeJSON,
})
if err != nil {
return
}
err = unmarshal(body, &ret)
if err != nil {
return
}
// add guild id to role
ret.guildID = guildID
return
}
// DeleteGuildRole [REST] Delete a guild role. Requires the 'MANAGE_ROLES' permission.
// Returns a 204 empty response on success. Fires a Guild Role Delete Gateway event.
// Method DELETE
// Endpoint /guilds/{guild.id}/roles/{role.id}
// Rate limiter /guilds/{guild.id}/roles
// Discord documentation https://discordapp.com/developers/docs/resources/guild#delete-guild-role
// Reviewed 2018-08-18
// Comment -
func DeleteGuildRole(client httd.Deleter, guildID, roleID Snowflake) (err error) {
resp, _, err := client.Delete(&httd.Request{
Ratelimiter: ratelimitGuildRoles(guildID),
Endpoint: endpoint.GuildRole(guildID, roleID),
})
if err != nil {
return
}
if resp.StatusCode != http.StatusNoContent {
msg := "Could not remove role. Do you have the MANAGE_ROLES permission?"
err = errors.New(msg)
}
return
}