Passed
Push — master ( fb5b4f...b9f2ca )
by Viktor
01:40
created

bot.*Session.IsOk   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 0
dl 0
loc 7
rs 10
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
	if vc == nil {
113
		return nil, errors.New("no voice connection")
114
	}
115
	sess := newSession(guildID, channelID, NewConnection(vc), volume)
116
	manager.sessions[channelID] = sess
117
	return sess, nil
118
}
119
120
// Leave remove bot from voice channel
121
func (manager *SessionManager) Leave(discord *discordgo.Session, session Session) {
122
	session.connection.Stop()
123
	session.connection.Disconnect()
124
	delete(manager.sessions, session.ChannelID)
125
}
126
127
// Count returns count of voice sessions
128
func (manager *SessionManager) Count() int {
129
	return len(manager.sessions)
130
}
131
132
func (manager *SessionManager) GetChannels() []string {
0 ignored issues
show
introduced by
exported method SessionManager.GetChannels should have comment or be unexported
Loading history...
133
	var ids []string
134
	for _,s := range manager.sessions {
135
		ids = append(ids, s.ChannelID)
136
	}
137
	return ids
138
}
139
140
func (manager *SessionManager) GetGuilds() []string {
0 ignored issues
show
introduced by
exported method SessionManager.GetGuilds should have comment or be unexported
Loading history...
141
	var ids []string
142
	for _,s := range manager.sessions {
143
		ids = append(ids, s.guildID)
144
	}
145
	return ids
146
}