audio sent in a Reader rather than a file (no write on disk)
This commit is contained in:
@ -0,0 +1,2 @@
|
|||||||
|
token: "some_really_long_string"
|
||||||
|
name: "gogodiscordo"
|
||||||
|
|||||||
@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
func TestCreateAudioSimple(t *testing.T) {
|
func TestCreateAudioSimple(t *testing.T) {
|
||||||
var message string = "test 1 2 3"
|
var message string = "test 1 2 3"
|
||||||
_, err, _ := createAudio(message)
|
filename, file := createAudio(message)
|
||||||
if err != nil {
|
if file == nil || len(filename) <= 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,7 +29,7 @@ func TestPrepareTTSMessageBigText(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrepareTTSEmoji(t *testing.T) {
|
func TestPrepareTTSEmoji(t *testing.T) {
|
||||||
var message string = "This is awesome!!! :thumbsup: :nerd_face:"
|
var message string = "This is awesome!!! :thumbsup: :nerd_face: :cop:"
|
||||||
cleanMessage := prepareTTSMessage(message)
|
cleanMessage := prepareTTSMessage(message)
|
||||||
if cleanMessage != "This is awesome!!!" {
|
if cleanMessage != "This is awesome!!!" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -36,6 +36,7 @@ func maxString(s string, max int) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this only strip custom emoji, not default unicode ones
|
||||||
func stripEmoji(msg string) string {
|
func stripEmoji(msg string) string {
|
||||||
re := regexp.MustCompile(`:\w+:`)
|
re := regexp.MustCompile(`:\w+:`)
|
||||||
match := re.FindAll([]byte(msg), -1)
|
match := re.FindAll([]byte(msg), -1)
|
||||||
@ -57,18 +58,18 @@ func prepareTTSMessage(msg string) string {
|
|||||||
return strings.Trim(t3, " ")
|
return strings.Trim(t3, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAudio(msg string) ([]byte, error, string) {
|
func createAudio(msg string) (filename string, file *bytes.Reader) {
|
||||||
curr_time := time.Now().Unix()
|
curr_time := time.Now().Unix()
|
||||||
var filename string = fmt.Sprintf("/tmp/%d.mp3", curr_time)
|
filename = fmt.Sprintf("/tmp/%d.mp3", curr_time)
|
||||||
var cmd_args string = fmt.Sprintf("espeak-ng -s 120 -v mb-fr2 -p 30 '%s' -w %s",
|
var cmd_args string = fmt.Sprintf("espeak-ng -s 120 -v mb-fr2 -p 30 '%s' --stdout",
|
||||||
maxString(msg, 300),
|
maxString(msg, 300))
|
||||||
filename)
|
|
||||||
cmd := exec.Command("sh", "-c", cmd_args)
|
cmd := exec.Command("sh", "-c", cmd_args)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(fmt.Sprint(err) + ": " + string(out))
|
fmt.Println(fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
return out, err, filename
|
file = bytes.NewReader(out)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
@ -95,8 +96,7 @@ func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
if strings.HasPrefix(m.Content, prefix) {
|
if strings.HasPrefix(m.Content, prefix) {
|
||||||
var message string = strings.TrimPrefix(m.Content, prefix)
|
var message string = strings.TrimPrefix(m.Content, prefix)
|
||||||
var cleanMessage string = prepareTTSMessage(message)
|
var cleanMessage string = prepareTTSMessage(message)
|
||||||
_, _, filename := createAudio(cleanMessage)
|
filename, file := createAudio(cleanMessage)
|
||||||
file, _ := os.Open(filename)
|
|
||||||
s.ChannelFileSend(m.ChannelID, filename, file)
|
s.ChannelFileSend(m.ChannelID, filename, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user