Passed
Push — master ( 3f894b...f30260 )
by Viktor
01:48
created

bot.*SessionManager.Join   B

Complexity

Conditions 6

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nop 5
dl 0
loc 26
rs 8.4666
c 0
b 0
f 0
1
package bot
2
3
import (
4
	"github.com/bwmarrin/discordgo"
5
	"errors"
6
	"log"
7
)
8
9
type (
10
	// Session structure with radio player and voice connection
11
	Session struct {
12
		Queue              *SongQueue
13
		Player             RadioPlayer
14
		guildID, ChannelID string
15
		connection         *Connection
16
		Volume             float32
17
	}
18
19
	// SessionManager contains all sessions
20
	SessionManager struct {
21
		sessions map[string]*Session
22
	}
23
24
	// JoinProperties voice connection properties struct
25
	JoinProperties struct {
26
		Muted    bool
27
		Deafened bool
28
	}
29
)
30
31
// Creates and returns new session
32
func newSession(newGuildID, newChannelID string, conn *Connection, volume float32) *Session {
33
	session := &Session{
34
		Queue:      newSongQueue(),
35
		guildID:    newGuildID,
36
		ChannelID:  newChannelID,
37
		connection: conn,
38
		Volume:     volume,
39
	}
40
	return session
41
}
42
43
// GetConnection returns vice connection struct
44
func (sess *Session) GetConnection() *Connection {
45
	return sess.connection
46
}
47
48
// Play starts to play radio
49
func (sess *Session) Play(source string, volume float32) error {
50
	return sess.connection.Play(source, volume)
51
}
52
53
// PlayYoutube starts to play song from youtube
54
func (sess Session) PlayYoutube(song Song) error {
55
	return sess.connection.PlayYoutube(song.Ffmpeg(sess.Volume))
56
}
57
58
// Stop stops radio
59
func (sess *Session) Stop() {
60
	sess.connection.Stop()
61
}
62
63
// NewSessionManager creates and returns new session manager
64
func NewSessionManager() *SessionManager {
65
	return &SessionManager{make(map[string]*Session)}
66
}
67
68
// GetByGuild returns session by guild ID
69
func (manager *SessionManager) GetByGuild(guildID string) *Session {
70
	for _, sess := range manager.sessions {
71
		if sess.guildID == guildID {
72
			return sess
73
		}
74
	}
75
	return nil
76
}
77
78
// GetByChannel returns session by channel ID
79
func (manager *SessionManager) GetByChannel(channelID string) (*Session, bool) {
80
	sess, found := manager.sessions[channelID]
81
	return sess, found
82
}
83
84
func (manager *SessionManager) GetAll() map[string]*Session {
0 ignored issues
show
introduced by
exported method SessionManager.GetAll should have comment or be unexported
Loading history...
85
	return manager.sessions
86
}
87
88
func (s *Session) IsOk() bool {
0 ignored issues
show
introduced by
exported method Session.IsOk should have comment or be unexported
Loading history...
introduced by
receiver name s should be consistent with previous receiver name sess for Session
Loading history...
89
	if s.connection.voiceConnection != nil {
90
		if s.connection.voiceConnection.Ready == true {
91
			return true
92
		}
93
	}
94
	return false
95
}
96
97
// Join add bot to voice channel
98
func (manager *SessionManager) Join(discord *discordgo.Session, guildID, channelID string,
99
	properties JoinProperties, volume float32) (*Session, error) {
100
	vc, err := discord.ChannelVoiceJoin(guildID, channelID, properties.Muted, properties.Deafened)
101
	if err != nil {
102
		if vc != nil {
103
			err := vc.Disconnect()
104
			if err != nil {
105
				log.Println(err)
106
			}
107
			vc.Close()
108
		}
109
110
		return nil, err
111
	}
112
	log.Println("Voice joined")
113
	if vc == nil {
114
		return nil, errors.New("no voice connection")
115
	}
116
	if vc.Ready != true {
117
		return nil, errors.New("voice connection not ready")
118
	}
119
	log.Println("Voice connection OK and Ready")
120
	sess := newSession(guildID, channelID, NewConnection(vc), volume)
121
	manager.sessions[channelID] = sess
122
	log.Println("Voice session created and saved")
123
	return sess, nil
124
}
125
126
// Leave remove bot from voice channel
127
func (manager *SessionManager) Leave(discord *discordgo.Session, session Session) {
128
	session.connection.Stop()
129
	session.connection.Disconnect()
130
	delete(manager.sessions, session.ChannelID)
131
}
132
133
// Count returns count of voice sessions
134
func (manager *SessionManager) Count() int {
135
	return len(manager.sessions)
136
}
137
138
func (manager *SessionManager) GetChannels() []string {
0 ignored issues
show
introduced by
exported method SessionManager.GetChannels should have comment or be unexported
Loading history...
139
	var ids []string
140
	for _,s := range manager.sessions {
141
		ids = append(ids, s.ChannelID)
142
	}
143
	return ids
144
}
145
146
func (manager *SessionManager) GetGuilds() []string {
0 ignored issues
show
introduced by
exported method SessionManager.GetGuilds should have comment or be unexported
Loading history...
147
	var ids []string
148
	for _,s := range manager.sessions {
149
		ids = append(ids, s.guildID)
150
	}
151
	return ids
152
}