1
0
Files
gogodiscordo/core/messages.go
adminoo cb5995640a i fucking hope multiple return values aren't good Go practices
createAudio send a mp3 file created with espeak
2020-12-06 12:04:40 +01:00

55 lines
1.4 KiB
Go

package core
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!")
}
if m.Content == "pong" {
s.ChannelMessageSend(m.ChannelID, "Ping!")
}
}
func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
var prefix string = "/gogodisco audio"
if strings.HasPrefix(m.Content, prefix) {
var message string = strings.TrimLeft(m.Content, prefix)
_, _, filename := createAudio(message)
// func (s *Session) ChannelFileSend(channelID, name string, r io.Reader) (*Message, error)
file, _ := os.Open(filename)
s.ChannelFileSend(m.ChannelID, filename, file)
}
}
func createAudio(msg string) ([]byte, error, string){
curr_time := time.Now().Unix()
var filename string = fmt.Sprintf("/tmp/%d.mp3", curr_time)
var cmd_args string = fmt.Sprintf("espeak-ng -s 120 -v mb-fr2 -p 30 %q -w %s", msg, filename)
cmd := exec.Command("sh","-c", cmd_args)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + string(out))
}
return out, err, filename
}