From bba1bc5fa091dc6ee8221af70f6c6c61f73438d3 Mon Sep 17 00:00:00 2001 From: Stein Ivar Berghei Date: Sun, 20 Nov 2022 17:30:22 +0100 Subject: [PATCH] Remove unused code and general cleanup --- bot.go | 10 +--------- events.go | 32 ++++---------------------------- queue.go | 5 +---- ws.go | 7 +++---- youtube.go | 17 +---------------- 5 files changed, 10 insertions(+), 61 deletions(-) diff --git a/bot.go b/bot.go index 7570a84..53398e8 100644 --- a/bot.go +++ b/bot.go @@ -5,18 +5,15 @@ import ( "log" "os" "os/signal" - "sync" "syscall" "time" "github.com/bwmarrin/discordgo" - "github.com/dpup/gohubbub" "github.com/faiface/beep" "github.com/gohugoio/hugo/cache/filecache" "github.com/jackc/pgx/v5" "github.com/julienschmidt/httprouter" "github.com/kataras/go-events" - "github.com/r3labs/sse/v2" "github.com/spf13/afero" "github.com/spf13/viper" "google.golang.org/api/youtube/v3" @@ -56,13 +53,9 @@ type App struct { events events.EventEmmiter next bool db *pgx.Conn - sse *sse.Server router *httprouter.Router active []string plidx int - playlist *Playlist - plm *sync.RWMutex - hubbub *gohubbub.Client cache *filecache.Cache } @@ -70,11 +63,10 @@ func main() { bfs := afero.NewBasePathFs(afero.NewOsFs(), "cache") app.cache = filecache.NewCache(bfs, 1*time.Hour, "") - app.plm = &sync.RWMutex{} ticker := time.NewTicker(300 * time.Millisecond) 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 { select { diff --git a/events.go b/events.go index 5b35414..47e4b74 100644 --- a/events.go +++ b/events.go @@ -1,7 +1,6 @@ package main import ( - "context" "dndmusicbot/ffmpeg" discordspeaker "dndmusicbot/speaker" "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{}) { if !app.queue.IsPlaying() { return @@ -389,6 +365,10 @@ func (app *App) loadPlaylist(payload ...interface{}) { } return uri, nil }) + if err != nil { + log.Println(err) + continue + } ff, err := ffmpeg.NewPCM(string(yt), sampleRate, channels) if err != nil { @@ -409,7 +389,3 @@ func (app *App) loadPlaylist(payload ...interface{}) { } }() } - -func (app *App) preloadSong(payload ...interface{}) { - app.queue.Preload() -} diff --git a/queue.go b/queue.go index 1f91d6b..f238c57 100644 --- a/queue.go +++ b/queue.go @@ -47,7 +47,6 @@ func (s *Song) NewStream() (err error) { type Queue struct { Events events.EventEmmiter playing bool - yt VideoInfo list *list.List current *list.Element } @@ -106,7 +105,6 @@ func (q *Queue) Next() { } q.current = next - return } func (q *Queue) Prev() { @@ -123,9 +121,8 @@ func (q *Queue) Prev() { if err != nil { log.Println(err) } - q.current = prev - return + q.current = prev } func (q *Queue) Preload() { diff --git a/ws.go b/ws.go index 265a720..24c3113 100644 --- a/ws.go +++ b/ws.go @@ -18,11 +18,10 @@ func init() { ws_msg = make(chan interface{}) go func() { + var msg interface{} for { - select { - case msg := <-ws_msg: - ws_clients.Send(msg) - } + msg = <-ws_msg + ws_clients.Send(msg) } }() diff --git a/youtube.go b/youtube.go index 36c4f4b..c7d1590 100644 --- a/youtube.go +++ b/youtube.go @@ -14,16 +14,11 @@ import ( mrand "math/rand" - "github.com/faiface/beep" "github.com/sosodev/duration" "google.golang.org/api/option" "google.golang.org/api/youtube/v3" ) -var ( - yt_url = "https://www.youtube.com/watch?v=%s" -) - func init() { log.Println("youtube.go loading..") @@ -38,14 +33,6 @@ func init() { 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) { seedb := make([]byte, 32) _, err := rand.Read(seedb) @@ -110,7 +97,7 @@ func (app App) Video(vid string) (out VideoInfo, err error) { } 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] @@ -165,6 +152,4 @@ func (app App) Playlist(playlist string) ([]string, error) { } } return list, nil - - //return fmt.Sprintf(yt_url, list[rand.Intn(len(list))]) }