Remove unused code and general cleanup
parent
10e5400fd5
commit
bba1bc5fa0
10
bot.go
10
bot.go
|
@ -5,18 +5,15 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/dpup/gohubbub"
|
|
||||||
"github.com/faiface/beep"
|
"github.com/faiface/beep"
|
||||||
"github.com/gohugoio/hugo/cache/filecache"
|
"github.com/gohugoio/hugo/cache/filecache"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/kataras/go-events"
|
"github.com/kataras/go-events"
|
||||||
"github.com/r3labs/sse/v2"
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"google.golang.org/api/youtube/v3"
|
"google.golang.org/api/youtube/v3"
|
||||||
|
@ -56,13 +53,9 @@ type App struct {
|
||||||
events events.EventEmmiter
|
events events.EventEmmiter
|
||||||
next bool
|
next bool
|
||||||
db *pgx.Conn
|
db *pgx.Conn
|
||||||
sse *sse.Server
|
|
||||||
router *httprouter.Router
|
router *httprouter.Router
|
||||||
active []string
|
active []string
|
||||||
plidx int
|
plidx int
|
||||||
playlist *Playlist
|
|
||||||
plm *sync.RWMutex
|
|
||||||
hubbub *gohubbub.Client
|
|
||||||
cache *filecache.Cache
|
cache *filecache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,11 +63,10 @@ func main() {
|
||||||
bfs := afero.NewBasePathFs(afero.NewOsFs(), "cache")
|
bfs := afero.NewBasePathFs(afero.NewOsFs(), "cache")
|
||||||
app.cache = filecache.NewCache(bfs, 1*time.Hour, "")
|
app.cache = filecache.NewCache(bfs, 1*time.Hour, "")
|
||||||
|
|
||||||
app.plm = &sync.RWMutex{}
|
|
||||||
ticker := time.NewTicker(300 * time.Millisecond)
|
ticker := time.NewTicker(300 * time.Millisecond)
|
||||||
|
|
||||||
sc := make(chan os.Signal, 1)
|
sc := make(chan os.Signal, 1)
|
||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
32
events.go
32
events.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"dndmusicbot/ffmpeg"
|
"dndmusicbot/ffmpeg"
|
||||||
discordspeaker "dndmusicbot/speaker"
|
discordspeaker "dndmusicbot/speaker"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -195,29 +194,6 @@ func (app *App) songPosition(payload ...interface{}) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) checkQueue(payload ...interface{}) {
|
|
||||||
if !app.queue.IsPlaying() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// This needs some tweaking.
|
|
||||||
if app.queue.playing && !app.next && app.queue.QLen() == 0 {
|
|
||||||
|
|
||||||
log.Println("Queue is 0. It should never be 0..")
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
if app.queue.QLen() == 0 {
|
|
||||||
log.Println("Queue is still 0. Queueing a song.")
|
|
||||||
app.events.Emit("next")
|
|
||||||
}
|
|
||||||
log.Println("Seems queue is filled, doing nothing.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) checkTimeleft(payload ...interface{}) {
|
func (app *App) checkTimeleft(payload ...interface{}) {
|
||||||
if !app.queue.IsPlaying() {
|
if !app.queue.IsPlaying() {
|
||||||
return
|
return
|
||||||
|
@ -389,6 +365,10 @@ func (app *App) loadPlaylist(payload ...interface{}) {
|
||||||
}
|
}
|
||||||
return uri, nil
|
return uri, nil
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
ff, err := ffmpeg.NewPCM(string(yt), sampleRate, channels)
|
ff, err := ffmpeg.NewPCM(string(yt), sampleRate, channels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -409,7 +389,3 @@ func (app *App) loadPlaylist(payload ...interface{}) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) preloadSong(payload ...interface{}) {
|
|
||||||
app.queue.Preload()
|
|
||||||
}
|
|
||||||
|
|
5
queue.go
5
queue.go
|
@ -47,7 +47,6 @@ func (s *Song) NewStream() (err error) {
|
||||||
type Queue struct {
|
type Queue struct {
|
||||||
Events events.EventEmmiter
|
Events events.EventEmmiter
|
||||||
playing bool
|
playing bool
|
||||||
yt VideoInfo
|
|
||||||
list *list.List
|
list *list.List
|
||||||
current *list.Element
|
current *list.Element
|
||||||
}
|
}
|
||||||
|
@ -106,7 +105,6 @@ func (q *Queue) Next() {
|
||||||
}
|
}
|
||||||
|
|
||||||
q.current = next
|
q.current = next
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue) Prev() {
|
func (q *Queue) Prev() {
|
||||||
|
@ -123,9 +121,8 @@ func (q *Queue) Prev() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
q.current = prev
|
|
||||||
|
|
||||||
return
|
q.current = prev
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue) Preload() {
|
func (q *Queue) Preload() {
|
||||||
|
|
5
ws.go
5
ws.go
|
@ -18,12 +18,11 @@ func init() {
|
||||||
ws_msg = make(chan interface{})
|
ws_msg = make(chan interface{})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
var msg interface{}
|
||||||
for {
|
for {
|
||||||
select {
|
msg = <-ws_msg
|
||||||
case msg := <-ws_msg:
|
|
||||||
ws_clients.Send(msg)
|
ws_clients.Send(msg)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Println("ws.go done.")
|
log.Println("ws.go done.")
|
||||||
|
|
17
youtube.go
17
youtube.go
|
@ -14,16 +14,11 @@ import (
|
||||||
|
|
||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
|
|
||||||
"github.com/faiface/beep"
|
|
||||||
"github.com/sosodev/duration"
|
"github.com/sosodev/duration"
|
||||||
"google.golang.org/api/option"
|
"google.golang.org/api/option"
|
||||||
"google.golang.org/api/youtube/v3"
|
"google.golang.org/api/youtube/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
yt_url = "https://www.youtube.com/watch?v=%s"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.Println("youtube.go loading..")
|
log.Println("youtube.go loading..")
|
||||||
|
|
||||||
|
@ -38,14 +33,6 @@ func init() {
|
||||||
log.Println("youtube.go done.")
|
log.Println("youtube.go done.")
|
||||||
}
|
}
|
||||||
|
|
||||||
type YT struct {
|
|
||||||
dst io.WriteCloser
|
|
||||||
pcm io.ReadCloser
|
|
||||||
dur time.Duration
|
|
||||||
pos time.Duration
|
|
||||||
f beep.Format
|
|
||||||
}
|
|
||||||
|
|
||||||
func ShufflePlaylist(list []string) ([]string, error) {
|
func ShufflePlaylist(list []string) ([]string, error) {
|
||||||
seedb := make([]byte, 32)
|
seedb := make([]byte, 32)
|
||||||
_, err := rand.Read(seedb)
|
_, err := rand.Read(seedb)
|
||||||
|
@ -110,7 +97,7 @@ func (app App) Video(vid string) (out VideoInfo, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resp.Items) != 1 {
|
if len(resp.Items) != 1 {
|
||||||
return nil, errors.New("Response contains not 1 item!")
|
return nil, errors.New("response contains not 1 item")
|
||||||
}
|
}
|
||||||
|
|
||||||
video := resp.Items[0]
|
video := resp.Items[0]
|
||||||
|
@ -165,6 +152,4 @@ func (app App) Playlist(playlist string) ([]string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
|
|
||||||
//return fmt.Sprintf(yt_url, list[rand.Intn(len(list))])
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue