1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 08:32:48 +00:00

feat: add yt-dlp

This commit is contained in:
2022-04-24 20:25:50 -05:00
parent 0532acfddd
commit 86d96c1c1d
5 changed files with 38 additions and 12 deletions

View File

@@ -19,6 +19,8 @@ FROM jrottenberg/ffmpeg:4.1-alpine
RUN apk update RUN apk update
RUN apk add ca-certificates opus-dev opusfile-dev RUN apk add ca-certificates opus-dev opusfile-dev
# add python for yt-dlp
RUN apk add python3
WORKDIR /server WORKDIR /server
COPY --from=0 /home/dist /server/dist COPY --from=0 /home/dist /server/dist
COPY --from=1 /build/bot /server/bot COPY --from=1 /build/bot /server/bot

View File

@@ -14,4 +14,4 @@ services:
- ./data.db:/server/data.db - ./data.db:/server/data.db
- ./logrus.log:/server/logrus.log - ./logrus.log:/server/logrus.log
- /usr/local/bin/youtube-dl:/usr/bin/youtube-dl - /usr/local/bin/yt-dlp:/usr/bin/yt-dlp

View File

@@ -7,7 +7,7 @@ A soundboard bot for discord. Built with Go/React.
## How to use ## How to use
- [Download latest release here](https://github.com/mgerb/go-discord-bot/releases) - [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) - Install [ffmpeg](https://www.ffmpeg.org/download.html)
- edit your config.json file - edit your config.json file
- `docker-compose up` - `docker-compose up`

View File

@@ -145,7 +145,10 @@ func (conn *AudioConnection) handleMessage(m *discordgo.MessageCreate) {
func (conn *AudioConnection) dismiss() { func (conn *AudioConnection) dismiss() {
voiceConnection := conn.getVoiceConnection() voiceConnection := conn.getVoiceConnection()
if voiceConnection != nil && !conn.SoundPlayingLock && len(conn.SoundQueue) == 0 { 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() { func (conn *AudioConnection) playSoundsInQueue() {
conn.toggleSoundPlayingLock(true) conn.toggleSoundPlayingLock(true)
voiceConnection := conn.getVoiceConnection() voiceConnection := conn.getVoiceConnection()
if voiceConnection == nil {
return
}
// Start speaking. // Start speaking.
voiceConnection.Speaking(true) err := voiceConnection.Speaking(true)
if err != nil {
log.Error(err)
}
for { for {
select { select {
@@ -295,7 +307,10 @@ func (conn *AudioConnection) playSoundsInQueue() {
default: default:
// Stop speaking // Stop speaking
voiceConnection.Speaking(false) err := voiceConnection.Speaking(false)
if err != nil {
log.Error(err)
}
conn.toggleSoundPlayingLock(false) conn.toggleSoundPlayingLock(false)
return return
} }
@@ -334,10 +349,16 @@ func (conn *AudioConnection) loadFile(fileName string) error {
func (conn *AudioConnection) clipAudio(m *discordgo.MessageCreate) { func (conn *AudioConnection) clipAudio(m *discordgo.MessageCreate) {
if len(conn.VoiceClipQueue) < 10 { 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 { } else {
writePacketsToFile(m.Author.Username, conn.VoiceClipQueue) 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 // create clips folder if it does not exist
if _, err := os.Stat(config.Config.ClipsPath); os.IsNotExist(err) { 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{} opusData := map[uint32][][]byte{}
@@ -411,7 +435,7 @@ func (conn *AudioConnection) startAudioListener() {
voiceConnection := conn.getVoiceConnection() voiceConnection := conn.getVoiceConnection()
if voiceConnection != nil { if voiceConnection != nil {
voiceConnection.RLock() voiceConnection.RLock()
ready := voiceConnection != nil && voiceConnection.Ready ready := voiceConnection.Ready
voiceConnection.RUnlock() voiceConnection.RUnlock()
if !ready { if !ready {
@@ -429,7 +453,7 @@ func (conn *AudioConnection) startAudioListener() {
continue continue
} }
voiceConnection.RLock() voiceConnection.RLock()
ready := voiceConnection != nil && voiceConnection.Ready ready := voiceConnection.Ready
voiceConnection.RUnlock() voiceConnection.RUnlock()
// if connection lost wait for ready // if connection lost wait for ready

View File

@@ -26,7 +26,7 @@ func getDownloaderHandler(c *gin.Context) {
} }
// get the video title // get the video title
titleCmd := exec.Command("youtube-dl", "--get-title", url) titleCmd := exec.Command("yt-dlp", "--get-title", url)
var titleOut bytes.Buffer var titleOut bytes.Buffer
titleCmd.Stdout = &titleOut titleCmd.Stdout = &titleOut
@@ -46,7 +46,7 @@ func getDownloaderHandler(c *gin.Context) {
cleanTitle := cleanseTitle(titleOut.String()) cleanTitle := cleanseTitle(titleOut.String())
log.Debug(cleanTitle) 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 var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out