This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
role.go
170 lines (141 loc) · 3.91 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
package disgord
import (
"context"
"fmt"
"net/http"
"sort"
"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
}
// Role https://discord.com/developers/docs/topics/permissions#role-object
type Role struct {
ID Snowflake `json:"id"`
Name string `json:"name"`
Color uint `json:"color"`
Hoist bool `json:"hoist"`
Position int `json:"position"` // can be -1
Permissions PermissionBit `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 _ 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
}
//////////////////////////////////////////////////////
//
// REST Methods
//
//////////////////////////////////////////////////////
type GuildRoleQueryBuilder interface {
WithContext(ctx context.Context) GuildRoleQueryBuilder
WithFlags(flags ...Flag) GuildRoleQueryBuilder
Update(params *UpdateRole) (*Role, error)
Delete() error
Get() (*Role, error)
}
func (g guildQueryBuilder) Role(id Snowflake) GuildRoleQueryBuilder {
return &guildRoleQueryBuilder{client: g.client, gid: g.gid, roleID: id}
}
type guildRoleQueryBuilder struct {
ctx context.Context
flags Flag
client *Client
gid Snowflake
roleID Snowflake
}
func (g *guildRoleQueryBuilder) validate() error {
if g.client == nil {
return ErrMissingClientInstance
}
if g.gid.IsZero() {
return ErrMissingGuildID
}
if g.roleID.IsZero() {
return ErrMissingRoleID
}
return nil
}
func (g guildRoleQueryBuilder) WithContext(ctx context.Context) GuildRoleQueryBuilder {
g.ctx = ctx
return &g
}
func (g guildRoleQueryBuilder) WithFlags(flags ...Flag) GuildRoleQueryBuilder {
g.flags = mergeFlags(flags)
return &g
}
// Delete a guild role. Requires the 'MANAGE_ROLES' permission.
// Returns a 204 empty response on success. Fires a Guild Role Delete Gateway event.
func (g guildRoleQueryBuilder) Delete() error {
if err := g.validate(); err != nil {
return err
}
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Endpoint: endpoint.GuildRole(g.gid, g.roleID),
Ctx: g.ctx,
}, g.flags)
_, err := r.Execute()
return err
}
// Update update a role
func (g guildRoleQueryBuilder) Update(params *UpdateRole) (*Role, error) {
if params == nil {
return nil, ErrMissingRESTParams
}
if err := g.validate(); err != nil {
return nil, err
}
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodPatch,
Ctx: g.ctx,
Endpoint: endpoint.GuildRole(g.gid, g.roleID),
ContentType: httd.ContentTypeJSON,
Body: params,
Reason: params.AuditLogReason,
}, g.flags)
r.factory = func() interface{} {
return &Role{}
}
return getRole(r.Execute)
}
type UpdateRole struct {
Name *string `json:"name,omitempty"`
Permissions *PermissionBit `json:"permissions,omitempty"`
Color *int `json:"color,omitempty"`
Hoist *bool `json:"hoist,omitempty"`
Mentionable *bool `json:"mentionable,omitempty"`
AuditLogReason string `json:"-"`
}