2022-11-18 21:18:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
discordspeaker "dndmusicbot/speaker"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-11-23 07:37:59 +00:00
|
|
|
"strconv"
|
2022-11-18 21:18:12 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
"github.com/faiface/beep"
|
2022-11-23 22:29:45 +00:00
|
|
|
"github.com/faiface/beep/effects"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/faiface/beep/mp3"
|
2022-11-23 07:37:59 +00:00
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SongInfo struct {
|
2022-11-19 16:37:49 +00:00
|
|
|
Playlist uuid.UUID `json:"playlist,omitempty"`
|
|
|
|
PlaylistName string `json:"playlistname,omitempty"`
|
|
|
|
Title string `json:"current,omitempty"`
|
|
|
|
Channel string `json:"channel,omitempty"`
|
2022-11-23 07:37:59 +00:00
|
|
|
Position int64 `json:"position"`
|
|
|
|
Length int64 `json:"len,omitempty"`
|
2022-11-18 21:18:12 +00:00
|
|
|
Pause bool `json:"pause"`
|
2022-11-19 16:37:49 +00:00
|
|
|
Song string `json:"song,omitempty"`
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
var l = rate.Sometimes{Interval: 800 * time.Millisecond}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.Println("events.go loading...")
|
2022-11-23 07:37:59 +00:00
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
app.events = events.New()
|
|
|
|
|
|
|
|
app.events.On("load_playlist", app.loadPlaylist)
|
|
|
|
app.events.On("add_playlist", app.addPlaylist)
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
//app.events.On("preload_song", app.preloadSong)
|
2022-11-23 07:37:59 +00:00
|
|
|
//app.events.On("song_over", app.songInfo)
|
|
|
|
//app.events.On("song_start", app.songInfo)
|
|
|
|
app.events.On("player", app.songInfo)
|
2022-11-18 21:18:12 +00:00
|
|
|
//app.events.On("song_position", app.songPosition)
|
|
|
|
|
|
|
|
app.events.On("ambiance_play", app.ambiancePlay)
|
|
|
|
app.events.On("ambiance_stop", app.ambianceStop)
|
|
|
|
app.events.On("ambiance_add", app.ambianceAdd)
|
|
|
|
|
|
|
|
app.events.On("stop", app.stop)
|
|
|
|
app.events.On("next", app.nextSong)
|
|
|
|
app.events.On("prev", app.prevSong)
|
|
|
|
|
|
|
|
//app.events.On("tick", app.checkQueue)
|
|
|
|
app.events.On("tick", app.songPosition)
|
2022-11-23 07:37:59 +00:00
|
|
|
//app.events.On("tick", app.checkTimeleft)
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) songInfoEvent(event string) map[string]interface{} {
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
msg["event"] = event
|
2022-11-23 07:37:59 +00:00
|
|
|
status, err := app.mpd.Status()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
cur, err := app.mpd.CurrentSong()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
info := new(SongInfo)
|
|
|
|
|
|
|
|
if status["state"] != "play" {
|
|
|
|
info.Pause = true
|
|
|
|
msg["payload"] = info
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
duration, ok := status["duration"]
|
|
|
|
if ok && duration != "" {
|
|
|
|
slen, err := strconv.ParseFloat(duration, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info.Length = time.Duration(slen * float64(time.Second)).Milliseconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed, ok := status["elapsed"]
|
|
|
|
if ok && elapsed != "" {
|
|
|
|
spos, err := strconv.ParseFloat(elapsed, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info.Position = time.Duration(spos * float64(time.Second)).Milliseconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
album, ok := cur["Album"]
|
|
|
|
if ok {
|
|
|
|
plid, err := uuid.ParseBytes([]byte(album))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pl, err := app.GetPlaylist(plid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Playlist = pl.Id
|
|
|
|
info.PlaylistName = pl.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
title, ok := cur["Title"]
|
|
|
|
if ok {
|
|
|
|
info.Title = title
|
|
|
|
}
|
|
|
|
|
|
|
|
artist, ok := cur["Artist"]
|
|
|
|
if ok {
|
|
|
|
info.Channel = artist
|
|
|
|
}
|
|
|
|
|
|
|
|
location, ok := cur["Location"]
|
|
|
|
if ok {
|
|
|
|
info.Song = location
|
|
|
|
}
|
|
|
|
|
|
|
|
msg["payload"] = *info
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ambiancePlay(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("ambiance_play called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var fn string
|
|
|
|
switch data := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
var err error
|
|
|
|
err = json.Unmarshal(data, &fn)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Println("loadPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(fmt.Sprintf("./ambiance/%s.mp3", fn))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
play, _, err := mp3.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-11-23 22:29:45 +00:00
|
|
|
loop := beep.Loop(-1, play)
|
|
|
|
|
|
|
|
volume := &effects.Volume{
|
|
|
|
Streamer: loop,
|
|
|
|
Base: 2,
|
|
|
|
Volume: -2,
|
|
|
|
Silent: false,
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
discordspeaker.Pause(false)
|
|
|
|
discordspeaker.Lock()
|
2022-11-19 16:37:49 +00:00
|
|
|
app.ambiance.Clear()
|
2022-11-23 22:29:45 +00:00
|
|
|
app.ambiance.Add(volume)
|
2022-11-18 21:18:12 +00:00
|
|
|
discordspeaker.Unlock()
|
|
|
|
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
out := make(map[string]interface{})
|
|
|
|
|
|
|
|
app.curamb = fn
|
|
|
|
|
|
|
|
msg["event"] = "ambiance_play"
|
|
|
|
out["type"] = fn
|
|
|
|
msg["payload"] = out
|
|
|
|
ws_msg <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ambianceStop(payload ...interface{}) {
|
|
|
|
log.Println("Stopping ambiance")
|
|
|
|
discordspeaker.Lock()
|
2022-11-19 16:37:49 +00:00
|
|
|
app.ambiance.Clear()
|
2022-11-18 21:18:12 +00:00
|
|
|
discordspeaker.Unlock()
|
|
|
|
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
msg["event"] = "ambiance_stop"
|
|
|
|
ws_msg <- msg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ambianceAdd(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("addPlaylist called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("ambiance_add event received")
|
|
|
|
|
|
|
|
var data map[string]string
|
|
|
|
switch js := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
json.Unmarshal(js, &data)
|
|
|
|
default:
|
|
|
|
log.Println("newPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
amburl, ok := data["url"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ambtitle, ok := data["title"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without title")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:05:02 +00:00
|
|
|
err := DownloadAmbiance(amburl, ambtitle)
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
out := make(map[string]interface{})
|
|
|
|
msg["event"] = "ambiance_add"
|
|
|
|
out["type"] = ambtitle
|
|
|
|
msg["payload"] = out
|
|
|
|
ws_msg <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) songPosition(payload ...interface{}) {
|
2022-11-23 07:37:59 +00:00
|
|
|
status, err := app.mpd.Status()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status["state"] == "stop" {
|
2022-11-18 21:18:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Do(func() {
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
out := make(map[string]interface{})
|
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
slen, err := strconv.ParseFloat(status["duration"], 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
spos, err := strconv.ParseFloat(status["elapsed"], 64)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
msg["event"] = "song_position"
|
2022-11-23 07:37:59 +00:00
|
|
|
out["len"] = time.Duration(slen * float64(time.Second)).Milliseconds()
|
|
|
|
out["position"] = time.Duration(spos * float64(time.Second)).Milliseconds()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
msg["payload"] = out
|
|
|
|
ws_msg <- msg
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
func (app *App) songInfo(payload ...interface{}) {
|
2022-11-23 07:37:59 +00:00
|
|
|
log.Println("song_info event received")
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
msg := app.songInfoEvent("song_info")
|
2022-11-23 07:37:59 +00:00
|
|
|
if msg != nil {
|
|
|
|
ws_msg <- msg
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) stop(payload ...interface{}) {
|
|
|
|
log.Println("stop event received")
|
2022-11-23 07:37:59 +00:00
|
|
|
|
|
|
|
app.mpd.Stop()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
msg["event"] = "stop"
|
|
|
|
|
|
|
|
ws_msg <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) prevSong(payload ...interface{}) {
|
|
|
|
log.Println("prev_song event received")
|
2022-11-23 07:37:59 +00:00
|
|
|
err := app.mpd.Previous()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) nextSong(payload ...interface{}) {
|
|
|
|
log.Println("next_song event received")
|
2022-11-23 07:37:59 +00:00
|
|
|
err := app.mpd.Next()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) addPlaylist(payload ...interface{}) {
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("addPlaylist called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("add_playlist event received")
|
|
|
|
|
|
|
|
var data map[string]string
|
|
|
|
switch js := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
json.Unmarshal(js, &data)
|
|
|
|
default:
|
|
|
|
log.Println("newPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
plurl, ok := data["url"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pltitle, ok := data["title"]
|
|
|
|
if !ok {
|
|
|
|
log.Println("addPlaylist without title")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pl, err := url.Parse(plurl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("addPlaylist invalid url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
plid := pl.Query().Get("list")
|
|
|
|
if plid == "" {
|
|
|
|
log.Println("addPlaylist missing list in url")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = app.Playlist(plid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting youtube playlist info,", plid)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := app.AddPlaylist(pltitle, plid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting youtube playlist info,", plid)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := make(map[string]interface{})
|
|
|
|
|
|
|
|
msg["event"] = "new_playlist"
|
|
|
|
msg["payload"] = map[string]string{"url": id.String(), "title": pltitle}
|
|
|
|
|
|
|
|
ws_msg <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) loadPlaylist(payload ...interface{}) {
|
|
|
|
log.Println("load_playlist event received")
|
|
|
|
|
|
|
|
if !(len(payload) > 0) {
|
|
|
|
log.Println("loadPlaylist called without a payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var id uuid.UUID
|
|
|
|
switch data := payload[0].(type) {
|
|
|
|
case json.RawMessage:
|
|
|
|
var err error
|
|
|
|
var tmp string
|
|
|
|
|
|
|
|
json.Unmarshal(data, &tmp)
|
|
|
|
id, err = uuid.Parse(tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to parse UUID,", err)
|
|
|
|
}
|
|
|
|
case uuid.UUID:
|
|
|
|
id = data
|
|
|
|
default:
|
|
|
|
log.Println("loadPlaylist called with invalid payload.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Loading new playlist: ", id)
|
|
|
|
pl, err := app.GetPlaylist(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to find playlist with id,", id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
discordspeaker.Pause(false)
|
|
|
|
list, err := app.Playlist(pl.Url)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting playlist info,", id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err = ShufflePlaylist(list)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to shuffle playlist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
app.mpd.Stop()
|
|
|
|
app.mpd.Clear()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
go func() {
|
|
|
|
for _, vid := range list {
|
|
|
|
ytinfo, err := app.Video(vid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-19 16:37:49 +00:00
|
|
|
_, yt, err := app.cache.GetOrCreateBytes(vid+".txt", func() ([]byte, error) {
|
2022-11-19 17:05:02 +00:00
|
|
|
uri, err := NewYTdl(vid)
|
2022-11-19 16:37:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return uri, nil
|
|
|
|
})
|
2022-11-20 16:30:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
2022-11-19 16:37:49 +00:00
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
// state:stop
|
|
|
|
songid, err := app.mpd.AddID(string(yt), 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mpdcmd := app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("artist"), ytinfo.Channel)
|
|
|
|
err = mpdcmd.OK()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mpdcmd = app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("title"), ytinfo.Title)
|
|
|
|
err = mpdcmd.OK()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mpdcmd = app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("location"), vid)
|
|
|
|
err = mpdcmd.OK()
|
2022-11-19 16:37:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-23 07:37:59 +00:00
|
|
|
mpdcmd = app.mpd.Command("%s %d %s %s", mpd.Quoted("addtagid"), songid, mpd.Quoted("album"), pl.Id.String())
|
|
|
|
err = mpdcmd.OK()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-11-23 07:37:59 +00:00
|
|
|
|
|
|
|
app.mpd.Play(-1)
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
2022-11-19 16:37:49 +00:00
|
|
|
}()
|
|
|
|
}
|