package main import ( "fmt" "log" "os" "os/signal" "syscall" "time" "git.thegrind.dev/thegrind/papibot/pkg/bot" "git.thegrind.dev/thegrind/papibot/pkg/opusframes" "git.thegrind.dev/thegrind/papibot/pkg/youtube" dg "github.com/bwmarrin/discordgo" "github.com/javif89/dotenv" ) func main() { env := dotenv.Load(".env") botToken := env.Get("DISCORD_BOT_TOKEN") appId := env.Get("DISCORD_APP_ID") bot := bot.New(botToken, appId) log.Println("Starting bot") err := bot.Start() log.Println("Error starting the bot", err) log.Println("Adding command handlers") bot.AddSlashCommand("play", playCommand) log.Println("Done") log.Println("Bot is running. Press CTRL+C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc log.Println("Bot is exiting...") bot.Stop() log.Println("Goodbye!") } func playCommand(s *dg.Session, i *dg.InteractionCreate) { log.Println("Handling play command") options := i.ApplicationCommandData().Options url := options[0].StringValue() s.InteractionRespond(i.Interaction, &dg.InteractionResponse{ Type: dg.InteractionResponseChannelMessageWithSource, Data: &dg.InteractionResponseData{ Content: fmt.Sprintf("Playing dat music baybeee"), }, }) os.Remove("vid.webm") youtube.Download(url, "vid.webm") start := time.Now() opusframes.Encode("vid.webm", "vid.of") duration := time.Since(start) log.Printf("Encoding took: %s", duration) // Get the channel msgServer, err := s.State.Guild(i.GuildID) if err != nil { log.Println("Failed to get server for action") } var voiceChannelId string for _, vs := range msgServer.VoiceStates { if vs.UserID == i.Member.User.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") 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("Decoding") start := time.Now() frames, err := opusframes.Decode("vid.of") if err != nil { log.Println("Decoding error: ", err) return } duration := time.Since(start) log.Printf("Decoding took: %s", duration) // for _, f := range frames { // log.Printf("Got frame. Size: %d", len(f)) // } voiceChannel.Speaking(true) time.Sleep(time.Second * 2) log.Println("Playing sound") for _, p := range frames { log.Printf("Sending packet: %d bytes", len(p)) voiceChannel.OpusSend <- p } }