-
Notifications
You must be signed in to change notification settings - Fork 0
/
muzikas.go
216 lines (191 loc) · 5.12 KB
/
muzikas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package muzikas
import (
"fmt"
"io"
"log"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/jonas747/dca"
)
type BotStatus int32
const (
Resting BotStatus = 0
Playing = 1
Paused = 2
Err = 3
)
type MuzikasBot struct {
session *discordgo.Session
voiceConn *discordgo.VoiceConnection
queue chan *Song
queueList []string
skipInterrupt chan bool
currentStream *dca.StreamingSession
botStatus BotStatus
}
func NewMuzikasBot(session *discordgo.Session) *MuzikasBot {
return &MuzikasBot{
session: session,
queue: make(chan *Song, 100),
skipInterrupt: make(chan bool, 1),
botStatus: Resting,
}
}
func (mb *MuzikasBot) Start() {
log.Println(`
♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫
♫♫♫♫♫♫♫♫STARTING MUZIKAS♫♫♫♫♫♫
♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫♫
`)
mb.session.AddHandler(messageCreatedHandler(mb))
mb.session.Open()
}
func messageCreatedHandler(mb *MuzikasBot) func(*discordgo.Session, *discordgo.MessageCreate) {
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
switch strings.Split(m.Message.Content, " ")[0] {
case "!play":
c, _ := s.State.Channel(m.Message.ChannelID)
g, _ := s.State.Guild(c.GuildID)
for _, vs := range g.VoiceStates {
if vs.UserID == m.Message.Author.ID {
if mb.voiceConn == nil {
conn, err := mb.session.ChannelVoiceJoin(g.ID, vs.ChannelID, false, true)
if err != nil {
log.Println("Error connecting to voice channel")
s.ChannelMessageSend(m.Message.ChannelID, "Error connecting to voice channel")
log.Println(err.Error())
return
}
mb.voiceConn = conn
}
song, err := GetSongInfo(strings.Split(m.Message.Content, " ")[1])
if err != nil {
log.Println("Error fetching song to play")
s.ChannelMessageSend(m.Message.ChannelID, "Error fetching song to play")
return
}
s.ChannelMessageSend(m.Message.ChannelID, fmt.Sprintf("Playing song: %v", song.name))
mb.enqueue(song)
mb.playSong()
}
}
case "!skip":
mb.skip()
case "!list":
var sngList strings.Builder
for _, sng := range mb.queueList {
sngList.WriteString(sng)
}
s.ChannelMessageSend(m.Message.ChannelID, sngList.String())
case "!queue":
song, err := GetSongInfo(strings.Split(m.Message.Content, " ")[1])
if err != nil {
log.Println("Error fetching song to enqueue")
s.ChannelMessageSend(m.Message.ChannelID, "Error fetching song to enqueue")
return
}
s.ChannelMessageSend(m.Message.ChannelID, fmt.Sprintf("Added song to queue: %v", song.name))
mb.enqueue(song)
case "!stop":
s.ChannelMessageSend(m.Message.ChannelID, "Stopping music")
mb.stop()
case "!pause":
mb.pause()
case "!unpause":
mb.unpause()
default:
log.Println("Unknown command")
}
}
}
func (mb *MuzikasBot) playSong() {
song := mb.dequeue()
log.Printf("Playing song: %v \n", song.name)
options := dca.StdEncodeOptions
options.RawOutput = true
options.Bitrate = 96
options.Application = "lowdelay"
encodingSession, err := dca.EncodeFile(song.downloadUrl, options)
if err != nil {
log.Println("Error encoding from yt url")
log.Println(err.Error())
return
}
defer encodingSession.Cleanup()
time.Sleep(250 * time.Millisecond)
err = mb.voiceConn.Speaking(true)
if err != nil {
log.Println("Error connecting to discord voice")
log.Println(err.Error())
}
done := make(chan error)
stream := dca.NewStream(encodingSession, mb.voiceConn, done)
mb.currentStream = stream
log.Println("Created stream, waiting on finish or err")
mb.botStatus = Playing
select {
case err := <-done:
log.Println("Song done")
if err != nil && err != io.EOF {
mb.botStatus = Err
log.Println(err.Error())
return
}
mb.voiceConn.Speaking(false)
break
case _ = <-mb.skipInterrupt:
log.Println("Song interrupted, stop playing")
mb.voiceConn.Speaking(false)
return
}
mb.voiceConn.Speaking(false)
if len(mb.queue) == 0 {
time.Sleep(250 * time.Millisecond)
log.Println("Audio done")
mb.stop()
mb.botStatus = Resting
return
}
time.Sleep(250 * time.Millisecond)
log.Println("Play next in queue")
go mb.playSong()
return
}
func (mb *MuzikasBot) skip() {
if len(mb.queue) == 0 {
mb.stop()
} else {
if len(mb.skipInterrupt) == 0 {
mb.skipInterrupt <- true
mb.playSong()
}
}
}
func (mb *MuzikasBot) enqueue(song *Song) {
log.Printf("Queueing song %v", song.name)
songString := fmt.Sprintf("-- :%v \n", song.name)
mb.queueList = append(mb.queueList, songString)
mb.queue <- song
}
func (mb *MuzikasBot) dequeue() *Song {
mb.queueList = mb.queueList[1:]
return <-mb.queue
}
func (mb *MuzikasBot) stop() {
mb.voiceConn.Disconnect()
mb.voiceConn = nil
mb.botStatus = Resting
}
func (mb *MuzikasBot) pause() {
if mb.currentStream != nil {
mb.currentStream.SetPaused(true)
log.Println("Paused playback")
}
}
func (mb *MuzikasBot) unpause() {
if mb.currentStream != nil {
mb.currentStream.SetPaused(false)
log.Println("Unpaused playback")
}
}