Progress on downloading videos

This commit is contained in:
Javier Feliz 2025-02-04 22:55:37 -05:00
parent f223c87353
commit 846b5a2123
3 changed files with 223 additions and 61 deletions

3
.gitignore vendored

@ -3,4 +3,5 @@ play.opus
output.opus output.opus
.env .env
*.wav *.wav
*.opus *.opus
*.webm

@ -2,6 +2,10 @@
Currently in development. Currently in development.
## System requirements
- `ffmpeg` make sure libopus is included
- `yt-dlp`
## Current testing steps ## Current testing steps
Copy .env.example to .env Copy .env.example to .env

277
main.go

@ -5,10 +5,8 @@ import (
"io" "io"
"log" "log"
"os" "os"
"os/signal"
"os/exec" "os/exec"
"runtime" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
@ -16,13 +14,21 @@ import (
"github.com/javif89/dotenv" "github.com/javif89/dotenv"
) )
var commandPrefix string = "!" // var commandPrefix string = "!"
var commandHandlers = map[string]func(s *dg.Session, i *dg.InteractionCreate){
"play": playCommand,
}
func main() { func main() {
env := dotenv.Load(".env") env := dotenv.Load(".env")
botToken := env.Get("DISCORD_BOT_TOKEN") botToken := env.Get("DISCORD_BOT_TOKEN")
log.Println("Starting bot") log.Println("Starting bot")
discord, err := dg.New(fmt.Sprintf("Bot %s", botToken))
var discord *dg.Session
var err error
discord, err = dg.New(fmt.Sprintf("Bot %s", botToken))
if err != nil { if err != nil {
log.Println("Error starting the bot", err) log.Println("Error starting the bot", err)
@ -30,7 +36,7 @@ func main() {
} }
discord.AddHandler(ready) discord.AddHandler(ready)
discord.AddHandler(handleCommand) // This will receive an event when a message is sent // discord.AddHandler(handleCommand) // This will receive an event when a message is sent
discord.Identify.Intents = dg.IntentsGuilds | dg.IntentsGuildMessages | dg.IntentsGuildVoiceStates discord.Identify.Intents = dg.IntentsGuilds | dg.IntentsGuildMessages | dg.IntentsGuildVoiceStates
@ -39,45 +45,50 @@ func main() {
log.Println("Error opening Discord session: ", err) log.Println("Error opening Discord session: ", err)
} }
// Register slash commands
discord.AddHandler(handleSlashCommand)
log.Println("Bot is running. Press CTRL+C to exit.") log.Println("Bot is running. Press CTRL+C to exit.")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc <-sc
log.Println("Bot is exiting...") log.Println("Bot is exiting...")
log.Println("Removing commands")
appId := env.Get("DISCORD_APP_ID")
cmds, _ := discord.ApplicationCommands(appId, "338782945110392832")
for _, cmd := range cmds {
discord.ApplicationCommandDelete(appId, "338782945110392832", cmd.ID)
}
discord.Close() discord.Close()
log.Println("Goodbye!") log.Println("Goodbye!")
} }
func ready(s *dg.Session, event *dg.Ready) { func playCommand(s *dg.Session, i *dg.InteractionCreate) {
log.Println("Bot is ready") log.Println("Handling play command")
} options := i.ApplicationCommandData().Options
url := options[0].StringValue()
func handleCommand(s *dg.Session, msg *dg.MessageCreate) { s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
// Ignore any message sent by the bot itself Type: dg.InteractionResponseChannelMessageWithSource,
if msg.Author.ID == s.State.User.ID { Data: &dg.InteractionResponseData{
return Content: fmt.Sprintf("Playing dat music baybeee"),
} },
})
if !strings.HasPrefix(msg.Content, commandPrefix) { downloadVideo(url)
return
}
// Get the channel // Get the channel
msgChannel, err := s.State.Channel(msg.ChannelID) msgServer, err := s.State.Guild(i.GuildID)
if err != nil { if err != nil {
log.Println("Could not find the channel the message came from") log.Println("Failed to get server for action")
return
}
msgServer, err := s.State.Guild(msgChannel.GuildID)
if err != nil {
log.Println("Failed to get server for message")
} }
var voiceChannelId string var voiceChannelId string
for _, vs := range msgServer.VoiceStates { for _, vs := range msgServer.VoiceStates {
if vs.UserID == msg.Author.ID { if vs.UserID == i.Member.User.ID {
voiceChannelId = vs.ChannelID voiceChannelId = vs.ChannelID
} }
} }
@ -87,26 +98,35 @@ func handleCommand(s *dg.Session, msg *dg.MessageCreate) {
log.Println("Failed to join voice channel") log.Println("Failed to join voice channel")
} }
log.Println("Joined channel. Playing sound") log.Println("Joined channel")
voiceChannel.Speaking(true)
time.Sleep(time.Second * 1)
playOnVoiceChannel(voiceChannel)
voiceChannel.Speaking(false)
log.Println("Disconnecting from voice channel")
time.Sleep(time.Second * 2)
voiceChannel.Disconnect()
log.Println("Disconnected")
}
func playOnVoiceChannel(voiceChannel *dg.VoiceConnection) {
log.Println("Starting ffmpeg stream") log.Println("Starting ffmpeg stream")
// I got this implementation from: // I got the original implementation from:
// https://github.com/nhooyr/botatouille/blob/7e1cd9d5a8d517fd43fd11599b2a62bf832a5c96/cmd/botatouille/music/music.go#L62-L104 // https://github.com/nhooyr/botatouille/blob/7e1cd9d5a8d517fd43fd11599b2a62bf832a5c96/cmd/botatouille/music/music.go#L62-L104
// after hours of searching. // after hours of searching.
ffmpeg := exec.Command( ffmpeg := exec.Command(
"ffmpeg", "ffmpeg",
"-i", "testfile.wav", "-i", "vid.webm",
"-hide_banner", "-hide_banner",
"-loglevel", "quiet", "-loglevel", "quiet",
"-i", "testfile.wav", "-i", "testfile.wav",
"-f", "data", "-f", "data",
"-map", "0:a", "-map", "0:a",
"-ar", "48k", "-ar", "48k",
"-ac", "2", "-ac", "2",
"-acodec", "libopus", "-acodec", "libopus",
"-b:a", "128k", "-b:a", "128k",
"pipe:1") "pipe:1")
ffmpegOut, err := ffmpeg.StdoutPipe() ffmpegOut, err := ffmpeg.StdoutPipe()
@ -114,14 +134,6 @@ func handleCommand(s *dg.Session, msg *dg.MessageCreate) {
log.Fatal(err) log.Fatal(err)
} }
framesChan := make(chan []byte, 100000)
go func() {
for {
voiceChannel.OpusSend <- <-framesChan
}
}()
runtime.LockOSThread()
err = ffmpeg.Start() err = ffmpeg.Start()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -142,25 +154,170 @@ func handleCommand(s *dg.Session, msg *dg.MessageCreate) {
log.Fatal(err) log.Fatal(err)
} }
log.Printf("Sending opus frame: %d bytes", n) // log.Printf("Read packet: %d bytes", n)
packets = append(packets, p[:n]) packets = append(packets, p[:n])
} }
log.Println("Iterating through packets") voiceChannel.Speaking(true)
time.Sleep(time.Second * 2)
for _, p := range packets { log.Println("Playing sound")
log.Printf("%d bytes", len(p))
}
for _, p := range packets { for _, p := range packets {
log.Printf("Sending packet: %d bytes", len(p)) log.Printf("Sending packet: %d bytes", len(p))
voiceChannel.OpusSend <- p voiceChannel.OpusSend <- p
}
log.Println("Ended stream")
}
func handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
log.Println("Command received?")
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func ready(s *dg.Session, event *dg.Ready) {
commands := []*dg.ApplicationCommand{
{
Name: "play",
Description: "Play a song from youtube, spotify, apple music, etc",
Options: []*dg.ApplicationCommandOption{
{
Type: dg.ApplicationCommandOptionString,
Name: "url",
Description: "URL to the song",
Required: true,
},
},
},
} }
voiceChannel.Speaking(false) env := dotenv.Load(".env")
log.Println("Disconnecting from voice channel") appId := env.Get("DISCORD_APP_ID")
time.Sleep(time.Second * 2)
voiceChannel.Disconnect() log.Println("Bot is ready")
log.Println("Disconnected") log.Println("Bulk registering commands")
} _, err := s.ApplicationCommandBulkOverwrite(appId, "338782945110392832", commands)
if err != nil {
log.Fatal(err)
}
log.Println("Done")
}
func downloadVideo(url string) error {
log.Printf("Downloading: %s", url)
os.Remove("vid.webm")
cmd := exec.Command(
"yt-dlp",
"-i",
"-q",
"-o",
"vid",
url)
err := cmd.Run()
log.Println("Downloaded")
return err
}
// func handleCommand(s *dg.Session, msg *dg.MessageCreate) {
// // Ignore any message sent by the bot itself
// if msg.Author.ID == s.State.User.ID {
// return
// }
// if !strings.HasPrefix(msg.Content, commandPrefix) {
// return
// }
// // Get the channel
// msgChannel, err := s.State.Channel(msg.ChannelID)
// if err != nil {
// log.Println("Could not find the channel the message came from")
// return
// }
// msgServer, err := s.State.Guild(msgChannel.GuildID)
// if err != nil {
// log.Println("Failed to get server for message")
// }
// var voiceChannelId string
// for _, vs := range msgServer.VoiceStates {
// if vs.UserID == msg.Author.ID {
// voiceChannelId = vs.ChannelID
// }
// }
// voiceChannel, err := s.ChannelVoiceJoin(msgServer.ID, voiceChannelId, false, true)
// if err != nil {
// log.Println("Failed to join voice channel")
// }
// log.Println("Joined channel")
// log.Println("Starting ffmpeg stream")
// // I got the original implementation from:
// // https://github.com/nhooyr/botatouille/blob/7e1cd9d5a8d517fd43fd11599b2a62bf832a5c96/cmd/botatouille/music/music.go#L62-L104
// // after hours of searching.
// ffmpeg := exec.Command(
// "ffmpeg",
// "-i", "testfile.wav",
// "-hide_banner",
// "-loglevel", "quiet",
// "-i", "testfile.wav",
// "-f", "data",
// "-map", "0:a",
// "-ar", "48k",
// "-ac", "2",
// "-acodec", "libopus",
// "-b:a", "128k",
// "pipe:1")
// ffmpegOut, err := ffmpeg.StdoutPipe()
// if err != nil {
// log.Fatal(err)
// }
// // runtime.LockOSThread()
// err = ffmpeg.Start()
// if err != nil {
// log.Fatal(err)
// }
// packets := [][]byte{}
// for {
// // I read in the RFC that frames will not be bigger than this size
// p := make([]byte, 960)
// n, err := ffmpegOut.Read(p)
// if err != nil {
// log.Printf("Bytes: %d", n)
// if err == io.EOF {
// log.Println("Done streaming")
// break
// }
// log.Fatal(err)
// }
// // log.Printf("Read packet: %d bytes", n)
// packets = append(packets, p[:n])
// }
// voiceChannel.Speaking(true)
// time.Sleep(time.Second * 2)
// log.Println("Playing sound")
// for _, p := range packets {
// log.Printf("Sending packet: %d bytes", len(p))
// voiceChannel.OpusSend <- p
// }
// voiceChannel.Speaking(false)
// log.Println("Disconnecting from voice channel")
// time.Sleep(time.Second * 2)
// voiceChannel.Disconnect()
// log.Println("Disconnected")
// }