forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
1225 lines (1039 loc) · 33.6 KB
/
client.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 disgord
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/andersfylling/snowflake/v3"
"github.com/andersfylling/disgord/constant"
"github.com/andersfylling/disgord/websocket"
"golang.org/x/net/proxy"
"github.com/andersfylling/disgord/event"
"github.com/andersfylling/disgord/httd"
)
// NewRESTClient creates a client for sending and handling Discord protocols such as rate limiting
func NewRESTClient(conf *Config) (client *httd.Client) {
// request client
reqConf := &httd.Config{
APIVersion: constant.DiscordVersion,
BotToken: conf.BotToken,
UserAgentSourceURL: constant.GitHubURL,
UserAgentVersion: constant.Version,
HTTPClient: conf.HTTPClient,
CancelRequestWhenRateLimited: conf.CancelRequestWhenRateLimited,
}
client = httd.NewClient(reqConf)
return
}
// NewSession create a client and return the Session interface
func NewClient(conf *Config) (*Client, error) {
var err error
if conf.HTTPClient == nil {
// http client configuration
conf.HTTPClient = &http.Client{
Timeout: time.Second * 10,
}
}
if conf.Proxy != nil {
conf.HTTPClient.Transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
return conf.Proxy.Dial(network, addr)
},
}
}
if conf.ProjectName == "" {
conf.ProjectName = LibraryInfo()
}
conf.shutdownChan = make(chan interface{})
// request client
reqClient := NewRESTClient(conf)
if conf.WSShardManagerConfig == nil {
conf.WSShardManagerConfig = &WSShardManagerConfig{}
}
if conf.WSShardManagerConfig.ShardRateLimit == 0 {
conf.WSShardManagerConfig.ShardRateLimit = DefaultShardRateLimit
}
sharding := NewShardManager(conf)
// caching
// TODO: should not pre-set the cache sizes as some guilds might be small while others huge.
// might spend more memory than needed
var cacher *Cache
if !conf.DisableCache {
if conf.CacheConfig == nil {
conf.CacheConfig = DefaultCacheConfig()
} else {
ensureBasicCacheConfig(conf.CacheConfig)
}
cacher, err = newCache(conf.CacheConfig)
if err != nil {
return nil, err
}
// register for events for activate caches
if !conf.CacheConfig.DisableUserCaching {
sharding.TrackEvent.Add(event.Ready)
sharding.TrackEvent.Add(event.UserUpdate)
}
if !conf.CacheConfig.DisableChannelCaching {
sharding.TrackEvent.Add(event.ChannelCreate)
sharding.TrackEvent.Add(event.ChannelUpdate)
sharding.TrackEvent.Add(event.ChannelPinsUpdate)
sharding.TrackEvent.Add(event.ChannelDelete)
}
if !conf.CacheConfig.DisableGuildCaching {
sharding.TrackEvent.Add(event.GuildCreate)
sharding.TrackEvent.Add(event.GuildDelete)
sharding.TrackEvent.Add(event.GuildUpdate)
sharding.TrackEvent.Add(event.GuildEmojisUpdate)
sharding.TrackEvent.Add(event.GuildMemberAdd)
sharding.TrackEvent.Add(event.GuildMemberRemove)
sharding.TrackEvent.Add(event.GuildMembersChunk)
sharding.TrackEvent.Add(event.GuildMemberUpdate)
sharding.TrackEvent.Add(event.GuildRoleCreate)
sharding.TrackEvent.Add(event.GuildRoleDelete)
sharding.TrackEvent.Add(event.GuildRoleUpdate)
sharding.TrackEvent.Add(event.GuildIntegrationsUpdate)
}
}
// Required for voice operation
sharding.TrackEvent.Add(event.VoiceStateUpdate)
sharding.TrackEvent.Add(event.VoiceServerUpdate)
// event dispatcher
eventChanSize := 20
evtDispatcher := NewDispatch(conf.ActivateEventChannels, eventChanSize)
// create a disgord client/instance/session
c := &Client{
shutdownChan: conf.shutdownChan,
config: conf,
shardManager: sharding,
httpClient: conf.HTTPClient,
Proxy: conf.Proxy,
botToken: conf.BotToken,
evtDispatch: evtDispatcher,
req: reqClient,
cache: cacher,
log: conf.Logger,
}
c.voiceRepository = newVoiceRepository(c)
return c, nil
}
// Config Configuration for the DisGord client
type Config struct {
BotToken string
HTTPClient *http.Client
Proxy proxy.Dialer
CancelRequestWhenRateLimited bool
DisableCache bool
CacheConfig *CacheConfig
WSShardManagerConfig *WSShardManagerConfig
//ImmutableCache bool
//LoadAllMembers bool
//LoadAllChannels bool
//LoadAllRoles bool
//LoadAllPresences bool
// for cancellation
shutdownChan chan interface{}
// your project name, name of bot, or application
ProjectName string
// ActivateEventChannels signifies that the developer will use channels to handle incoming events. May it be
// in addition to handlers or not. This forces the use of a scheduler to empty the buffered channels when they
// reach their capacity. Since it requires extra resources, others who have no interest in utilizing channels
// should not experience any performance penalty (even though it might be unnoticeable).
ActivateEventChannels bool
// Logger is a dependency that must be injected to support logging.
// disgord.DefaultLogger() can be used
Logger Logger
}
// Client is the main disgord client to hold your state and data
type Client struct {
sync.RWMutex
shutdownChan chan interface{}
config *Config
botToken string
myID Snowflake
// register listeners for events
evtDispatch *Dispatch
// cancelRequestWhenRateLimited by default the client waits until either the HTTPClient.timeout or
// the rate limit ends before closing a request channel. If activated, in stead, requests will
// instantly be denied, and the channel closed.
cancelRequestWhenRateLimited bool
// discord http api
req *httd.Client
// http client used for connections
httpClient *http.Client
Proxy proxy.Dialer
shardManager *WSShardManager
cache *Cache
log Logger
// voice
*voiceRepository
}
var _ Session = (*Client)(nil)
// HeartbeatLatency checks the duration of waiting before receiving a response from Discord when a
// heartbeat packet was sent. Note that heartbeats are usually sent around once a minute and is not a accurate
// way to measure delay between the client and Discord server
func (c *Client) HeartbeatLatency() (duration time.Duration, err error) {
return c.shardManager.GetAvgHeartbeatLatency()
}
// Myself get the current user / connected user
func (c *Client) Myself() (user *User, err error) {
if c.myID.Empty() {
user, err = c.GetCurrentUser().IgnoreCache().Execute()
if err == nil {
c.myID = user.ID
}
return
}
var usr interface{}
usr, err = c.cache.Get(UserCache, c.myID)
if err == nil {
user = usr.(*User)
}
return
}
// GetConnectedGuilds get a list over guild IDs that this client is "connected to"; or have joined through the ws connection. This will always hold the different Guild IDs, while the GetGuilds or GetCurrentUserGuilds might be affected by cache configuration.
func (c *Client) GetConnectedGuilds() []snowflake.ID {
c.shardManager.RLock()
defer c.shardManager.RUnlock()
var guilds []snowflake.ID
for i := range c.shardManager.shards {
guilds = append(guilds, c.shardManager.shards[i].guilds...)
}
return guilds
}
// Deprecated
func (c *Client) logInfo(msg string) {
c.Info(msg)
}
// Deprecated
func (c *Client) logErr(msg string) {
c.Error(msg)
}
func (c *Client) Info(v ...interface{}) {
if c.log != nil {
c.log.Info(v)
}
}
func (c *Client) Debug(v ...interface{}) {
if c.log != nil {
c.log.Debug(v)
}
}
func (c *Client) Error(v ...interface{}) {
if c.log != nil {
c.log.Error(v)
}
}
var _ Logger = (*Client)(nil)
func (c *Client) String() string {
return LibraryInfo()
}
var _ fmt.Stringer = (*Client)(nil)
// RateLimiter return the rate limiter object
func (c *Client) RateLimiter() httd.RateLimiter {
return c.req.RateLimiter()
}
func (c *Client) setupConnectEnv() {
// set the user ID upon connection
// only works with socket logic
c.On(event.UserUpdate, c.handlerUpdateSelfBot)
c.On(event.GuildCreate, c.handlerAddToConnectedGuilds)
c.On(event.GuildDelete, c.handlerRemoveFromConnectedGuilds)
// setup event observer
go c.eventHandler()
//c.evtDispatch.start()
}
// Connect establishes a websocket connection to the discord API
func (c *Client) Connect() (err error) {
// set the user ID upon connection
// only works for socketing
//
// also verifies that the correct credentials were supplied
var me *User
if me, err = c.GetCurrentUser().Execute(); err != nil {
return
}
c.myID = me.ID
url, shardCount, err := c.shardManager.GetConnectionDetails(c.req)
if err != nil {
return err
}
if c.config.WSShardManagerConfig.URL == "" {
c.config.WSShardManagerConfig.URL = url
}
if c.config.WSShardManagerConfig.ShardLimit == 0 {
c.config.WSShardManagerConfig.ShardLimit = shardCount
}
_ = c.shardManager.Prepare(c.config)
c.setupConnectEnv() // calling this before the c.ShardManager.Prepare will cause a evtChan deadlock
c.Info("Connecting to discord Gateway")
err = c.shardManager.Connect()
if err != nil {
c.Info(err)
return
}
c.Info("Connected")
return nil
}
// Disconnect closes the discord websocket connection
func (c *Client) Disconnect() (err error) {
fmt.Println() // to keep ^C on it's own line
c.Info("Closing Discord gateway connection")
close(c.evtDispatch.shutdown)
err = c.shardManager.Disconnect()
if err != nil {
c.Error(err)
return
}
close(c.shutdownChan)
c.Info("Disconnected")
return nil
}
var _ Link = (*Client)(nil)
// DisconnectOnInterrupt wait until a termination signal is detected
func (c *Client) DisconnectOnInterrupt() (err error) {
// create a channel to listen for termination signals (graceful shutdown)
termSignal := make(chan os.Signal, 1)
signal.Notify(termSignal, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-termSignal
return c.Disconnect()
}
// Req return the request object. Used in REST requests to handle rate limits,
// wrong http responses, etc.
func (c *Client) Req() httd.Requester {
return c.req
}
// Cache returns the cacheLink manager for the session
func (c *Client) Cache() Cacher {
return c.cache
}
// Ready triggers a given callback when all shards has gotten their first Ready event
// Warning: if you run client.Disconnect and want to run Connect again later, this will
// not work. The callback will be triggered instantly, as all the shards have already
// successfully connected once.
// TODO: allow this method to be reused (see Warning paragraph)
func (c *Client) Ready(cb func()) {
// TODO: optimize..
go func() {
for {
<-time.After(1 * time.Second)
if c.shardManager.InitialReadyReceived() {
break
}
}
cb()
}()
}
// On adds a event handler on the given event.
// On => event => handle the content like this
func (c *Client) On(event string, handlers ...interface{}) {
c.shardManager.TrackEvent.Add(event)
c.evtDispatch.listenersLock.Lock()
defer c.evtDispatch.listenersLock.Unlock()
for _, handler := range handlers {
c.evtDispatch.listeners[event] = append(c.evtDispatch.listeners[event], handler)
}
}
// Once same as `On`, however, once the handler is triggered, it is removed. In other words, it is only triggered once.
func (c *Client) Once(event string, handlers ...interface{}) {
c.shardManager.TrackEvent.Add(event) // TODO: remove event after firing. unless there are more handlers
c.evtDispatch.listenersLock.Lock()
defer c.evtDispatch.listenersLock.Unlock()
for _, handler := range handlers {
index := len(c.evtDispatch.listeners[event])
c.evtDispatch.listeners[event] = append(c.evtDispatch.listeners[event], handler)
c.evtDispatch.listenOnceOnly[event] = append(c.evtDispatch.listenOnceOnly[event], index)
}
}
// Emit sends a socket command directly to Discord.
func (c *Client) Emit(command SocketCommand, data interface{}) error {
switch command {
case CommandUpdateStatus, CommandUpdateVoiceState, CommandRequestGuildMembers:
default:
return errors.New("command is not supported")
}
return c.shardManager.Emit(command, data)
}
// EventChan get a event channel using the event name
func (c *Client) EventChan(event string) (channel interface{}, err error) {
return c.evtDispatch.EventChan(event)
}
// EventChannels get access to all the event channels
func (c *Client) EventChannels() (channels EventChannels) {
return c.evtDispatch
}
// AcceptEvent only events registered using this method is accepted from the Discord socket API. The rest is discarded
// to reduce unnecessary marshalling and controls.
func (c *Client) AcceptEvent(events ...string) {
for _, evt := range events {
c.shardManager.TrackEvent.Add(evt)
}
}
// Generic CRUDS
// DeleteFromDiscord if the given object has implemented the private interface discordDeleter this method can
// be used to delete said object.
func (c *Client) DeleteFromDiscord(obj discordDeleter) (err error) {
err = obj.deleteFromDiscord(c)
return
}
// SaveToDiscord saves an object to the Discord servers. This supports creation of new objects. Given two arguments,
// the original object before changes (that reflects the most recent known discord state) and the new object that
// represents what the state should look like, the changes can be determined by a basic diff check and let DisGord
// try to udpating/modify the object at Discord.
//
// client.SaveToDiscord(object) -> saves an entirely new object (must not have a discord id)
// client.SaveToDiscord(original, new) -> updates an existing object (must have a discord id)
func (c *Client) SaveToDiscord(objects ...discordSaver) (err error) {
if len(objects) == 0 {
return errors.New("you must specify at least one discord object to be saved to Discord")
}
var changes discordSaver
if len(objects) > 1 {
changes = objects[1]
}
err = objects[0].saveToDiscord(c, changes)
return
}
// REST
// Channel
// GetChannel ...
func (c *Client) GetChannel(id Snowflake) (ret *Channel, err error) {
if ret, err = c.cache.GetChannel(id); err != nil {
ret, err = GetChannel(c.req, id)
if err != nil {
return
}
_ = c.cache.Update(ChannelCache, ret)
}
return
}
// ModifyChannel ...
func (c *Client) ModifyChannel(id Snowflake, changes *ModifyChannelParams) (ret *Channel, err error) {
ret, err = ModifyChannel(c.req, id, changes) // should trigger a socket event, no need to update cacheLink
return
}
// DeleteChannel ...
func (c *Client) DeleteChannel(id Snowflake) (channel *Channel, err error) {
channel, err = DeleteChannel(c.req, id) // should trigger a socket event, no need to update cacheLink
return
}
// SetChannelPermissions ...
func (c *Client) SetChannelPermissions(chanID, overwriteID Snowflake, params *SetChannelPermissionsParams) (err error) {
err = EditChannelPermissions(c.req, chanID, overwriteID, params)
return
}
// GetChannelInvites ...
func (c *Client) GetChannelInvites(id Snowflake) (ret []*Invite, err error) {
ret, err = GetChannelInvites(c.req, id)
return
}
// CreateChannelInvites ...
func (c *Client) CreateChannelInvites(id Snowflake, params *CreateChannelInvitesParams) (ret *Invite, err error) {
ret, err = CreateChannelInvites(c.req, id, params)
return
}
// DeleteChannelPermission .
func (c *Client) DeleteChannelPermission(channelID, overwriteID Snowflake) (err error) {
err = DeleteChannelPermission(c.req, channelID, overwriteID)
return
}
// TriggerTypingIndicator .
func (c *Client) TriggerTypingIndicator(channelID Snowflake) (err error) {
err = TriggerTypingIndicator(c.req, channelID)
return
}
// GetPinnedMessages .
func (c *Client) GetPinnedMessages(channelID Snowflake) (ret []*Message, err error) {
ret, err = GetPinnedMessages(c.req, channelID)
return
}
// AddPinnedChannelMessage .
func (c *Client) AddPinnedChannelMessage(channelID, msgID Snowflake) (err error) {
err = AddPinnedChannelMessage(c.req, channelID, msgID)
return
}
// DeletePinnedChannelMessage .
func (c *Client) DeletePinnedChannelMessage(channelID, msgID Snowflake) (err error) {
err = DeletePinnedChannelMessage(c.req, channelID, msgID)
return
}
// GroupDMAddRecipient .
func (c *Client) GroupDMAddRecipient(channelID, userID Snowflake, params *GroupDMAddRecipientParams) (err error) {
err = GroupDMAddRecipient(c.req, channelID, userID, params)
return
}
// GroupDMRemoveRecipient .
func (c *Client) GroupDMRemoveRecipient(channelID, userID Snowflake) (err error) {
err = GroupDMRemoveRecipient(c.req, channelID, userID)
return
}
// GetChannelMessages .
func (c *Client) GetChannelMessages(channelID Snowflake, params URLParameters) (ret []*Message, err error) {
ret, err = GetChannelMessages(c.req, channelID, params)
return
}
// GetChannelMessage .
func (c *Client) GetChannelMessage(channelID, messageID Snowflake) (ret *Message, err error) {
ret, err = GetChannelMessage(c.req, channelID, messageID)
return
}
// CreateChannelMessage .
func (c *Client) CreateChannelMessage(channelID Snowflake, params *CreateChannelMessageParams) (ret *Message, err error) {
ret, err = CreateChannelMessage(c.req, channelID, params)
return
}
// EditMessage .
func (c *Client) EditMessage(chanID, msgID Snowflake, params *EditMessageParams) (ret *Message, err error) {
ret, err = EditMessage(c.req, chanID, msgID, params)
return
}
// DeleteMessage .
func (c *Client) DeleteMessage(channelID, msgID Snowflake) (err error) {
err = DeleteMessage(c.req, channelID, msgID)
return
}
// BulkDeleteMessages .
func (c *Client) BulkDeleteMessages(chanID Snowflake, params *BulkDeleteMessagesParams) (err error) {
err = BulkDeleteMessages(c.req, chanID, params)
return
}
// CreateReaction .
func (c *Client) CreateReaction(channelID, messageID Snowflake, emoji interface{}) (err error) {
err = CreateReaction(c.req, channelID, messageID, emoji)
return
}
// DeleteOwnReaction .
func (c *Client) DeleteOwnReaction(channelID, messageID Snowflake, emoji interface{}) (err error) {
err = DeleteOwnReaction(c.req, channelID, messageID, emoji)
return
}
// DeleteUserReaction .
func (c *Client) DeleteUserReaction(channelID, messageID, userID Snowflake, emoji interface{}) (err error) {
err = DeleteUserReaction(c.req, channelID, messageID, userID, emoji)
return
}
// GetReaction .
func (c *Client) GetReaction(channelID, messageID Snowflake, emoji interface{}, params URLParameters) (ret []*User, err error) {
ret, err = GetReaction(c.req, channelID, messageID, emoji, params)
return
}
// DeleteAllReactions .
func (c *Client) DeleteAllReactions(channelID, messageID Snowflake) (err error) {
err = DeleteAllReactions(c.req, channelID, messageID)
return
}
// Guild
// CreateGuild .
func (c *Client) CreateGuild(params *CreateGuildParams) (ret *Guild, err error) {
ret, err = CreateGuild(c.req, params)
return
}
// GetGuild .
func (c *Client) GetGuild(id Snowflake) (ret *Guild, err error) {
ret, err = c.cache.GetGuild(id)
if err != nil {
ret, err = GetGuild(c.req, id)
if err != nil {
return
}
c.cache.SetGuild(ret)
}
return
}
// ModifyGuild .
func (c *Client) ModifyGuild(id Snowflake, params *ModifyGuildParams) (ret *Guild, err error) {
ret, err = ModifyGuild(c.req, id, params)
return
}
// DeleteGuild .
func (c *Client) DeleteGuild(id Snowflake) (err error) {
err = DeleteGuild(c.req, id)
return
}
// GetGuildChannels .
func (c *Client) GetGuildChannels(id Snowflake) (ret []*Channel, err error) {
var guild *Guild
guild, err = c.cache.GetGuild(id)
if err != nil {
ret, err = GetGuildChannels(c.req, id)
if err != nil {
return
}
c.cache.SetGuild(&Guild{
ID: id,
Channels: ret,
})
} else {
ret = guild.Channels
}
return
}
// CreateGuildChannel .
func (c *Client) CreateGuildChannel(id Snowflake, params *CreateGuildChannelParams) (ret *Channel, err error) {
ret, err = CreateGuildChannel(c.req, id, params)
return
}
// ModifyGuildChannelPositions .
func (c *Client) ModifyGuildChannelPositions(id Snowflake, params []ModifyGuildChannelPositionsParams) (ret *Guild, err error) {
ret, err = ModifyGuildChannelPositions(c.req, id, params)
return
}
// GetGuildMember .
func (c *Client) GetGuildMember(guildID, userID Snowflake) (ret *Member, err error) {
ret, err = c.cache.GetGuildMember(guildID, userID)
if err != nil {
ret, err = GetGuildMember(c.req, guildID, userID)
if err != nil {
return
}
c.cache.SetGuildMember(guildID, ret)
c.cache.Update(UserCache, ret.User)
}
return
}
// GetGuildMembers .
func (c *Client) GetGuildMembers(guildID, after Snowflake, limit int) (ret []*Member, err error) {
ret, err = c.cache.GetGuildMembersAfter(guildID, after, limit)
if err != nil {
ret, err = GetGuildMembers(c.req, guildID, after, limit)
if err != nil {
return
}
c.cache.SetGuildMembers(guildID, ret)
//c.cacheLink.Update(UserCache, ret.User)
// TODO: update users
}
return
}
// AddGuildMember .
func (c *Client) AddGuildMember(guildID, userID Snowflake, params *AddGuildMemberParams) (ret *Member, err error) {
ret, err = AddGuildMember(c.req, guildID, userID, params)
return
}
// ModifyGuildMember .
func (c *Client) ModifyGuildMember(guildID, userID Snowflake, params *ModifyGuildMemberParams) (err error) {
err = ModifyGuildMember(c.req, guildID, userID, params)
return
}
// ModifyCurrentUserNick .
func (c *Client) ModifyCurrentUserNick(id Snowflake, params *ModifyCurrentUserNickParams) (nick string, err error) {
nick, err = ModifyCurrentUserNick(c.req, id, params)
return
}
// AddGuildMemberRole .
func (c *Client) AddGuildMemberRole(guildID, userID, roleID Snowflake) (err error) {
err = AddGuildMemberRole(c.req, guildID, userID, roleID)
return
}
// RemoveGuildMemberRole .
func (c *Client) RemoveGuildMemberRole(guildID, userID, roleID Snowflake) (err error) {
err = RemoveGuildMemberRole(c.req, guildID, userID, roleID)
return
}
// RemoveGuildMember .
func (c *Client) RemoveGuildMember(guildID, userID Snowflake) (err error) {
err = RemoveGuildMember(c.req, guildID, userID)
return
}
// GetGuildBans .
func (c *Client) GetGuildBans(id Snowflake) (ret []*Ban, err error) {
ret, err = GetGuildBans(c.req, id)
return
}
// GetGuildBan .
func (c *Client) GetGuildBan(guildID, userID Snowflake) (ret *Ban, err error) {
ret, err = GetGuildBan(c.req, guildID, userID)
return
}
// CreateGuildBan .
func (c *Client) CreateGuildBan(guildID, userID Snowflake, params *CreateGuildBanParams) (err error) {
err = CreateGuildBan(c.req, guildID, userID, params)
return
}
// RemoveGuildBan .
func (c *Client) RemoveGuildBan(guildID, userID Snowflake) (err error) {
err = RemoveGuildBan(c.req, guildID, userID)
return
}
// GetGuildRoles .
func (c *Client) GetGuildRoles(guildID Snowflake) (ret []*Role, err error) {
ret, err = c.cache.GetGuildRoles(guildID)
if err != nil {
ret, err = GetGuildRoles(c.req, guildID)
if err != nil {
return
}
c.cache.SetGuildRoles(guildID, ret)
}
return
}
// CreateGuildRole .
func (c *Client) CreateGuildRole(id Snowflake, params *CreateGuildRoleParams) (ret *Role, err error) {
ret, err = CreateGuildRole(c.req, id, params)
return
}
// ModifyGuildRolePositions .
func (c *Client) ModifyGuildRolePositions(guildID Snowflake, params []ModifyGuildRolePositionsParams) (ret []*Role, err error) {
ret, err = ModifyGuildRolePositions(c.req, guildID, params)
return
}
// ModifyGuildRole .
func (c *Client) ModifyGuildRole(guildID, roleID Snowflake, params *ModifyGuildRoleParams) (ret *Role, err error) {
ret, err = ModifyGuildRole(c.req, guildID, roleID, params)
return
}
// DeleteGuildRole .
func (c *Client) DeleteGuildRole(guildID, roleID Snowflake) (err error) {
err = DeleteGuildRole(c.req, guildID, roleID)
return
}
// GetGuildPruneCount .
func (c *Client) GetGuildPruneCount(id Snowflake, params *GuildPruneParams) (ret *GuildPruneCount, err error) {
ret, err = GetGuildPruneCount(c.req, id, params)
return
}
// BeginGuildPrune .
func (c *Client) BeginGuildPrune(id Snowflake, params *GuildPruneParams) (ret *GuildPruneCount, err error) {
ret, err = BeginGuildPrune(c.req, id, params)
return
}
// GetGuildVoiceRegions .
func (c *Client) GetGuildVoiceRegions(id Snowflake) (ret []*VoiceRegion, err error) {
ret, err = GetGuildVoiceRegions(c.req, id)
return
}
// GetGuildInvites .
func (c *Client) GetGuildInvites(id Snowflake) (ret []*Invite, err error) {
ret, err = GetGuildInvites(c.req, id)
return
}
// GetGuildIntegrations .
func (c *Client) GetGuildIntegrations(id Snowflake) (ret []*Integration, err error) {
ret, err = GetGuildIntegrations(c.req, id)
return
}
// CreateGuildIntegration .
func (c *Client) CreateGuildIntegration(guildID Snowflake, params *CreateGuildIntegrationParams) (err error) {
err = CreateGuildIntegration(c.req, guildID, params)
return
}
// ModifyGuildIntegration .
func (c *Client) ModifyGuildIntegration(guildID, integrationID Snowflake, params *ModifyGuildIntegrationParams) (err error) {
err = ModifyGuildIntegration(c.req, guildID, integrationID, params)
return
}
// DeleteGuildIntegration .
func (c *Client) DeleteGuildIntegration(guildID, integrationID Snowflake) (err error) {
err = DeleteGuildIntegration(c.req, guildID, integrationID)
return
}
// SyncGuildIntegration .
func (c *Client) SyncGuildIntegration(guildID, integrationID Snowflake) (err error) {
err = SyncGuildIntegration(c.req, guildID, integrationID)
return
}
// GetGuildEmbed .
func (c *Client) GetGuildEmbed(guildID Snowflake) (ret *GuildEmbed, err error) {
ret, err = GetGuildEmbed(c.req, guildID)
return
}
// ModifyGuildEmbed .
func (c *Client) ModifyGuildEmbed(guildID Snowflake, params *GuildEmbed) (ret *GuildEmbed, err error) {
ret, err = ModifyGuildEmbed(c.req, guildID, params)
return
}
// GetGuildVanityURL .
func (c *Client) GetGuildVanityURL(guildID Snowflake) (ret *PartialInvite, err error) {
ret, err = GetGuildVanityURL(c.req, guildID)
return
}
// Webhook
// CreateWebhook .
func (c *Client) CreateWebhook(channelID Snowflake, params *CreateWebhookParams) (ret *Webhook, err error) {
ret, err = CreateWebhook(c.req, channelID, params)
return
}
// GetChannelWebhooks .
func (c *Client) GetChannelWebhooks(channelID Snowflake) (ret []*Webhook, err error) {
ret, err = GetChannelWebhooks(c.req, channelID)
return
}
// GetGuildWebhooks .
func (c *Client) GetGuildWebhooks(guildID Snowflake) (ret []*Webhook, err error) {
ret, err = GetGuildWebhooks(c.req, guildID)
return
}
// GetWebhook .
func (c *Client) GetWebhook(id Snowflake) (ret *Webhook, err error) {
ret, err = GetWebhook(c.req, id)
return
}
// GetWebhookWithToken .
func (c *Client) GetWebhookWithToken(id Snowflake, token string) (ret *Webhook, err error) {
ret, err = GetWebhookWithToken(c.req, id, token)
return
}
// ModifyWebhook .
func (c *Client) ModifyWebhook(id Snowflake, params *ModifyWebhookParams) (ret *Webhook, err error) {
if id.Empty() {
err = errors.New("given webhook ID was not set, there is nothing to modify")
return
}
if params == nil {
err = errors.New("given param object was nil, there is nothing to modify")
return
}
if params.Empty() {
err = errors.New("given param object was empty, there is nothing to modify")
return
}
// verify avatar string prefix
if params.avatarIsSet && params.avatar != "" && !validAvatarPrefix(params.avatar) {
err = errors.New("given avatar string is invalid. Must specify data encoding. Eg. data:image/jpeg;base64,")
return
}
// TODO: check if user has permission to modify webhook
ret, err = ModifyWebhook(c.req, id, params)
return
}
// ModifyWebhookWithToken .
func (c *Client) ModifyWebhookWithToken(newWebhook *Webhook) (ret *Webhook, err error) {
ret, err = ModifyWebhookWithToken(c.req, newWebhook)
return
}
// DeleteWebhook .
func (c *Client) DeleteWebhook(webhookID Snowflake) (err error) {
err = DeleteWebhook(c.req, webhookID)
return
}
// DeleteWebhookWithToken .
func (c *Client) DeleteWebhookWithToken(id Snowflake, token string) (err error) {
err = DeleteWebhookWithToken(c.req, id, token)
return
}
// ExecuteWebhook .
func (c *Client) ExecuteWebhook(params *ExecuteWebhookParams, wait bool, URLSuffix string) (err error) {
err = ExecuteWebhook(c.req, params, wait, URLSuffix)
return
}
// ExecuteSlackWebhook .
func (c *Client) ExecuteSlackWebhook(params *ExecuteWebhookParams, wait bool) (err error) {
err = ExecuteSlackWebhook(c.req, params, wait)
return
}
// ExecuteGitHubWebhook .
func (c *Client) ExecuteGitHubWebhook(params *ExecuteWebhookParams, wait bool) (err error) {
err = ExecuteGitHubWebhook(c.req, params, wait)
return
}
//
// #########################################################################
//
//
// Custom methods are usually reused by the resource package for readability
// -----