forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
197 lines (170 loc) · 5.26 KB
/
member.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
package disgord
import (
"context"
"errors"
"net/http"
"github.com/andersfylling/disgord/internal/endpoint"
"github.com/andersfylling/disgord/internal/httd"
)
type GuildMemberQueryBuilder interface {
WithContext(ctx context.Context) GuildMemberQueryBuilder
WithFlags(flags ...Flag) GuildMemberQueryBuilder
Get() (*Member, error)
Update(params *UpdateMember) (*Member, error)
AddRole(roleID Snowflake) error
RemoveRole(roleID Snowflake) error
Kick(reason string) error
Ban(params *BanMember) error
GetPermissions() (PermissionBit, error)
}
func (g guildQueryBuilder) Member(userID Snowflake) GuildMemberQueryBuilder {
return &guildMemberQueryBuilder{client: g.client, gid: g.gid, uid: userID}
}
type guildMemberQueryBuilder struct {
ctx context.Context
flags Flag
client *Client
gid Snowflake
uid Snowflake
}
func (g *guildMemberQueryBuilder) validate() error {
if g.client == nil {
return ErrMissingClientInstance
}
if g.gid.IsZero() {
return ErrMissingGuildID
}
if g.uid.IsZero() {
return ErrMissingUserID
}
return nil
}
func (g guildMemberQueryBuilder) WithContext(ctx context.Context) GuildMemberQueryBuilder {
g.ctx = ctx
return &g
}
func (g guildMemberQueryBuilder) WithFlags(flags ...Flag) GuildMemberQueryBuilder {
g.flags = mergeFlags(flags)
return &g
}
// Get Returns a guild member object for the specified user.
func (g guildMemberQueryBuilder) Get() (*Member, error) {
if !ignoreCache(g.flags) {
if member, _ := g.client.cache.GetMember(g.gid, g.uid); member != nil {
return member, nil
}
}
r := g.client.newRESTRequest(&httd.Request{
Endpoint: endpoint.GuildMember(g.gid, g.uid),
Ctx: g.ctx,
}, g.flags)
r.factory = func() interface{} {
return &Member{
GuildID: g.gid,
UserID: g.uid,
}
}
member, err := getMember(r.Execute)
if err != nil {
return nil, err
}
member.GuildID = g.gid
return member, nil
}
// Update update a guild member
func (g guildMemberQueryBuilder) Update(params *UpdateMember) (*Member, 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.GuildMember(g.gid, g.uid),
ContentType: httd.ContentTypeJSON,
Body: params,
Reason: params.AuditLogReason,
}, g.flags)
r.factory = func() interface{} {
return &Member{}
}
member, err := getMember(r.Execute)
if err != nil {
return nil, err
}
member.GuildID = g.gid
member.UserID = g.uid
return member, nil
}
type UpdateMember struct {
Nick *string `json:"nick,omitempty"`
Roles *[]Snowflake `json:"roles,omitempty"`
Mute *bool `json:"mute,omitempty"`
Deaf *bool `json:"deaf,omitempty"`
ChannelID *Snowflake `json:"channel_id,omitempty"`
// CommunicationDisabledUntil defines when the user's timeout will expire and the user will be able to communicate in the guild again (up to 28 days in the future)
CommunicationDisabledUntil *Time `json:"communication_disabled_until,omitempty"`
AuditLogReason string `json:"-"`
}
// AddRole adds a role to a guild member. Requires the 'MANAGE_ROLES' permission.
// Returns a 204 empty response on success. Fires a Guild Member Update Gateway event.
func (g guildMemberQueryBuilder) AddRole(roleID Snowflake) error {
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodPut,
Endpoint: endpoint.GuildMemberRole(g.gid, g.uid, roleID),
Ctx: g.ctx,
}, g.flags)
_, err := r.Execute()
return err
}
// RemoveRole removes a role from a guild member. Requires the 'MANAGE_ROLES' permission.
// Returns a 204 empty response on success. Fires a Guild Member Update Gateway event.
func (g guildMemberQueryBuilder) RemoveRole(roleID Snowflake) error {
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Endpoint: endpoint.GuildMemberRole(g.gid, g.uid, roleID),
Ctx: g.ctx,
}, g.flags)
_, err := r.Execute()
return err
}
// Kick kicks a member from a guild. Requires 'KICK_MEMBERS' permission.
// Returns a 204 empty response on success. Fires a Guild Member Remove Gateway event.
func (g guildMemberQueryBuilder) Kick(reason string) error {
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Endpoint: endpoint.GuildMember(g.gid, g.uid),
Reason: reason,
Ctx: g.ctx,
}, g.flags)
_, err := r.Execute()
return err
}
// Ban Create a guild ban, and optionally delete previous messages sent by the banned user. Requires
// the 'BAN_MEMBERS' permission. Returns a 204 empty response on success. Fires a Guild Ban Add Gateway event.
func (g guildMemberQueryBuilder) Ban(params *BanMember) (err error) {
if params == nil {
return errors.New("params was nil")
}
if err = params.FindErrors(); err != nil {
return err
}
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodPut,
Endpoint: endpoint.GuildBan(g.gid, g.uid) + params.URLQueryString(),
Ctx: g.ctx,
Reason: params.Reason,
}, g.flags)
_, err = r.Execute()
return
}
// GetPermissions is used to return the members permissions.
func (g guildMemberQueryBuilder) GetPermissions() (PermissionBit, error) {
member, err := g.Get()
if err != nil {
return 0, err
}
return member.GetPermissions(g.ctx, g.client)
}