forked from Necroforger/dream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
785 lines (642 loc) · 18 KB
/
api.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
package dream
import (
"errors"
"fmt"
"log"
"sort"
"github.com/jonas747/dca"
//Blank imports included for decoding a user's avatar into an image.
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"os"
"github.com/bwmarrin/discordgo"
)
/* TODO
TODO: Improve GuildMemberRoleAdd, GuildMemberRoleRemove methods to accept interfaces to be more convenient.
It could possibly obtain the guildID from an interface.
*/
//Error values//
var (
ErrInvalidType = errors.New("err: invalid type")
ErrNotFound = errors.New("err: not found")
ErrInvalidArgLength = errors.New("err: invalid argument length")
)
// MessageFromInterface returns the underlying message struct of an event relating to messages
// Such as MessageCreate, MessageDelete, MessageUpdate.
func (s *Session) MessageFromInterface(i interface{}) (*discordgo.Message, error) {
switch t := i.(type) {
case *discordgo.MessageCreate:
return t.Message, nil
case *discordgo.MessageUpdate:
return t.Message, nil
case *discordgo.MessageDelete:
return t.Message, nil
case *Message:
return t.Message, nil
case *discordgo.Message:
return t, nil
default:
return nil, ErrInvalidType
}
}
// ChannelID Returns the channelID from a variety of objects
func (s *Session) ChannelID(i interface{}) (string, error) {
// Attempt to retrieve a message object from the interface
if m, err := s.MessageFromInterface(i); err == nil {
return m.ChannelID, nil
}
// If no message object is found check for other types containing a channelID.
switch t := i.(type) {
case string:
return t, nil
case *discordgo.VoiceState:
return t.ChannelID, nil
case *discordgo.VoiceConnection:
return t.ChannelID, nil
}
return "", ErrInvalidType
}
// GuildID returns the GuildID from a variety of objects
func (s *Session) GuildID(i interface{}) (string, error) {
// Attempt to get the Message object from the interface.
// If it fails, check the other possible types.
if t, err := s.MessageFromInterface(i); err == nil {
c, err := s.Channel(t.ChannelID)
if err != nil {
return "", err
}
return c.GuildID, nil
}
// Check for other types
switch t := i.(type) {
case string:
return t, nil
case *discordgo.Channel:
return t.GuildID, nil
case *discordgo.Guild:
return t.ID, nil
case *discordgo.VoiceConnection:
return t.GuildID, nil
case *discordgo.Member:
return t.GuildID, nil
default:
return "", ErrInvalidType
}
}
// UserID returns the userID from a variety of objects.
func (s *Session) UserID(i interface{}) (userid string, err error) {
if t, err := s.MessageFromInterface(i); err == nil {
if t.Author != nil {
return t.Author.ID, nil
}
}
switch t := i.(type) {
case string:
return t, nil
case *discordgo.User:
return t.ID, nil
case *discordgo.Member:
return t.User.ID, nil
case *discordgo.Presence:
return t.User.ID, nil
default:
return "", ErrInvalidType
}
}
// Channel is a convenience method for retrieving a channel from a variety of objects
func (s *Session) Channel(i interface{}) (*discordgo.Channel, error) {
// Return channel if `i` is already of type channel
if c, ok := i.(*discordgo.Channel); ok {
return c, nil
}
channelid, err := s.ChannelID(i)
if err != nil {
return nil, err
}
c, err := s.DG.State.Channel(channelid)
if err != nil {
c, err = s.DG.Channel(channelid)
if err != nil {
return nil, err
}
}
return c, nil
}
// ChannelVoiceJoin joins the specified voice channel
func (s *Session) ChannelVoiceJoin(guildID, channelID string, mute, deaf bool) (*discordgo.VoiceConnection, error) {
vc, err := s.DG.ChannelVoiceJoin(guildID, channelID, mute, deaf)
if err != nil {
return nil, err
}
return vc, nil
}
// UserVoiceState finds a user's voice state from all the guilds in the session
func (s *Session) UserVoiceState(userID string) (*discordgo.VoiceState, error) {
for _, v := range s.DG.State.Guilds {
for _, c := range v.VoiceStates {
if c.UserID == userID {
return c, nil
}
}
}
return nil, errors.New("VoiceState not found")
}
// UserVoiceStateJoin joins a user's voice state channel.
func (s *Session) UserVoiceStateJoin(userID interface{}, mute, deaf bool) (*discordgo.VoiceConnection, error) {
uid, err := s.UserID(userID)
if err != nil {
return nil, err
}
vs, err := s.UserVoiceState(uid)
if err != nil {
return nil, err
}
return s.ChannelVoiceJoin(vs.GuildID, vs.ChannelID, mute, deaf)
}
func (s *Session) convertToOpus(dst io.Writer, src io.Reader) error {
// dca.Logger = log.New(os.Stderr, "----", log.Ldate)
encodingSession, err := dca.EncodeMem(src, &dca.EncodeOptions{
Volume: 256,
Channels: 2,
FrameRate: 48000,
FrameDuration: 20,
BufferedFrames: 100,
Bitrate: 96,
Application: dca.AudioApplicationAudio,
CompressionLevel: 10,
PacketLoss: 0,
VBR: true,
RawOutput: true,
})
if err != nil {
return err
}
defer encodingSession.Cleanup()
_, err = io.Copy(dst, encodingSession)
if err != nil {
return err
}
return nil
}
// GuildAudioDispatcherStop a guild's currently playing audio dispatchers
func (s *Session) GuildAudioDispatcherStop(i interface{}) error {
guildID, err := s.GuildID(i)
if err != nil {
return err
}
disp, err := s.audioDispatcher(guildID)
if err != nil {
return err
}
disp.Stop()
return nil
}
// GuildAudioDispatcherPause pauses the guild's currently playing audio dispatcher
func (s *Session) GuildAudioDispatcherPause(i interface{}) error {
guildID, err := s.GuildID(i)
if err != nil {
return err
}
disp, err := s.audioDispatcher(guildID)
if err != nil {
return err
}
disp.Pause()
return nil
}
// GuildAudioDispatcherResume resumes the guild's currently playing audio dispatcher
func (s *Session) GuildAudioDispatcherResume(i interface{}) error {
guildID, err := s.GuildID(i)
if err != nil {
return err
}
disp, err := s.audioDispatcher(guildID)
if err != nil {
return err
}
disp.Resume()
return nil
}
// GuildAudioDispatcher returns the specified guild's audio dispatcher
func (s *Session) GuildAudioDispatcher(i interface{}) (*AudioDispatcher, error) {
guildID, err := s.GuildID(i)
if err != nil {
return nil, err
}
return s.audioDispatcher(guildID)
}
// PlayStream plays an audio stream from the given io reader and uses ffmpeg to convert to a suitable format
func (s *Session) PlayStream(vc *discordgo.VoiceConnection, rd io.ReadCloser) *AudioDispatcher {
opusStream, wr := io.Pipe()
go func() {
err := s.convertToOpus(wr, rd)
if err != nil {
log.Println("error converting audio to opus: ", err)
}
wr.Close()
}()
disp := NewAudioDispatcher(vc, opusStream)
s.GuildAudioDispatcherStop(vc.GuildID)
s.addAudioDispatcher(disp)
go func() {
disp.Start()
}()
return disp
}
// PlayRawStream plays a direct stream
func (s *Session) PlayRawStream(vc *discordgo.VoiceConnection, rd io.ReadCloser) *AudioDispatcher {
disp := NewAudioDispatcher(vc, rd)
s.GuildAudioDispatcherStop(vc.GuildID)
s.addAudioDispatcher(disp)
go func() {
disp.Start()
//b.removeAudioDispatcher(disp.GuildID)
}()
return disp
}
// PlayFile opens a file and calls PlayStream on it
func (s *Session) PlayFile(vc *discordgo.VoiceConnection, path string) (*AudioDispatcher, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
disp := s.PlayStream(vc, f)
return disp, nil
}
// PlayRawFile opens a file and calls PlayRawstream on it
func (s *Session) PlayRawFile(vc *discordgo.VoiceConnection, path string) (*AudioDispatcher, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
disp := s.PlayRawStream(vc, f)
return disp, nil
}
// Guild is a convenience method for retrieving a channel from a variety of objects
func (s *Session) Guild(i interface{}) (*discordgo.Guild, error) {
if g, ok := i.(*discordgo.Guild); ok {
return g, nil
}
guildid, err := s.GuildID(i)
if err != nil {
return nil, err
}
guild, err := s.DG.State.Guild(guildid)
if err != nil {
guild, err = s.DG.Guild(guildid)
if err != nil {
return nil, err
}
}
return guild, nil
}
// TODO Rework GuildPresence
// GuildPresence attempts to first find a guildMember object from the supplied arguments. If a member is found,
// It uses the members guildID and userID.
func (s *Session) GuildPresence(i ...interface{}) (*discordgo.Presence, error) {
var userID string
var guildID string
if len(i) == 0 {
return nil, ErrInvalidArgLength
}
// Return if the first argument is already a presence type
if p, ok := i[0].(*discordgo.Presence); ok {
return p, nil
}
//If there is only one argument, obtain both the GuildID and the UserID from it
//Otherwise, Get the guild ID from the first argument and the UserID from the second.
if len(i) == 1 {
gid, err := s.GuildID(i[0])
if err != nil {
return nil, err
}
uid, err := s.UserID(i[0])
if err != nil {
return nil, err
}
userID, guildID = uid, gid
} else {
//Get guildID from first argument
gid, err := s.GuildID(i[0])
if err != nil {
return nil, err
}
//Get userID from second argument
uid, err := s.UserID(i[1])
if err != nil {
return nil, err
}
userID, guildID = uid, gid
}
p, err := s.DG.State.Presence(guildID, userID)
if err == nil {
return p, nil
}
return nil, ErrNotFound
}
// GuildVoiceConnection returns the voice connection object for the given guild
func (s *Session) GuildVoiceConnection(i interface{}) (*discordgo.VoiceConnection, error) {
guildID, err := s.GuildID(i)
if err != nil {
return nil, err
}
if vc, ok := s.DG.VoiceConnections[guildID]; ok {
return vc, nil
}
return nil, errors.New("Voice connection not found")
}
// GuildVoiceConnectionDisconnect finds the current guild voice connection and disconnects from it
func (s *Session) GuildVoiceConnectionDisconnect(guildID interface{}) error {
vc, err := s.GuildVoiceConnection(guildID)
if err != nil {
return err
}
return vc.Disconnect()
}
// GuildMember is a convenience method for fetching the member object from various sources
func (s *Session) GuildMember(i ...interface{}) (*discordgo.Member, error) {
if len(i) == 0 {
return nil, ErrInvalidArgLength
}
var (
userid string
guildid string
err error
)
// Return if the type is already a member object
if v, ok := i[0].(*discordgo.Member); ok {
return v, nil
}
if len(i) == 1 {
// Attempt to get both the userID and the guildid
// From the first argument
guildid, err = s.GuildID(i[0])
if err != nil {
return nil, err
}
userid, err = s.UserID(i[0])
if err != nil {
return nil, err
}
} else {
// If there are more than one argument present,
// Attempt to get the guildID from the first one
// And the userID from the second
guildid, err = s.GuildID(i[0])
if err != nil {
return nil, err
}
userid, err = s.UserID(i[1])
if err != nil {
return nil, err
}
}
// Fetch member from state and fall back to the restAPI if that fails.
gm, err := s.DG.State.Member(guildid, userid)
if err != nil {
gm, err = s.DG.GuildMember(guildid, userid)
if err != nil {
return nil, err
}
}
return gm, nil
}
// GuildRoles requests a guilds roles from the state or the API if not available in state
func (s *Session) GuildRoles(i interface{}) ([]*discordgo.Role, error) {
guild, err := s.Guild(i)
if err != nil {
return nil, err
}
// Make sure to make a copy of guild.Roles as to not effect the underlying
// Array in guild.Roles.
roles := make([]*discordgo.Role, len(guild.Roles))
copy(roles, guild.Roles)
sort.Sort(Roles(roles))
return roles, nil
}
// GuildMemberRoles returns an array of the roles of a member sorted by position
func (s *Session) GuildMemberRoles(i ...interface{}) ([]*discordgo.Role, error) {
var roles []*discordgo.Role
member, err := s.GuildMember(i...)
if err != nil {
return nil, err
}
guildRoles, err := s.GuildRoles(member.GuildID)
if err != nil {
return nil, err
}
// Find the Member's roles in the guild
for _, gr := range guildRoles {
for _, mr := range member.Roles {
if gr.ID == mr {
roles = append(roles, gr)
}
}
}
return roles, nil
}
// GuildMemberRoleAdd adds the specified role to a given member
// guildID : The ID of a Guild.
// userID : The ID of a User.
// roleID : The ID of a Role to be assigned to the user.
func (s *Session) GuildMemberRoleAdd(guildID, userID interface{}, roleID string) error {
gid, err := s.GuildID(guildID)
if err != nil {
return err
}
uid, err := s.UserID(userID)
if err != nil {
return err
}
return s.DG.GuildMemberRoleAdd(gid, uid, roleID)
}
// GuildMemberRoleAddByName adds a role to the specified guild member
func (s *Session) GuildMemberRoleAddByName(guildID, userID interface{}, name string) error {
guild, err := s.Guild(guildID)
if err != nil {
return err
}
uid, err := s.UserID(userID)
if err != nil {
return err
}
for _, v := range guild.Roles {
if v.Name == name {
return s.DG.GuildMemberRoleAdd(guild.ID, uid, v.ID)
}
}
return ErrNotFound
}
// GuildMemberRoleAddByNames ...
func (s *Session) GuildMemberRoleAddByNames(guildID, userID interface{}, names ...string) (err error) {
guild, err := s.Guild(guildID)
if err != nil {
return
}
uid, err := s.UserID(userID)
if err != nil {
return
}
for _, name := range names {
err = s.GuildMemberRoleAddByName(guild, uid, name)
}
return
}
// GuildMemberRoleRemove removes the specified role to a given member
// guildID : The ID of a Guild.
// userID : The ID of a User.
// roleID : The ID of a Role to be removed from the user.
func (s *Session) GuildMemberRoleRemove(guildID, userID interface{}, roleID string) error {
gid, err := s.GuildID(guildID)
if err != nil {
return err
}
uid, err := s.UserID(userID)
if err != nil {
return err
}
return s.DG.GuildMemberRoleRemove(gid, uid, roleID)
}
// GuildMemberRoleRemoveByName removes a role from a member by name
func (s *Session) GuildMemberRoleRemoveByName(guildID, userID interface{}, rolename string) error {
gid, err := s.GuildID(guildID)
if err != nil {
return err
}
uid, err := s.UserID(userID)
if err != nil {
return err
}
memberRoles, err := s.GuildMemberRoles(gid, uid)
if err != nil {
return err
}
for _, v := range memberRoles {
if v.Name == rolename {
return s.GuildMemberRoleRemove(gid, uid, v.ID)
}
}
return ErrNotFound
}
// GuildMemberRolesRemoveByName removes a list of roles by name from a guild member
func (s *Session) GuildMemberRolesRemoveByName(guildID, userID interface{}, rolenames ...string) (err error) {
gid, err := s.GuildID(guildID)
if err != nil {
return err
}
uid, err := s.UserID(userID)
if err != nil {
return err
}
for _, rolename := range rolenames {
err = s.GuildMemberRoleRemoveByName(gid, uid, rolename)
}
return
}
// RoleSettings is an object passes to GuildRoleCreate or GuildRoleEdit to
// Deal with setting the values of the command easier
// guildID : The ID of a Guild.
// roleID : The ID of a Role.
// name : The name of the Role.
// color : The color of the role (decimal, not hex).
// hoist : Whether to display the role's users separately.
// perm : The permissions for the role.
// mention : Whether this role is mentionable
type RoleSettings struct {
Name string
Color int
Hoist bool
Perm int
Mention bool
}
// GuildRoleCreate creates a role and edits it with the given GuildRoleSettings
// Struct requires paramater [GuildID] to be set
func (s *Session) GuildRoleCreate(guildID string, settings RoleSettings) (*discordgo.Role, error) {
role, err := s.DG.GuildRoleCreate(guildID)
if err != nil {
return nil, err
}
// Wait until the role gets updated in the guild
// fors.NextGuildRoleCreate().Role.ID != role.ID {
// }
return s.GuildRoleEdit(guildID, role.ID, settings)
}
// GuildRoleEdit edit edits the role in the given guild with 'settings'
// Struct requires parameters [GuildID] [RoleID] to be set.
func (s *Session) GuildRoleEdit(guildID, roleID string, settings RoleSettings) (*discordgo.Role, error) {
return s.DG.GuildRoleEdit(
guildID, roleID,
settings.Name, settings.Color, settings.Hoist,
settings.Perm, settings.Mention,
)
}
// GuildRoleDelete deletes a role from a guild
func (s *Session) GuildRoleDelete(i interface{}, roleID string) error {
guild, err := s.Guild(i)
if err != nil {
return err
}
return s.DG.GuildRoleDelete(guild.ID, roleID)
}
// GuildRoleDeleteByName deletes a role from the guild by name.
// The first argument will be used to obtain the Guild.
func (s *Session) GuildRoleDeleteByName(i interface{}, name string) error {
guild, err := s.Guild(i)
if err != nil {
return err
}
for _, v := range guild.Roles {
if v.Name == name {
return s.DG.GuildRoleDelete(guild.ID, v.ID)
}
}
return ErrNotFound
}
// GuildRoleDeleteByNames deletes multiple roles from the guild by name
func (s *Session) GuildRoleDeleteByNames(i interface{}, names ...string) (err error) {
guild, err := s.Guild(i)
if err != nil {
return
}
for _, v := range names {
err = s.GuildRoleDeleteByName(guild, v)
}
return
}
// SendMessage is a convenience method for sending messages to a channel
func (s *Session) SendMessage(i interface{}, text ...interface{}) (*discordgo.Message, error) {
channelid, err := s.ChannelID(i)
if err != nil {
return nil, err
}
return s.DG.ChannelMessageSend(channelid, fmt.Sprint(text...))
}
// SendFile is a convenience method for sending files to a channel
func (s *Session) SendFile(i interface{}, filename string, rd io.Reader) (*discordgo.Message, error) {
channelid, err := s.ChannelID(i)
if err != nil {
return nil, err
}
return s.DG.ChannelFileSend(channelid, filename, rd)
}
// SendEmbed is a convenience method for sending embeds to a channel
func (s *Session) SendEmbed(i interface{}, e interface{}) (*discordgo.Message, error) {
var embed *discordgo.MessageEmbed
switch t := e.(type) {
case *discordgo.MessageEmbed:
embed = t
case *Embed:
embed = t.MessageEmbed
case string:
embed = NewEmbed().SetDescription(t).MessageEmbed
default:
return nil, ErrInvalidType
}
channelid, err := s.ChannelID(i)
if err != nil {
return nil, err
}
return s.DG.ChannelMessageSendEmbed(channelid, embed)
}