Passed
Push — master ( 7335fd...fcb1a0 )
by Viktor
01:34
created

bot.NewSessionManager   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
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
	}
15
16
	// SessionManager contains all sessions
17
	SessionManager struct {
18
		sessions map[string]*Session
19
	}
20
21
	// JoinProperties voice connection properties struct
22
	JoinProperties struct {
23
		Muted    bool
24
		Deafened bool
25
	}
26
)
27
28
// Creates and returns new session
29
func newSession(newGuildID, newChannelID string, conn *Connection) *Session {
30
	session := &Session{
31
		Queue:      newSongQueue(),
32
		guildID:    newGuildID,
33
		ChannelID:  newChannelID,
34
		connection: conn,
35
	}
36
	return session
37
}
38
39
// GetConnection returns vice connection struct
40
func (sess *Session) GetConnection() *Connection {
41
	return sess.connection
42
}
43
44
// Play starts to play radio
45
func (sess Session) Play(source string) error {
46
	return sess.connection.Play(source)
47
}
48
49
// PlayYoutube starts to play song from youtube
50
func (sess Session) PlayYoutube(song Song) error {
51
	return sess.connection.PlayYoutube(song.Ffmpeg())
52
}
53
54
// Stop stops radio
55
func (sess *Session) Stop() {
56
	sess.connection.Stop()
57
}
58
59
// NewSessionManager creates and returns new session manager
60
func NewSessionManager() *SessionManager {
61
	return &SessionManager{make(map[string]*Session)}
62
}
63
64
// GetByGuild returns session by guild ID
65
func (manager *SessionManager) GetByGuild(guildID string) *Session {
66
	for _, sess := range manager.sessions {
67
		if sess.guildID == guildID {
68
			return sess
69
		}
70
	}
71
	return nil
72
}
73
74
// GetByChannel returns session by channel ID
75
func (manager SessionManager) GetByChannel(channelID string) (*Session, bool) {
76
	sess, found := manager.sessions[channelID]
77
	return sess, found
78
}
79
80
// Join add bot to voice channel
81
func (manager *SessionManager) Join(discord *discordgo.Session, guildID, channelID string,
82
	properties JoinProperties) (*Session, error) {
83
	vc, err := discord.ChannelVoiceJoin(guildID, channelID, properties.Muted, properties.Deafened)
84
	if err != nil {
85
		return nil, err
86
	}
87
	sess := newSession(guildID, channelID, NewConnection(vc))
88
	manager.sessions[channelID] = sess
89
	return sess, nil
90
}
91
92
// Leave remove bot from voice channel
93
func (manager *SessionManager) Leave(discord *discordgo.Session, session Session) {
94
	session.connection.Stop()
95
	session.connection.Disconnect()
96
	delete(manager.sessions, session.ChannelID)
97
}
98
99
func (manager *SessionManager) Count() int {
0 ignored issues
show
introduced by
exported method SessionManager.Count should have comment or be unexported
Loading history...
100
	return len(manager.sessions)
101
}