fixup! Initial commit

Stein Ivar Berghei 2022-11-18 22:05:41 +01:00
parent d25655ef4a
commit ef2d568a6e
6 changed files with 120 additions and 25 deletions

View File

@ -3,7 +3,9 @@ package main
import (
"context"
"dndmusicbot/ffmpeg"
"dndmusicbot/loop"
discordspeaker "dndmusicbot/speaker"
"dndmusicbot/ytdl"
"encoding/json"
"fmt"
"log"
@ -40,8 +42,10 @@ func init() {
app.events.On("preload_song", app.preloadSong)
app.events.On("song_over", app.songOver)
//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)
@ -127,7 +131,9 @@ func (app *App) ambiancePlay(payload ...interface{}) {
if app.ambiance.IsPlaying() {
app.ambiance.Reset()
}
app.ambiance.Add(play)
loop := loop.Loop(-1, play)
app.ambiance.Add(loop)
discordspeaker.Unlock()
msg := make(map[string]interface{})
@ -153,6 +159,48 @@ func (app *App) ambianceStop(payload ...interface{}) {
}
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
}
err := ytdl.DownloadAmbiance(amburl, ambtitle)
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{}) {
if !app.queue.IsPlaying() {
return
@ -256,16 +304,6 @@ func (app *App) nextSong(payload ...interface{}) {
return
}
for {
time.Sleep(1 * time.Second)
if app.queue.QLen() == 0 {
log.Println("retrying...")
song = app.GetSong(app.active)
} else {
break
}
}
discordspeaker.Lock()
app.queue.Reset()
app.queue.Add(f)

View File

@ -24,6 +24,7 @@ type FFmpeg struct {
PMutex *sync.RWMutex
ProcessState *os.ProcessState
err chan error
fb chan bool
}
func NewFFmpeg(uri string, sampleRate int, channels int) (ff *FFmpeg, err error) {
@ -42,6 +43,7 @@ func NewFFmpeg(uri string, sampleRate int, channels int) (ff *FFmpeg, err error)
time.Sleep(500 * time.Millisecond)
}
RETRY:
ff = new(FFmpeg)
ff.PMutex = &sync.RWMutex{}
ff.Len = yt.Len
@ -53,6 +55,7 @@ func NewFFmpeg(uri string, sampleRate int, channels int) (ff *FFmpeg, err error)
ff.Cmd = exec.CommandContext(
ctx,
"ffmpeg",
"-xerror",
"-i", yt.Url,
"-f", "s16le",
"-v", "error",
@ -71,18 +74,23 @@ func NewFFmpeg(uri string, sampleRate int, channels int) (ff *FFmpeg, err error)
ff.Out = bytes.NewBuffer(make([]byte, 43920))
ff.Cmd.Stdout = ff.Out
ff.Start()
exitctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
return
}
func (ff *FFmpeg) Start() {
go func() {
ff.Cmd.Start()
ff.err <- ff.Cmd.Wait()
}()
select {
case <-exitctx.Done():
ff.Started = true
case <-ff.err:
log.Println("ffmpeg exited early, retry...")
goto RETRY
}
return
}
func (ff *FFmpeg) Close() error {

2
go.mod
View File

@ -2,8 +2,6 @@ module dndmusicbot
go 1.19
replace github.com/3d0c/gmf => /home/steino/dev/go/gmf
require (
github.com/bwmarrin/discordgo v0.26.1
github.com/dpup/gohubbub v0.0.0-20140517235056-2dc6969d22d8

View File

@ -122,12 +122,32 @@ window.onload = function() {
}
}
document.querySelector("#addambiance").addEventListener("click", (e) => {
e.preventDefault()
output.innerText = ""
var title = document.querySelector("#inputambiance > input[name='title']")
var url = document.querySelector("#inputambiance > input[name='url']")
if (title.value == "" || url.value == "") {
output.innerText = "Title or Url is empty!"
return
}
ws.send(JSON.stringify({
"event": "ambiance_add",
"payload": {
"title": title.value,
"url": url.value
}
}))
})
submit.addEventListener("click", (e) => {
e.preventDefault()
output.innerText = ""
var title = document.querySelector(".container2 input[name='title'")
var url = document.querySelector(".container2 input[name='url'")
var title = document.querySelector("#inputplaylist > input[name='title']")
var url = document.querySelector("#inputplaylist > input[name='url']")
if (title.value == "" || url.value == "") {
output.innerText = "Title or Url is empty!"
return
@ -140,6 +160,9 @@ window.onload = function() {
"url": url.value
}
}))
title.innerText = ""
url.innerText = ""
})
document.querySelectorAll("#items").forEach(item => item.addEventListener("click", (e) => {

View File

@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>D&D Music Bot!</title>
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/solarized-dark.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div><h2 class="bot">Playlists</h2></div>
@ -16,17 +16,17 @@
{{ end}}
<div class="item locked" data-id="reset">Stop</div>
</div>
<div class="container2">
<div id="inputplaylist" class="container2">
<input name="title" type="text" placeholder="Enter name..">
<input name="url" type="text" placeholder="https://youtube.com/playlist?list=...">
<input id="addplaylist" name="submit" value="Add" type="submit">
<input id="prev" name="prev" type="button" value="prev">
<input id="next" name="next" type="button" value="next">
</div>
<div class="container2">
<p id="output"></p>
</div>
<div id="info" class="container2">
<input id="prev" name="prev" type="button" value="prev">
<input id="next" name="next" type="button" value="next">
<a id="link" style="text-decoration: none;">
<span id="channel"></span>
<span> - </span>
@ -41,6 +41,11 @@
{{ end}}
<div class="item locked" data-id="reset">Stop</div>
</div>
<div id="inputambiance" class="container2">
<input name="title" type="text" placeholder="Enter name..">
<input name="url" type="text" placeholder="Enter url...">
<input id="addambiance" name="submit" value="Add" type="submit">
</div>
<!-- The Modal -->
<div id="waiting" class="modal">
<!-- Modal content -->

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"time"
"github.com/tidwall/gjson"
@ -45,3 +46,25 @@ func NewYTdl(uri string) (*YTdl, error) {
return &YTdl{title, geturl, channel, duration}, nil
}
func DownloadAmbiance(uri string, name string) error {
err := exec.Command(
"./bin/yt-dlp_linux",
uri,
"-x",
"--audio-format", "mp3",
"--postprocessor-args", "-ar 48000 -ac 2",
"--cookies", "./cookies.txt",
"--no-call-home",
"--no-cache-dir",
"--restrict-filenames",
"-f", "140",
"-o", filepath.Join("./ambiance/", name+".mp3"),
).Run()
if err != nil {
return nil
}
return nil
}