This repository has been archived by the owner on May 11, 2020. It is now read-only.
forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.go
309 lines (262 loc) · 8.3 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
package disgord
import (
"context"
"fmt"
"net/http"
"sort"
"github.com/andersfylling/disgord/internal/constant"
"github.com/andersfylling/disgord/internal/endpoint"
"github.com/andersfylling/disgord/internal/httd"
)
type roles []*Role
var _ sort.Interface = (roles)(nil)
func (r roles) Len() int {
return len(r)
}
// Less is reversed due to the visual ordering in Discord.
func (r roles) Less(i, j int) bool {
a := r[i]
b := r[j]
if a.Position == b.Position {
return a.ID < b.ID
}
return a.Position > b.Position
}
func (r roles) Swap(i, j int) {
tmp := r[i]
r[i] = r[j]
r[j] = tmp
}
// SortRoles sorts a slice of roles such that the first element is the top one in the Discord Guild Settings UI.
func SortRoles(rs []*Role) {
sort.Sort(roles(rs))
}
// 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 int `json:"position"` // can be -1
Permissions uint64 `json:"permissions"`
Managed bool `json:"managed"`
Mentionable bool `json:"mentionable"`
guildID Snowflake
}
var _ Mentioner = (*Role)(nil)
var _ Reseter = (*Role)(nil)
var _ DeepCopier = (*Role)(nil)
var _ Copier = (*Role)(nil)
var _ discordDeleter = (*Role)(nil)
var _ fmt.Stringer = (*Role)(nil)
func (r *Role) String() string {
return r.Name
}
// 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.guildID = 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) deleteFromDiscord(ctx context.Context, s Session, flags ...Flag) (err error) {
if constant.LockedMethods {
r.RLock()
}
guildID := r.guildID
id := r.ID
if constant.LockedMethods {
r.RUnlock()
}
if id.IsZero() {
return newErrorMissingSnowflake("role has no ID")
}
if guildID.IsZero() {
return newErrorMissingSnowflake("role has no guildID")
}
err = s.DeleteGuildRole(ctx, guildID, id, flags...)
return err
}
//////////////////////////////////////////////////////
//
// REST Methods
//
//////////////////////////////////////////////////////
// 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"`
// Reason is a X-Audit-Log-Reason header field that will show up on the audit log for this action.
Reason string `json:"-"`
}
// 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
// Discord documentation https://discordapp.com/developers/docs/resources/guild#create-guild-role
// Reviewed 2018-08-18
// Comment All JSON params are optional.
func (c *Client) CreateGuildRole(ctx context.Context, id Snowflake, params *CreateGuildRoleParams, flags ...Flag) (ret *Role, err error) {
r := c.newRESTRequest(&httd.Request{
Method: httd.MethodPost,
Ctx: ctx,
Endpoint: endpoint.GuildRoles(id),
Body: params,
ContentType: httd.ContentTypeJSON,
Reason: params.Reason,
}, flags)
r.CacheRegistry = GuildRoleCache
r.factory = func() interface{} {
return &Role{}
}
r.preUpdateCache = func(x interface{}) {
r := x.(*Role)
r.guildID = id
}
return getRole(r.Execute)
}
// 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}
// Discord documentation https://discordapp.com/developers/docs/resources/guild#modify-guild-role
// Reviewed 2018-08-18
// Comment -
func (c *Client) UpdateGuildRole(ctx context.Context, guildID, roleID Snowflake, flags ...Flag) (builder *updateGuildRoleBuilder) {
builder = &updateGuildRoleBuilder{}
builder.r.itemFactory = func() interface{} {
return &Role{}
}
builder.r.flags = flags
builder.r.IgnoreCache().setup(c.cache, c.req, &httd.Request{
Method: httd.MethodPatch,
Ctx: ctx,
Endpoint: endpoint.GuildRole(guildID, roleID),
ContentType: httd.ContentTypeJSON,
}, nil)
builder.r.cacheMiddleware = func(resp *http.Response, v interface{}, err error) error {
role := v.(*Role)
role.guildID = guildID
return nil
}
return builder
}
// 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}
// Discord documentation https://discordapp.com/developers/docs/resources/guild#delete-guild-role
// Reviewed 2018-08-18
// Comment -
func (c *Client) DeleteGuildRole(ctx context.Context, guildID, roleID Snowflake, flags ...Flag) (err error) {
r := c.newRESTRequest(&httd.Request{
Method: httd.MethodDelete,
Endpoint: endpoint.GuildRole(guildID, roleID),
Ctx: ctx,
}, flags)
r.expectsStatusCode = http.StatusNoContent
_, err = r.Execute()
return err
}
// GetGuildRoles [REST] Returns a list of role objects for the guild.
// Method GET
// Endpoint /guilds/{guild.id}/roles
// Discord documentation https://discordapp.com/developers/docs/resources/guild#get-guild-roles
// Reviewed 2018-08-18
// Comment -
func (c *Client) GetGuildRoles(ctx context.Context, guildID Snowflake, flags ...Flag) (ret []*Role, err error) {
r := c.newRESTRequest(&httd.Request{
Endpoint: "/guilds/" + guildID.String() + "/roles",
Ctx: ctx,
}, flags)
r.CacheRegistry = GuildRolesCache
r.factory = func() interface{} {
tmp := make([]*Role, 0)
return &tmp
}
r.preUpdateCache = func(x interface{}) {
roles := *x.(*[]*Role)
for i := range roles {
roles[i].guildID = guildID
}
}
return getRoles(r.Execute)
}
// GetMemberPermissions populates a uint64 with all the permission flags
func (c *Client) GetMemberPermissions(ctx context.Context, guildID, userID Snowflake, flags ...Flag) (permissions PermissionBits, err error) {
roles, err := c.GetGuildRoles(ctx, guildID, flags...)
if err != nil {
return 0, err
}
member, err := c.GetMember(ctx, guildID, userID, flags...)
if err != nil {
return 0, err
}
roleIDs := member.Roles
for i := range roles {
for j := range roleIDs {
if roles[i].ID == roleIDs[j] {
permissions |= roles[i].Permissions
roleIDs = roleIDs[:j+copy(roleIDs[j:], roleIDs[j+1:])]
break
}
}
if len(roleIDs) == 0 {
break
}
}
return permissions, nil
}
//////////////////////////////////////////////////////
//
// REST Builders
//
//////////////////////////////////////////////////////
// updateGuildRoleBuilder ...
//generate-rest-basic-execute: role:*Role,
//generate-rest-params: name:string, permissions:PermissionBits, color:uint, hoist:bool, mentionable:bool,
type updateGuildRoleBuilder struct {
r RESTBuilder
}