From 86d96c1c1d689cd217deb19a55771bf7ec0dfa2f Mon Sep 17 00:00:00 2001 From: Mitchell Date: Sun, 24 Apr 2022 20:25:50 -0500 Subject: [PATCH] feat: add yt-dlp --- Dockerfile | 2 ++ docker-compose.yml | 2 +- readme.md | 2 +- server/bothandlers/sounds.go | 40 +++++++++++++++++++++------ server/webserver/routes/downloader.go | 4 +-- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 45c79e5..f9d006b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,6 +19,8 @@ FROM jrottenberg/ffmpeg:4.1-alpine RUN apk update RUN apk add ca-certificates opus-dev opusfile-dev +# add python for yt-dlp +RUN apk add python3 WORKDIR /server COPY --from=0 /home/dist /server/dist COPY --from=1 /build/bot /server/bot diff --git a/docker-compose.yml b/docker-compose.yml index 8feb035..b7959ae 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,4 +14,4 @@ services: - ./data.db:/server/data.db - ./logrus.log:/server/logrus.log - - /usr/local/bin/youtube-dl:/usr/bin/youtube-dl + - /usr/local/bin/yt-dlp:/usr/bin/yt-dlp diff --git a/readme.md b/readme.md index 292f988..fed6670 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,7 @@ A soundboard bot for discord. Built with Go/React. ## How to use - [Download latest release here](https://github.com/mgerb/go-discord-bot/releases) -- Install [youtube-dl](https://github.com/rg3/youtube-dl/blob/master/README.md#installation) +- Install [yt-dlp](https://github.com/yt-dlp/yt-dlp) - Install [ffmpeg](https://www.ffmpeg.org/download.html) - edit your config.json file - `docker-compose up` diff --git a/server/bothandlers/sounds.go b/server/bothandlers/sounds.go index 743ee29..a126a5a 100644 --- a/server/bothandlers/sounds.go +++ b/server/bothandlers/sounds.go @@ -145,7 +145,10 @@ func (conn *AudioConnection) handleMessage(m *discordgo.MessageCreate) { func (conn *AudioConnection) dismiss() { voiceConnection := conn.getVoiceConnection() if voiceConnection != nil && !conn.SoundPlayingLock && len(conn.SoundQueue) == 0 { - voiceConnection.Disconnect() + err := voiceConnection.Disconnect() + if err != nil { + log.Error(err) + } } } @@ -274,8 +277,17 @@ func (conn *AudioConnection) PlayAudio(soundName string, m *discordgo.MessageCre func (conn *AudioConnection) playSoundsInQueue() { conn.toggleSoundPlayingLock(true) voiceConnection := conn.getVoiceConnection() + + if voiceConnection == nil { + return + } + // Start speaking. - voiceConnection.Speaking(true) + err := voiceConnection.Speaking(true) + + if err != nil { + log.Error(err) + } for { select { @@ -295,7 +307,10 @@ func (conn *AudioConnection) playSoundsInQueue() { default: // Stop speaking - voiceConnection.Speaking(false) + err := voiceConnection.Speaking(false) + if err != nil { + log.Error(err) + } conn.toggleSoundPlayingLock(false) return } @@ -334,10 +349,16 @@ func (conn *AudioConnection) loadFile(fileName string) error { func (conn *AudioConnection) clipAudio(m *discordgo.MessageCreate) { if len(conn.VoiceClipQueue) < 10 { - conn.Session.ChannelMessageSend(m.ChannelID, "Clip failed.") + _, err := conn.Session.ChannelMessageSend(m.ChannelID, "Clip failed.") + if err != nil { + log.Error(err) + } } else { writePacketsToFile(m.Author.Username, conn.VoiceClipQueue) - conn.Session.ChannelMessageSend(m.ChannelID, "Sound clipped!") + _, err := conn.Session.ChannelMessageSend(m.ChannelID, "Sound clipped!") + if err != nil { + log.Error(err) + } } } @@ -345,7 +366,10 @@ func writePacketsToFile(username string, packets chan *discordgo.Packet) { // create clips folder if it does not exist if _, err := os.Stat(config.Config.ClipsPath); os.IsNotExist(err) { - os.Mkdir(config.Config.ClipsPath, os.ModePerm) + err := os.Mkdir(config.Config.ClipsPath, os.ModePerm) + if err != nil { + log.Error(err) + } } opusData := map[uint32][][]byte{} @@ -411,7 +435,7 @@ func (conn *AudioConnection) startAudioListener() { voiceConnection := conn.getVoiceConnection() if voiceConnection != nil { voiceConnection.RLock() - ready := voiceConnection != nil && voiceConnection.Ready + ready := voiceConnection.Ready voiceConnection.RUnlock() if !ready { @@ -429,7 +453,7 @@ func (conn *AudioConnection) startAudioListener() { continue } voiceConnection.RLock() - ready := voiceConnection != nil && voiceConnection.Ready + ready := voiceConnection.Ready voiceConnection.RUnlock() // if connection lost wait for ready diff --git a/server/webserver/routes/downloader.go b/server/webserver/routes/downloader.go index 1a72ec7..b637283 100644 --- a/server/webserver/routes/downloader.go +++ b/server/webserver/routes/downloader.go @@ -26,7 +26,7 @@ func getDownloaderHandler(c *gin.Context) { } // get the video title - titleCmd := exec.Command("youtube-dl", "--get-title", url) + titleCmd := exec.Command("yt-dlp", "--get-title", url) var titleOut bytes.Buffer titleCmd.Stdout = &titleOut @@ -46,7 +46,7 @@ func getDownloaderHandler(c *gin.Context) { cleanTitle := cleanseTitle(titleOut.String()) log.Debug(cleanTitle) - cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", config.Config.YoutubePath+"/"+cleanTitle+".%(ext)s", url) + cmd := exec.Command("yt-dlp", "-x", "--audio-format", "mp3", "-o", config.Config.YoutubePath+"/"+cleanTitle+".%(ext)s", url) var out bytes.Buffer cmd.Stdout = &out