From 03d23a56668c9f042cedcdece46306d4a231a41f Mon Sep 17 00:00:00 2001 From: adminoo Date: Sun, 6 Dec 2020 00:06:21 +0100 Subject: [PATCH] core package, embryonic espeak function and tests --- core/config.go | 2 +- core/core_test.go | 13 ++++++++++++ core/messages.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 13 +++++++----- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 core/core_test.go create mode 100644 core/messages.go diff --git a/core/config.go b/core/config.go index 01bc599..6a40421 100644 --- a/core/config.go +++ b/core/config.go @@ -1,4 +1,4 @@ -package config +package core import ( "io/ioutil" diff --git a/core/core_test.go b/core/core_test.go new file mode 100644 index 0000000..59075ee --- /dev/null +++ b/core/core_test.go @@ -0,0 +1,13 @@ +package core + +import ( + "testing" +) + +func TestCreateAudioSimple(t *testing.T) { + var message string = "test 1 2 3" + _, err := createAudio(message) + if err != nil { + t.Fail() + } +} diff --git a/core/messages.go b/core/messages.go new file mode 100644 index 0000000..de6e576 --- /dev/null +++ b/core/messages.go @@ -0,0 +1,51 @@ +package core + +import ( + "fmt" + "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) + createAudio(message) + } +} + +func createAudio(msg string) ([]byte, error){ + curr_time := time.Now().Unix() + var filename string = fmt.Sprintf("/tmp/%d.mp3", curr_time) + fmt.Println(filename) + var cmd_args string = fmt.Sprintf("-s 120 -v mb-fr2 -p 30 '%s'", msg) + cmd := exec.Command("espeak-ng", cmd_args) + out, err := cmd.CombinedOutput() + if err != nil { + fmt.Println(fmt.Sprint(err) + ": " + string(out)) + } + return out, err +} diff --git a/main.go b/main.go index 6c43719..d55fd48 100644 --- a/main.go +++ b/main.go @@ -2,25 +2,28 @@ package main import ( "ariona.fr/git/gator/gogodisco/core" - + "fmt" "os" "os/signal" "syscall" - + "github.com/bwmarrin/discordgo" ) func main() { - var c config.Config - c.LoadConf() - + var c core.Config + c.LoadConf() + discord, err := discordgo.New("Bot " + c.Token) if err != nil { fmt.Println("error creating Discord session, ", err) return } + discord.AddHandler(core.MessagePing) + discord.AddHandler(core.MessageAudio) + err = discord.Open() if err != nil { fmt.Println("error opening connection, ", err)