|
1
|
|
|
package bot |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"github.com/bwmarrin/discordgo" |
|
5
|
|
|
) |
|
6
|
|
|
|
|
7
|
|
|
type ( |
|
8
|
|
|
// Session structure with radio player and voice connection |
|
9
|
|
|
Session struct { |
|
10
|
|
|
Queue *SongQueue |
|
11
|
|
|
Player RadioPlayer |
|
12
|
|
|
guildID, ChannelID string |
|
13
|
|
|
connection *Connection |
|
14
|
|
|
Volume float32 |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
// SessionManager contains all sessions |
|
18
|
|
|
SessionManager struct { |
|
19
|
|
|
sessions map[string]*Session |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// JoinProperties voice connection properties struct |
|
23
|
|
|
JoinProperties struct { |
|
24
|
|
|
Muted bool |
|
25
|
|
|
Deafened bool |
|
26
|
|
|
} |
|
27
|
|
|
) |
|
28
|
|
|
|
|
29
|
|
|
// Creates and returns new session |
|
30
|
|
|
func newSession(newGuildID, newChannelID string, conn *Connection, volume float32) *Session { |
|
31
|
|
|
session := &Session{ |
|
32
|
|
|
Queue: newSongQueue(), |
|
33
|
|
|
guildID: newGuildID, |
|
34
|
|
|
ChannelID: newChannelID, |
|
35
|
|
|
connection: conn, |
|
36
|
|
|
Volume: volume, |
|
37
|
|
|
} |
|
38
|
|
|
return session |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// GetConnection returns vice connection struct |
|
42
|
|
|
func (sess *Session) GetConnection() *Connection { |
|
43
|
|
|
return sess.connection |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Play starts to play radio |
|
47
|
|
|
func (sess *Session) Play(source string, volume float32) error { |
|
48
|
|
|
return sess.connection.Play(source, volume) |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// PlayYoutube starts to play song from youtube |
|
52
|
|
|
func (sess Session) PlayYoutube(song Song) error { |
|
53
|
|
|
return sess.connection.PlayYoutube(song.Ffmpeg(sess.Volume)) |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Stop stops radio |
|
57
|
|
|
func (sess *Session) Stop() { |
|
58
|
|
|
sess.connection.Stop() |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// NewSessionManager creates and returns new session manager |
|
62
|
|
|
func NewSessionManager() *SessionManager { |
|
63
|
|
|
return &SessionManager{make(map[string]*Session)} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// GetByGuild returns session by guild ID |
|
67
|
|
|
func (manager *SessionManager) GetByGuild(guildID string) *Session { |
|
68
|
|
|
for _, sess := range manager.sessions { |
|
69
|
|
|
if sess.guildID == guildID { |
|
70
|
|
|
return sess |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return nil |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// GetByChannel returns session by channel ID |
|
77
|
|
|
func (manager *SessionManager) GetByChannel(channelID string) (*Session, bool) { |
|
78
|
|
|
sess, found := manager.sessions[channelID] |
|
79
|
|
|
return sess, found |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Join add bot to voice channel |
|
83
|
|
|
func (manager *SessionManager) Join(discord *discordgo.Session, guildID, channelID string, |
|
84
|
|
|
properties JoinProperties, volume float32) (*Session, error) { |
|
85
|
|
|
vc, err := discord.ChannelVoiceJoin(guildID, channelID, properties.Muted, properties.Deafened) |
|
86
|
|
|
if err != nil { |
|
87
|
|
|
return nil, err |
|
88
|
|
|
} |
|
89
|
|
|
sess := newSession(guildID, channelID, NewConnection(vc), volume) |
|
90
|
|
|
manager.sessions[channelID] = sess |
|
91
|
|
|
return sess, nil |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Leave remove bot from voice channel |
|
95
|
|
|
func (manager *SessionManager) Leave(discord *discordgo.Session, session Session) { |
|
96
|
|
|
session.connection.Stop() |
|
97
|
|
|
session.connection.Disconnect() |
|
98
|
|
|
delete(manager.sessions, session.ChannelID) |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Count returns count of voice sessions |
|
102
|
|
|
func (manager *SessionManager) Count() int { |
|
103
|
|
|
return len(manager.sessions) |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
func (manager *SessionManager) GetChannels() []string { |
|
|
|
|
|
|
107
|
|
|
var ids []string |
|
108
|
|
|
for _,s := range manager.sessions { |
|
109
|
|
|
ids = append(ids, s.ChannelID) |
|
110
|
|
|
} |
|
111
|
|
|
return ids |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
func (manager *SessionManager) GetGuilds() []string { |
|
|
|
|
|
|
115
|
|
|
var ids []string |
|
116
|
|
|
for _,s := range manager.sessions { |
|
117
|
|
|
ids = append(ids, s.guildID) |
|
118
|
|
|
} |
|
119
|
|
|
return ids |
|
120
|
|
|
} |