-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
385 lines (289 loc) · 8.13 KB
/
server.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package revolt
import (
"encoding/json"
"fmt"
"time"
"github.com/oklog/ulid/v2"
)
// Server struct.
type Server struct {
Client *Client
CreatedAt time.Time
Id string `json:"_id"`
Nonce string `json:"nonce"`
OwnerId string `json:"owner"`
Name string `json:"name"`
Description string `json:"description"`
ChannelIds []string `json:"channels"`
Categories []*Category `json:"categories"`
SystemMessages *ServerSystemMessages `json:"system_messages"`
Roles map[string]interface{} `json:"roles"`
DefaultPermissions uint `json:"default_permissions"`
Icon *Attachment `json:"icon"`
Banner *Attachment `json:"banner"`
}
type ServerCreate struct {
ID string `json:"_id"`
Server Server `json:"server"`
Channels []Channel `json:"channels"`
}
// Server categories struct.
type Category struct {
Id string `json:"id"`
Title string `json:"title"`
ChannelIds []string `json:"channels"`
}
// System messages struct.
type ServerSystemMessages struct {
UserJoined string `json:"user_joined,omitempty"`
UserLeft string `json:"user_left,omitempty"`
UserKicked string `json:"user_kicked,omitempty"`
UserBanned string `json:"user_banned,omitempty"`
UserTimeout string `json:"user_timeout,omitempty"`
}
// Server member struct.
type Member struct {
Informations struct {
ServerId string `json:"server"`
UserId string `json:"user"`
} `json:"_id"`
Nickname string `json:"nickname"`
Avatar *Attachment `json:"avatar"`
Roles []string `json:"roles"`
}
// Fetched server members struct.
type FetchedMembers struct {
Members []*Member `json:"members"`
Users []*User `json:"users"`
}
// Fetched bans struct.
type FetchedBans struct {
Users []*User `json:"users"`
Bans []struct {
Ids struct {
UserId string `json:"user"`
ServerUd string `json:"server"`
} `json:"_id"`
Reason string `json:"reason"`
} `json:"bans"`
}
// Calculate creation date and edit the struct.
func (s *Server) CalculateCreationDate() error {
ulid, err := ulid.Parse(s.Id)
if err != nil {
return err
}
s.CreatedAt = time.UnixMilli(int64(ulid.Time()))
return nil
}
// Edit server.
func (s Server) Edit(es *EditServer) error {
data, err := json.Marshal(es)
if err != nil {
return err
}
_, err = s.Client.Request("PATCH", "/servers/"+s.Id, data)
if err != nil {
return err
}
return nil
}
// Delete / leave server.
// If the server not created by client, it will leave.
// Otherwise it will be deleted.
func (s Server) Delete() error {
_, err := s.Client.Request("DELETE", "/servers/"+s.Id, []byte{})
if err != nil {
return err
}
return nil
}
// Create a new text-channel.
func (s Server) CreateTextChannel(name, description string) (*Channel, error) {
channel := &Channel{}
channel.Client = s.Client
data, err := s.Client.Request("POST", "/servers/"+s.Id+"/channels", []byte("{\"type\":\"Text\",\"name\":\""+name+"\",\"description\":\""+description+"\",\"nonce\":\""+genULID()+"\"}"))
if err != nil {
return channel, err
}
err = json.Unmarshal(data, channel)
if err != nil {
return channel, err
}
return channel, nil
}
// Create a new voice-channel.
func (s Server) CreateVoiceChannel(name, description string) (*Channel, error) {
channel := &Channel{}
channel.Client = s.Client
data, err := s.Client.Request("POST", "/servers/"+s.Id+"/channels", []byte("{\"type\":\"Voice\",\"name\":\""+name+"\",\"description\":\""+description+"\",\"nonce\":\""+genULID()+"\"}"))
if err != nil {
return channel, err
}
err = json.Unmarshal(data, channel)
if err != nil {
return channel, err
}
return channel, nil
}
// Fetch a member from Server.
func (s Server) FetchMember(id string) (*Member, error) {
member := &Member{}
data, err := s.Client.Request("GET", "/servers/"+s.Id+"/members/"+id, []byte{})
if err != nil {
return member, err
}
err = json.Unmarshal(data, member)
if err != nil {
return member, err
}
return member, nil
}
// Fetch all of the members from Server.
func (s Server) FetchMembers() (*FetchedMembers, error) {
members := &FetchedMembers{}
data, err := s.Client.Request("GET", "/servers/"+s.Id+"/members", []byte{})
if err != nil {
return members, err
}
err = json.Unmarshal(data, members)
if err != nil {
return members, err
}
// Add client to the user
for _, i := range members.Users {
i.Client = s.Client
}
return members, nil
}
// Edit a member.
func (s Server) EditMember(id string, em *EditMember) error {
data, err := json.Marshal(em)
if err != nil {
return err
}
_, err = s.Client.Request("PATCH", "/servers/"+s.Id+"/members/"+id, data)
if err != nil {
return err
}
return nil
}
// Kick a member from server.
func (s Server) KickMember(id string) error {
_, err := s.Client.Request("DELETE", "/servers/"+s.Id+"/members/"+id, []byte{})
if err != nil {
return err
}
return nil
}
// Ban a member from server.
func (s Server) BanMember(id, reason string) error {
_, err := s.Client.Request("PUT", "/servers/"+s.Id+"/bans/"+id, []byte("{\"reason\":\""+reason+"\"}"))
if err != nil {
return err
}
return nil
}
// Unban a member from server.
func (s Server) UnbanMember(id string) error {
_, err := s.Client.Request("DELETE", "/servers/"+s.Id+"/bans/"+id, []byte{})
if err != nil {
return err
}
return nil
}
// Fetch server bans.
func (s Server) FetchBans() (*FetchedBans, error) {
bans := &FetchedBans{}
data, err := s.Client.Request("GET", "/servers/"+s.Id+"/bans", []byte{})
if err != nil {
return bans, err
}
err = json.Unmarshal(data, bans)
if err != nil {
return bans, err
}
// Add client to the user
for _, i := range bans.Users {
i.Client = s.Client
}
return bans, nil
}
// Timeout a member from server.
func (s Server) TimeoutMember(id string) error {
// Placeholder for timeout.
return nil
}
// Set server permissions for a role.
// Leave role field empty if you want to edit default permissions
func (s Server) SetPermissions(role_id string, channel_permissions, server_permissions uint) error {
if role_id == "" {
role_id = "default"
}
_, err := s.Client.Request("PUT", "/servers/"+s.Id+"/permissions/"+role_id, []byte(fmt.Sprintf("{\"permissions\":{\"server\":%d,\"channel\":%d}}", channel_permissions, server_permissions)))
if err != nil {
return err
}
return nil
}
// Create a new role for server.
// Returns string (role id), uint (server perms), uint (channel perms) and error.
func (s Server) CreateRole(name string) (string, uint, uint, error) {
role := &struct {
Id string `json:"id"`
Permissions []uint `json:"permissions"`
}{}
data, err := s.Client.Request("POST", "/servers/"+s.Id+"/roles", []byte("{\"name\":\""+name+"\"}"))
if err != nil {
return role.Id, 0, 0, err
}
err = json.Unmarshal(data, role)
if err != nil {
return role.Id, 0, 0, err
}
return role.Id, role.Permissions[0], role.Permissions[1], nil
}
// Edit a server role.
func (s Server) EditRole(id string, er *EditRole) error {
data, err := json.Marshal(er)
if err != nil {
return err
}
_, err = s.Client.Request("PATCH", "/servers/"+s.Id+"/roles/"+id, data)
if err != nil {
return err
}
return nil
}
// Delete a server role.
func (s Server) DeleteRole(id string) error {
_, err := s.Client.Request("DELETE", "/servers/"+s.Id+"/roles/"+id, []byte{})
if err != nil {
return err
}
return nil
}
// Fetch server invite.
func (s Server) FetchInvites(id string) error {
_, err := s.Client.Request("GET", "/servers/"+id+"/invites", []byte{})
if err != nil {
return err
}
return nil
}
// Create server invite.
func (s Server) CreateInvites(id string) error {
_, err := s.Client.Request("POST", "/channels/"+id+"/invites", []byte{})
if err != nil {
return err
}
return nil
}
// Mark a server as read.
func (s Server) MarkServerAsRead(id string) error {
_, err := s.Client.Request("PUT", "/servers/"+id+"/ack", []byte{})
if err != nil {
return err
}
return nil
}