moving things around and stub before creating a cmd register
This commit is contained in:
@ -4,60 +4,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// source on www.socketloop.com
|
|
||||||
func removeCharacters(input string, characters string) string {
|
|
||||||
filter := func(r rune) rune {
|
|
||||||
if strings.IndexRune(characters, r) < 0 {
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return strings.Map(filter, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/34839659/how-can-i-easily-get-a-substring-in-go-while-guarding-against-slice-bounds-out
|
|
||||||
func maxString(s string, max int) string {
|
|
||||||
if len(s) > max {
|
|
||||||
r := 0
|
|
||||||
for i := range s {
|
|
||||||
r++
|
|
||||||
if r > max {
|
|
||||||
return s[:i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// this only strip custom emoji, not default unicode ones
|
|
||||||
func stripEmoji(msg string) string {
|
|
||||||
re := regexp.MustCompile(`:\w+:`)
|
|
||||||
match := re.FindAll([]byte(msg), -1)
|
|
||||||
if len(match) > 0 {
|
|
||||||
s := msg
|
|
||||||
for i := range match {
|
|
||||||
s = strings.Replace(s, string(match[i]), "", -1)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
} else {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func prepareTTSMessage(msg string) string {
|
|
||||||
t1 := maxString(msg, 300)
|
|
||||||
t2 := stripEmoji(t1)
|
|
||||||
t3 := removeCharacters(t2, "-\"'`$();:.\\")
|
|
||||||
return strings.Trim(t3, " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
func createAudio(msg string) (filename string, file *bytes.Reader) {
|
func createAudio(msg string) (filename string, file *bytes.Reader) {
|
||||||
curr_time := time.Now().Unix()
|
curr_time := time.Now().Unix()
|
||||||
filename = fmt.Sprintf("/tmp/%d.mp3", curr_time)
|
filename = fmt.Sprintf("/tmp/%d.mp3", curr_time)
|
||||||
@ -72,9 +24,17 @@ func createAudio(msg string) (filename string, file *bytes.Reader) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if m.Content == "/ggd help" {
|
||||||
|
// build list of possible commands
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate) {
|
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 {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -92,7 +52,7 @@ func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var prefix string = "/gogodisco audio"
|
var prefix string = "/ggd tts"
|
||||||
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)
|
||||||
|
|||||||
53
core/text.go
Normal file
53
core/text.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// source on www.socketloop.com
|
||||||
|
func removeCharacters(input string, characters string) string {
|
||||||
|
filter := func(r rune) rune {
|
||||||
|
if strings.IndexRune(characters, r) < 0 {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return strings.Map(filter, input)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/34839659/how-can-i-easily-get-a-substring-in-go-while-guarding-against-slice-bounds-out
|
||||||
|
func maxString(s string, max int) string {
|
||||||
|
if len(s) > max {
|
||||||
|
r := 0
|
||||||
|
for i := range s {
|
||||||
|
r++
|
||||||
|
if r > max {
|
||||||
|
return s[:i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// this only strip custom emoji, not default unicode ones
|
||||||
|
func stripEmoji(msg string) string {
|
||||||
|
re := regexp.MustCompile(`:\w+:`)
|
||||||
|
match := re.FindAll([]byte(msg), -1)
|
||||||
|
if len(match) > 0 {
|
||||||
|
s := msg
|
||||||
|
for i := range match {
|
||||||
|
s = strings.Replace(s, string(match[i]), "", -1)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
} else {
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareTTSMessage(msg string) string {
|
||||||
|
t1 := maxString(msg, 300)
|
||||||
|
t2 := stripEmoji(t1)
|
||||||
|
t3 := removeCharacters(t2, "-\"'`$();:.\\")
|
||||||
|
return strings.Trim(t3, " ")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user