Passed
Push — master ( 67951a...37a2ed )
by Viktor
01:42
created

bot.*SessionManager.GetChannels   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 {
0 ignored issues
show
introduced by
exported method SessionManager.GetChannels should have comment or be unexported
Loading history...
107
	var ids []string
108
	for _,s := range manager.sessions {
109
		ids = append(ids, s.ChannelID)
110
	}
111
	return ids
112
}