This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrest.go
1892 lines (1727 loc) · 58.2 KB
/
rest.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package kook
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strconv"
)
// Gateway returns the url for websocket gateway.
// FYI: https://developer.kookapp.cn/doc/http/gateway#%E8%8E%B7%E5%8F%96%E7%BD%91%E5%85%B3%E8%BF%9E%E6%8E%A5%E5%9C%B0%E5%9D%80
func (s *Session) Gateway() (gateway string, err error) {
u, _ := url.Parse(EndpointGatewayIndex)
q := u.Query()
q.Set("compress", "0")
if s.Identify.Compress {
q.Set("compress", "1")
}
u.RawQuery = q.Encode()
response, err := s.Request("GET", u.String(), nil)
if err != nil {
return
}
temp := struct {
URL string `json:"url"`
}{}
err = json.Unmarshal(response, &temp)
if err != nil {
return
}
gateway = temp.URL
return
}
// MessageListOption is the type for optional arguments for MessageList request.
type MessageListOption func(values url.Values)
// MessageListWithMsgID adds optional `msg_id` argument to MessageList request.
func MessageListWithMsgID(msgID string) MessageListOption {
return func(values url.Values) {
values.Set("msg_id", msgID)
}
}
// MessageListWithPin adds optional `pin` argument to MessageList request.
func MessageListWithPin(pin bool) MessageListOption {
return func(values url.Values) {
if pin {
values.Set("pin", "1")
} else {
values.Set("pin", "0")
}
}
}
// MessageListFlag is the type for the flag of MessageList.
type MessageListFlag string
// These are the usable flags
const (
MessageListFlagBefore MessageListFlag = "before"
MessageListFlagAround MessageListFlag = "around"
MessageListFlagAfter MessageListFlag = "after"
)
// MessageListWithFlag adds optional `flag` argument to MessageList request.
func MessageListWithFlag(flag MessageListFlag) MessageListOption {
return func(values url.Values) {
values.Set("flag", string(flag))
}
}
// MessageListWithPageSize adds optional `page_size` argument to MessageList request.
func MessageListWithPageSize(size int) MessageListOption {
return func(values url.Values) {
values.Set("page_size", strconv.Itoa(size))
}
}
// MessageList returns a list of messages of a channel.
// FYI: https://developer.kookapp.cn/doc/http/message#%E8%8E%B7%E5%8F%96%E9%A2%91%E9%81%93%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF%E5%88%97%E8%A1%A8
func (s *Session) MessageList(targetID string, options ...MessageListOption) (ms []*DetailedChannelMessage, err error) {
var response []byte
u, _ := url.Parse(EndpointMessageList)
q := u.Query()
q.Set("target_id", targetID)
for _, item := range options {
item(q)
}
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
data := struct {
Items []*DetailedChannelMessage `json:"items"`
}{}
err = json.Unmarshal(response, &data)
if err != nil {
return nil, err
}
ms = data.Items
return ms, nil
}
// MessageView returns a detailed message.
//
// FYI: https://developer.kookapp.cn/doc/http/message#%E8%8E%B7%E5%8F%96%E9%A2%91%E9%81%93%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF%E8%AF%A6%E6%83%85
func (s *Session) MessageView(msgId string) (m *DetailedChannelMessage, err error) {
var response []byte
u, _ := url.Parse(EndpointMessageView)
q := u.Query()
q.Add("msg_id", msgId)
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &m)
if err != nil {
return nil, err
}
return
}
// MessageCreateBase is the common arguments for message creation.
type MessageCreateBase struct {
Type MessageType `json:"type,omitempty"`
TargetID string `json:"target_id,omitempty"`
Content string `json:"content,omitempty"`
Quote string `json:"quote,omitempty"`
Nonce string `json:"nonce,omitempty"`
}
// MessageCreate is the type for message creation arguments.
type MessageCreate struct {
MessageCreateBase
TempTargetID string `json:"temp_target_id,omitempty"`
}
// MessageResp is the type for response for MessageCreate.
type MessageResp struct {
MsgID string `json:"msg_id"`
MsgTimestamp MilliTimeStamp `json:"msg_timestamp"`
Nonce string `json:"nonce"`
}
// MessageCreate creates a message.
// FYI: https://developer.kookapp.cn/doc/http/message#%E5%8F%91%E9%80%81%E9%A2%91%E9%81%93%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF
func (s *Session) MessageCreate(m *MessageCreate) (resp *MessageResp, err error) {
var response []byte
response, err = s.Request("POST", EndpointMessageCreate, m)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &resp)
if err != nil {
return nil, err
}
return
}
// MessageUpdateBase is the shared arguments for message update related requests.
type MessageUpdateBase struct {
MsgID string `json:"msg_id"`
Content string `json:"content"`
Quote string `json:"quote,omitempty"`
}
// MessageUpdate is the request data for MessageUpdate.
type MessageUpdate struct {
MessageUpdateBase
TempTargetID string `json:"temp_target_id,omitempty"`
}
// MessageUpdate updates a message.
// FYI: https://developer.kookapp.cn/doc/http/message#%E6%9B%B4%E6%96%B0%E9%A2%91%E9%81%93%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF
func (s *Session) MessageUpdate(m *MessageUpdate) (err error) {
_, err = s.Request("POST", EndpointMessageUpdate, m)
return
}
// MessageDelete deletes a message.
// FYI: https://developer.kookapp.cn/doc/http/message#%E5%88%A0%E9%99%A4%E9%A2%91%E9%81%93%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF
func (s *Session) MessageDelete(msgID string) (err error) {
_, err = s.Request("POST", EndpointMessageDelete, struct {
MsgID string `json:"msg_id"`
}{msgID})
return
}
// ReactedUser is the type for every user reacted to a specific message with a specific emoji.
type ReactedUser struct {
User
ReactionTime MilliTimeStamp `json:"reaction_time"`
TagInfo struct {
Color string `json:"color"`
Text string `json:"text"`
} `json:"tag_info"`
}
// MessageReactionList returns the list of the reacted users with a specific emoji to a message.
// FYI: https://developer.kookapp.cn/doc/http/message#%E8%8E%B7%E5%8F%96%E9%A2%91%E9%81%93%E6%B6%88%E6%81%AF%E6%9F%90%E5%9B%9E%E5%BA%94%E7%9A%84%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8
func (s *Session) MessageReactionList(msgID, emoji string) (us []*ReactedUser, err error) {
u, _ := url.Parse(EndpointMessageReactionList)
q := u.Query()
q.Add("msg_id", msgID)
q.Add("emoji", emoji)
u.RawQuery = q.Encode()
var response []byte
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &us)
if err != nil {
return nil, err
}
return us, nil
}
// MessageAddReaction add a reaction to a message as the bot.
// FYI: https://developer.kookapp.cn/doc/http/message#%E7%BB%99%E6%9F%90%E4%B8%AA%E6%B6%88%E6%81%AF%E6%B7%BB%E5%8A%A0%E5%9B%9E%E5%BA%94
func (s *Session) MessageAddReaction(msgID, emoji string) (err error) {
_, err = s.Request("POST", EndpointMessageAddReaction, struct {
MsgID string `json:"msg_id"`
Emoji string `json:"emoji"`
}{msgID, emoji})
return err
}
// MessageDeleteReaction deletes a reaction of a user from a message.
// FYI: https://developer.kookapp.cn/doc/http/message#%E5%88%A0%E9%99%A4%E6%B6%88%E6%81%AF%E7%9A%84%E6%9F%90%E4%B8%AA%E5%9B%9E%E5%BA%94
func (s *Session) MessageDeleteReaction(msgID, emoji string, userID string) (err error) {
_, err = s.Request("POST", EndpointMessageDeleteReaction, struct {
MsgID string `json:"msg_id"`
Emoji string `json:"emoji"`
UserID string `json:"user_id,omitempty"`
}{msgID, emoji, userID})
return err
}
// ChannelList lists all channels from a guild.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E8%8E%B7%E5%8F%96%E9%A2%91%E9%81%93%E5%88%97%E8%A1%A8
func (s *Session) ChannelList(guildID string, page *PageSetting) (cs []*Channel, meta *PageInfo, err error) {
var response []byte
u, _ := url.Parse(EndpointChannelList)
q := u.Query()
q.Set("guild_id", guildID)
u.RawQuery = q.Encode()
response, meta, err = s.RequestWithPage("GET", u.String(), page)
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(response, &cs)
if err != nil {
return nil, nil, err
}
return cs, meta, nil
}
// ChannelViewOption is the type for optional arguments for ChannelView request.
type ChannelViewOption func(values url.Values)
// ChannelViewWithNeedChildren adds optional `need_children` argument to ChannelView request.
func ChannelViewWithNeedChildren(needChildren bool) ChannelViewOption {
return func(values url.Values) {
if needChildren {
values.Set("need_children", "1")
}
}
}
// ChannelView returns the detailed information for a channel.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E8%8E%B7%E5%8F%96%E9%A2%91%E9%81%93%E8%AF%A6%E6%83%85
func (s *Session) ChannelView(channelID string, options ...ChannelViewOption) (c *Channel, err error) {
var response []byte
u, _ := url.Parse(EndpointChannelView)
q := u.Query()
q.Set("target_id", channelID)
for _, item := range options {
item(q)
}
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &c)
if err != nil {
return nil, err
}
return c, nil
}
// ChannelCreate is the arguments for creating a channel.
type ChannelCreate struct {
GuildID string `json:"guild_id"`
ParentID string `json:"parent_id,omitempty"`
Name string `json:"name"`
Type ChannelType `json:"type,omitempty"`
LimitAmount int `json:"limit_amount,omitempty"`
VoiceQuality string `json:"voice_quality,omitempty"`
IsCategory int `json:"is_category"`
}
// ChannelCreate creates a channel.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E5%88%9B%E5%BB%BA%E9%A2%91%E9%81%93
func (s *Session) ChannelCreate(cc *ChannelCreate) (c *Channel, err error) {
var response []byte
response, err = s.Request("POST", EndpointChannelCreate, cc)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &c)
if err != nil {
return nil, err
}
return c, err
}
// ChannelUpdate is the arguments for updating a channel's settings.
type ChannelUpdate struct {
ChannelID string `json:"channel_id"`
Name string `json:"name,omitempty"`
Level int `json:"level,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Topic string `json:"topic,omitempty"`
SlowMode int `json:"slow_mode,omitempty"`
LimitAmount int `json:"limit_amount,omitempty"`
VoiceQuality string `json:"voice_quality,omitempty"`
Password string `json:"password,omitempty"`
}
// ChannelUpdate updates a channel's settings.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E7%BC%96%E8%BE%91%E9%A2%91%E9%81%93
func (s *Session) ChannelUpdate(cu *ChannelUpdate) (c *Channel, err error) {
var response []byte
response, err = s.Request("POST", EndpointChannelUpdate, cu)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &c)
if err != nil {
return nil, err
}
return c, err
}
// ChannelDelete deletes a channel.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E5%88%A0%E9%99%A4%E9%A2%91%E9%81%93
func (s *Session) ChannelDelete(channelID string) (err error) {
_, err = s.Request("POST", EndpointChannelDelete, struct {
ChannelID string `json:"channel_id"`
}{channelID})
return err
}
// ChannelMoveUsers moves users to a channel.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E8%AF%AD%E9%9F%B3%E9%A2%91%E9%81%93%E4%B9%8B%E9%97%B4%E7%A7%BB%E5%8A%A8%E7%94%A8%E6%88%B7
func (s *Session) ChannelMoveUsers(targetChannelID string, userIDs []string) (err error) {
_, err = s.Request("POST", EndpointChannelMoveUser, struct {
TargetID string `json:"target_id"`
UserIDs []string `json:"user_ids"`
}{targetChannelID, userIDs})
return err
}
// ChannelRoleIndex is the role and permission list of a channel.
type ChannelRoleIndex struct {
PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"`
PermissionUsers []struct {
User User `json:"user"`
Allow RolePermission `json:"allow"`
Deny RolePermission `json:"deny"`
} `json:"permission_users"`
PermissionSync IntBool `json:"permission_sync"`
}
// ChannelRoleIndex returns the role and permission list of the channel.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E9%A2%91%E9%81%93%E8%A7%92%E8%89%B2%E6%9D%83%E9%99%90%E8%AF%A6%E6%83%85
func (s *Session) ChannelRoleIndex(channelID string) (cr *ChannelRoleIndex, err error) {
var response []byte
u, _ := url.Parse(EndpointChannelRoleIndex)
q := u.Query()
q.Set("channel_id", channelID)
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &cr)
if err != nil {
return nil, err
}
return cr, err
}
// ChannelRoleBase is the common arguments for channel role requests.
type ChannelRoleBase struct {
ChannelID string `json:"channel_id"`
Type string `json:"type,omitempty"`
Value string `json:"value,omitempty"`
}
// ChannelRoleCreate is the request query data for ChannelRoleCreate.
type ChannelRoleCreate ChannelRoleBase
// ChannelRoleCreateResp is the response for ChannelRoleCreate.
type ChannelRoleCreateResp ChannelRoleUpdateResp
// ChannelRoleCreate creates a role for a channel
// FYI: https://developer.kookapp.cn/doc/http/channel#%E5%88%9B%E5%BB%BA%E9%A2%91%E9%81%93%E8%A7%92%E8%89%B2%E6%9D%83%E9%99%90
func (s *Session) ChannelRoleCreate(crc *ChannelRoleCreate) (crcr *ChannelRoleCreateResp, err error) {
var resp []byte
resp, err = s.Request("POST", EndpointChannelRoleCreate, crc)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &crcr)
if err != nil {
return nil, err
}
return crcr, err
}
// ChannelRoleUpdate is the request query data for ChannelRoleUpdate
type ChannelRoleUpdate struct {
ChannelRoleBase
Allow RolePermission `json:"allow,omitempty"`
Deny RolePermission `json:"deny,omitempty"`
}
// ChannelRoleUpdateResp is the response of ChannelRoleUpdate
type ChannelRoleUpdateResp struct {
UserID string `json:"user_id"`
RoleID string `json:"role_id"`
Allow RolePermission `json:"allow"`
Deny RolePermission `json:"deny"`
}
// ChannelRoleUpdate updates a role from channel setting.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E6%9B%B4%E6%96%B0%E9%A2%91%E9%81%93%E8%A7%92%E8%89%B2%E6%9D%83%E9%99%90
func (s *Session) ChannelRoleUpdate(cru *ChannelRoleUpdate) (crur *ChannelRoleUpdateResp, err error) {
var response []byte
response, err = s.Request("POST", EndpointChannelRoleUpdate, cru)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &crur)
if err != nil {
return nil, err
}
return crur, nil
}
// ChannelRoleDelete is the type for settings when deleting a role from channel setting.
type ChannelRoleDelete ChannelRoleBase
// ChannelRoleDelete deletes a role form channel setting.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E5%88%A0%E9%99%A4%E9%A2%91%E9%81%93%E8%A7%92%E8%89%B2%E6%9D%83%E9%99%90
func (s *Session) ChannelRoleDelete(crd *ChannelRoleDelete) (err error) {
_, err = s.Request("POST", EndpointChannelRoleDelete, crd)
return err
}
// ChannelRoleSync syncs the roles' permissions.
// FYI: https://developer.kookapp.cn/doc/http/channel#%E5%90%8C%E6%AD%A5%E9%A2%91%E9%81%93%E8%A7%92%E8%89%B2%E6%9D%83%E9%99%90
func (s *Session) ChannelRoleSync(cid string) (err error) {
_, err = s.Request("POST", EndpointChannelRoleDelete, struct {
ChannelID string `json:"channel_id"`
}{cid})
return err
}
// UserInVoiceChannel is a user in a voice channel
type UserInVoiceChannel struct {
ID string `json:"id"`
GuildID string `json:"guild_id"`
MasterID string `json:"master_id"`
ParentID string `json:"parent_id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Topic string `json:"topic"`
Type ChannelType `json:"type"`
Level int `json:"level"`
SlowMode int `json:"slow_mode"`
LimitAmount int `json:"limit_amount"`
IsCategory bool `json:"is_category"`
PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"`
PermissionUsers []UserPermissionOverwrite `json:"permission_users"`
PermissionSync IntBool `json:"permission_sync"`
}
// ChannelUserGetJoinedChannel gets the user in voice channel
func (s *Session) ChannelUserGetJoinedChannel(guildID, userID string, page *PageSetting) (us []*UserInVoiceChannel, meta *PageInfo, err error) {
var response []byte
u, _ := url.Parse(EndpointChannelUserGetJoinedChannel)
q := u.Query()
q.Set("guild_id", guildID)
q.Set("user_id", userID)
u.RawQuery = q.Encode()
response, meta, err = s.RequestWithPage("GET", u.String(), page)
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(response, &us)
if err != nil {
return nil, nil, err
}
return us, meta, nil
}
// ChannelUserList returns a list of users in a voice channel.
func (s *Session) ChannelUserList(channelID string) (us []*User, err error) {
var response []byte
u, _ := url.Parse(EndpointChannelUserList)
q := u.Query()
q.Set("channel_id", channelID)
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &us)
if err != nil {
return nil, err
}
return us, nil
}
// UserChatList returns a list of user chats that bot owns.
//
// Note: for User in TargetInfo, only ID, Username, Online, Avatar is filled
//
// FYI: https://developer.kookapp.cn/doc/http/user-chat#%E8%8E%B7%E5%8F%96%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E4%BC%9A%E8%AF%9D%E5%88%97%E8%A1%A8
func (s *Session) UserChatList(page *PageSetting) (ucs []*UserChat, meta *PageInfo, err error) {
var response []byte
response, meta, err = s.RequestWithPage("GET", EndpointUserChatList, page)
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(response, &ucs)
if err != nil {
return nil, nil, err
}
return ucs, meta, err
}
// UserChatView returns a detailed user chat.
//
// FYI: https://developer.kookapp.cn/doc/http/user-chat#%E8%8E%B7%E5%8F%96%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E4%BC%9A%E8%AF%9D%E8%AF%A6%E6%83%85
func (s *Session) UserChatView(chatCode string) (uc *UserChat, err error) {
var response []byte
u, _ := url.Parse(EndpointUserChatView)
q := u.Query()
q.Set("chat_code", chatCode)
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
uc = &UserChat{}
err = json.Unmarshal(response, uc)
if err != nil {
return nil, err
}
return uc, nil
}
// UserChatCreate creates a direct chat session.
// FYI: https://developer.kookapp.cn/doc/http/user-chat#%E5%88%9B%E5%BB%BA%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E4%BC%9A%E8%AF%9D
func (s *Session) UserChatCreate(UserID string) (uc *UserChat, err error) {
var response []byte
response, err = s.Request("POST", EndpointUserChatCreate, struct {
TargetID string `json:"target_id"`
}{UserID})
if err != nil {
return nil, err
}
uc = &UserChat{}
err = json.Unmarshal(response, uc)
if err != nil {
return nil, err
}
return uc, err
}
// UserChatDelete deletes a direct chat session.
// FYI: https://developer.kookapp.cn/doc/http/user-chat#%E5%88%9B%E5%BB%BA%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E4%BC%9A%E8%AF%9D
func (s *Session) UserChatDelete(ChatCode string) (err error) {
_, err = s.Request("POST", EndpointUserChatDelete, struct {
ChatCode string `json:"chat_code"`
}{ChatCode: ChatCode})
return err
}
// DirectMessageListOption is the type for optional arguments for DirectMessageList request.
type DirectMessageListOption func(values url.Values)
// DirectMessageListWithChatCode adds optional `chat_code` argument to DirectMessageList request.
func DirectMessageListWithChatCode(chatCode string) DirectMessageListOption {
return func(values url.Values) {
values.Set("chat_code", chatCode)
}
}
// DirectMessageListWithTargetID adds optional `target_id` argument to DirectMessageList request.
func DirectMessageListWithTargetID(targetID string) DirectMessageListOption {
return func(values url.Values) {
values.Set("target_id", targetID)
}
}
// DirectMessageListWithMsgID adds optional `msg_id` argument to DirectMessageList request.
func DirectMessageListWithMsgID(msgID string) DirectMessageListOption {
return func(values url.Values) {
values.Set("msg_id", msgID)
}
}
// DirectMessageListWithFlag adds optional `flag` argument to DirectMessageList request.
func DirectMessageListWithFlag(flag MessageListFlag) DirectMessageListOption {
return func(values url.Values) {
values.Set("flag", string(flag))
}
}
// DirectMessageResp is the type for direct messages.
type DirectMessageResp struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Content string `json:"content"`
Embeds []map[string]string `json:"embeds"`
Attachments []Attachment `json:"attachments"`
CreateAt MilliTimeStamp `json:"create_at"`
UpdatedAt MilliTimeStamp `json:"updated_at"`
Reactions []ReactionItem `json:"reactions"`
ImageName string `json:"image_name"`
ReadStatus bool `json:"read_status"`
Quote *User `json:"quote"`
MentionInfo struct {
MentionPart []*User `json:"mention_part"`
MentionRolePart []*Role `json:"mention_role_part"`
} `json:"mention_info"`
}
// DirectMessageList returns the messages in direct chat.
//
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E8%8E%B7%E5%8F%96%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF%E5%88%97%E8%A1%A8
func (s *Session) DirectMessageList(options ...DirectMessageListOption) (dmrs []*DirectMessageResp, err error) {
var response []byte
u, _ := url.Parse(EndpointDirectMessageList)
q := u.Query()
for _, item := range options {
item(q)
}
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &dmrs)
if err != nil {
return nil, err
}
return dmrs, nil
}
// DirectMessageView returns the specified message.
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E8%8E%B7%E5%8F%96%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF%E8%AF%A6%E6%83%85
func (s *Session) DirectMessageView(chatCode, msgID string) (dmr *DirectMessageResp, err error) {
var response []byte
u, _ := url.Parse(EndpointDirectMessageView)
q := u.Query()
q.Set("chat_code", chatCode)
q.Set("msg_id", msgID)
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &dmr)
return
}
// DirectMessageCreate is the struct for settings of creating a message in direct chat.
type DirectMessageCreate struct {
MessageCreateBase
ChatCode string `json:"chat_code,omitempty"`
}
// DirectMessageCreate creates a message in direct chat.
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E5%8F%91%E9%80%81%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF
func (s *Session) DirectMessageCreate(create *DirectMessageCreate) (mr *MessageResp, err error) {
var response []byte
response, err = s.Request("POST", EndpointDirectMessageCreate, create)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &mr)
if err != nil {
return nil, err
}
return mr, nil
}
// DirectMessageUpdate is the type for settings of updating a message in direct chat.
type DirectMessageUpdate MessageUpdateBase
// DirectMessageUpdate updates a message in direct chat.
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E6%9B%B4%E6%96%B0%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF
func (s *Session) DirectMessageUpdate(update *DirectMessageUpdate) (err error) {
_, err = s.Request("POST", EndpointDirectMessageUpdate, update)
return err
}
// DirectMessageDelete deletes a message in direct chat.
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E5%88%A0%E9%99%A4%E7%A7%81%E4%BF%A1%E8%81%8A%E5%A4%A9%E6%B6%88%E6%81%AF
func (s *Session) DirectMessageDelete(msgID string) (err error) {
_, err = s.Request("POST", EndpointDirectMessageDelete, struct {
MsgID string `json:"msg_id"`
}{msgID})
return err
}
// DirectMessageReactionList returns the list of the reacted users with a specific emoji to a message.
//
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E8%8E%B7%E5%8F%96%E9%A2%91%E9%81%93%E6%B6%88%E6%81%AF%E6%9F%90%E5%9B%9E%E5%BA%94%E7%9A%84%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8
func (s *Session) DirectMessageReactionList(msgID, emoji string) (us []*ReactedUser, err error) {
u, _ := url.Parse(EndpointDirectMessageReactionList)
q := u.Query()
q.Add("msg_id", msgID)
q.Add("emoji", emoji)
u.RawQuery = q.Encode()
var response []byte
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(response, &us)
if err != nil {
return nil, err
}
return us, nil
}
// DirectMessageAddReaction add a reaction to a message as the bot.
//
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E7%BB%99%E6%9F%90%E4%B8%AA%E6%B6%88%E6%81%AF%E6%B7%BB%E5%8A%A0%E5%9B%9E%E5%BA%94
func (s *Session) DirectMessageAddReaction(msgID, emoji string) (err error) {
_, err = s.Request("POST", EndpointDirectMessageAddReaction, struct {
MsgID string `json:"msg_id"`
Emoji string `json:"emoji"`
}{msgID, emoji})
return err
}
// AssetCreate uploads attachments to kook server.
//
// FYI: https://developer.kookapp.cn/doc/http/asset#%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6/%E5%9B%BE%E7%89%87
func (s *Session) AssetCreate(name string, file []byte) (url string, err error) {
b := &bytes.Buffer{}
w := multipart.NewWriter(b)
var fw io.Writer
fw, err = w.CreateFormFile("file", name)
if err != nil {
return "", err
}
_, err = fw.Write(file)
if err != nil {
return "", err
}
err = w.Close()
if err != nil {
return "", err
}
var f assetFile
f.Payload = b.Bytes()
f.ContentType = w.FormDataContentType()
var response []byte
response, err = s.Request("POST", EndpointAssetCreate, &f)
if err != nil {
return "", err
}
urlStruct := struct {
URL string `json:"url"`
}{}
err = json.Unmarshal(response, &urlStruct)
if err != nil {
return "", err
}
return urlStruct.URL, nil
}
// DirectMessageDeleteReaction deletes a reaction of a user from a message.
//
// FYI: https://developer.kookapp.cn/doc/http/direct-message#%E5%88%A0%E9%99%A4%E6%B6%88%E6%81%AF%E7%9A%84%E6%9F%90%E4%B8%AA%E5%9B%9E%E5%BA%94
func (s *Session) DirectMessageDeleteReaction(msgID, emoji string) (err error) {
_, err = s.Request("POST", EndpointDirectMessageDeleteReaction, struct {
MsgID string `json:"msg_id"`
Emoji string `json:"emoji"`
}{msgID, emoji})
return err
}
// GuildList returns a list of guild that bot joins.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E7%94%A8%E6%88%B7%E5%8A%A0%E5%85%A5%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%88%97%E8%A1%A8
func (s *Session) GuildList(page *PageSetting) (gs []*Guild, meta *PageInfo, err error) {
var response []byte
response, meta, err = s.RequestWithPage("GET", EndpointGuildList, page)
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(response, &gs)
if err != nil {
return nil, nil, err
}
return gs, meta, nil
}
// GuildView returns a detailed info for a guild.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E8%8E%B7%E5%8F%96%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%AF%A6%E6%83%85
func (s *Session) GuildView(guildID string) (g *Guild, err error) {
var response []byte
u, _ := url.Parse(EndpointGuildView)
q := u.Query()
q.Add("guild_id", guildID)
u.RawQuery = q.Encode()
response, err = s.Request("GET", u.String(), nil)
if err != nil {
return nil, err
}
g = &Guild{}
err = json.Unmarshal(response, g)
if err != nil {
return nil, err
}
return g, nil
}
// GuildUserListOption is the type for optional arguments for GuildUserList request.
type GuildUserListOption func(values url.Values)
// GuildUserListWithChannelID adds optional `channel_id` argument to GuildUserList request.
func GuildUserListWithChannelID(id string) GuildUserListOption {
return func(values url.Values) {
values.Set("channel_id", id)
}
}
// GuildUserListWithSearch adds optional `search` argument to GuildUserList request.
func GuildUserListWithSearch(search string) GuildUserListOption {
return func(values url.Values) {
values.Set("search", search)
}
}
// GuildUserListWithRoleID adds optional `role_id` argument to GuildUserList request.
func GuildUserListWithRoleID(roleID int64) GuildUserListOption {
return func(values url.Values) {
values.Set("role_id", strconv.FormatInt(roleID, 10))
}
}
// GuildUserListWithMobileVerified adds optional `mobile_verified` argument to GuildUserList request.
func GuildUserListWithMobileVerified(verified bool) GuildUserListOption {
return func(values url.Values) {
if verified {
values.Set("mobile_verified", "1")
} else {
values.Set("mobile_verified", "0")
}
}
}
// GuildUserListWithActiveTime adds optional `active_time` argument to GuildUserList request.
func GuildUserListWithActiveTime(activeTime bool) GuildUserListOption {
return func(values url.Values) {
if activeTime {
values.Set("active_time", "1")
} else {
values.Set("active_time", "0")
}
}
}
// GuildUserListWithJoinedAt adds optional `joined_at` argument to GuildUserList request.
func GuildUserListWithJoinedAt(joinedAt bool) GuildUserListOption {
return func(values url.Values) {
if joinedAt {
values.Set("joined_at", "1")
} else {
values.Set("joined_at", "0")
}
}
}
type GuildUserListInfo struct {
UserCount int64 `json:"user_count"`
OnlineCount int64 `json:"online_count"`
OfflineCount int64 `json:"offline_count"`
}
// GuildUserList returns the list of users in a guild.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E8%8E%B7%E5%8F%96%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%AD%E7%9A%84%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8
func (s *Session) GuildUserList(guildID string, page *PageSetting, options ...GuildUserListOption) (us []*User, guli *GuildUserListInfo, meta *PageInfo, err error) {
var response []byte
u, _ := url.Parse(EndpointGuildUserList)
q := u.Query()
q.Set("guild_id", guildID)
for _, item := range options {
item(q)
}
if page != nil {
if page.Page != nil {
q.Add("page", strconv.Itoa(*page.Page))
}
if page.PageSize != nil {
q.Add("page_size", strconv.Itoa(*page.PageSize))
}
if page.Sort != nil {
q.Add("sort", *page.Sort)
}
}
u.RawQuery = q.Encode()
resp, err := s.Request("GET", u.String(), nil)
if err != nil {
return nil, nil, nil, err
}
g := &struct {
GeneralListData
GuildUserListInfo
}{}
err = json.Unmarshal(resp, g)
if err != nil {
return nil, nil, nil, err
}
err = json.Unmarshal(response, &us)
if err != nil {
return nil, nil, nil, err
}
return us, &g.GuildUserListInfo, &g.Meta, err
}
// GuildNickname is the arguments for GuildNickname.
type GuildNickname struct {
GuildID string `json:"guild_id"`
Nickname string `json:"nickname,omitempty"`
UserID string `json:"user_id,omitempty"`
}
// GuildNickname changes the nickname of a user in a guild.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E4%BF%AE%E6%94%B9%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%AD%E7%94%A8%E6%88%B7%E7%9A%84%E6%98%B5%E7%A7%B0
func (s *Session) GuildNickname(gn *GuildNickname) (err error) {
_, err = s.Request("POST", EndpointGuildNickName, gn)
return err
}
// GuildLeave let the bot leave a guild.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E7%A6%BB%E5%BC%80%E6%9C%8D%E5%8A%A1%E5%99%A8
func (s *Session) GuildLeave(guildID string) (err error) {
_, err = s.Request("POST", EndpointGuildLeave, struct {
GuildID string `json:"guild_id"`
}{guildID})
return err
}
// GuildKickout force a user to leave a guild.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E8%B8%A2%E5%87%BA%E6%9C%8D%E5%8A%A1%E5%99%A8
func (s *Session) GuildKickout(guildID, targetID string) (err error) {
_, err = s.Request("POST", EndpointGuildKickout, struct {
GuildID string `json:"guild_id"`
TargetID string `json:"target_id"`
}{guildID, targetID})
return err
}
// GuildMuteList is the type for users that got muted in a guild.
type GuildMuteList struct {
Mic []string `json:"1"`
Headset []string `json:"2"`
}
// GuildMuteList returns the list of users got mutes in mic or earphone.
// FYI: https://developer.kookapp.cn/doc/http/guild#%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%9D%99%E9%9F%B3%E9%97%AD%E9%BA%A6%E5%88%97%E8%A1%A8
func (s *Session) GuildMuteList(guildID string) (gml *GuildMuteList, err error) {
var response []byte
u, _ := url.Parse(EndpointGuildMuteList)
q := u.Query()
q.Set("guild_id", guildID)