55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
// playSound plays the current buffer to the provided channel.
|
|
func playSound(s *discordgo.Session, guildID, channelID string, audioBuffer []byte) (err error) {
|
|
|
|
// Join the provided voice channel.
|
|
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, false)
|
|
if err != nil {
|
|
log.Println("Could not find channel.")
|
|
return err
|
|
}
|
|
|
|
// Sleep for a specified amount of time before playing the sound
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
// Start speaking.
|
|
vc.Speaking(true)
|
|
|
|
log.Println(len(audioBuffer))
|
|
vc.OpusSend <- audioBuffer
|
|
|
|
// Stop speaking
|
|
vc.Speaking(false)
|
|
|
|
// Sleep for a specificed amount of time before ending.
|
|
time.Sleep(5000 * time.Millisecond)
|
|
|
|
// Disconnect from the provided voice channel.
|
|
vc.Disconnect()
|
|
|
|
return nil
|
|
}
|
|
|
|
func getAudioMessage(msg string) (filename string, out []byte) {
|
|
curr_time := time.Now().Unix()
|
|
filename = fmt.Sprintf("%d.ogg", curr_time)
|
|
var cmd_args string = fmt.Sprintf("espeak-ng -s 120 -v mb-fr2 -p 30 '%s' --stdout | ffmpeg -i - -c:a libopus -f ogg pipe:1 2>/dev/null",
|
|
maxString(msg, 300))
|
|
cmd := exec.Command("sh", "-c", cmd_args)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
fmt.Println(fmt.Sprint(err))
|
|
}
|
|
return
|
|
}
|