1
0

decoupled some stuffs, news related tests, new commands file

This commit is contained in:
2020-12-08 14:47:34 +01:00
parent 804ed6ee5b
commit 53d01e04fe
6 changed files with 103 additions and 68 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@
# vendor/ # vendor/
config.yaml config.yaml
gogodiscordo

61
core/commands.go Normal file
View File

@ -0,0 +1,61 @@
package core
import (
"github.com/bwmarrin/discordgo"
"log"
)
type Command struct {
name string
desc string
example string
command func(
s *discordgo.Session,
m *discordgo.MessageCreate,
message string)
}
var CommandRegister = make(
map[string]Command)
func SetCommand(
aliases []string,
desc string,
example string,
command func(s *discordgo.Session, m *discordgo.MessageCreate, message string)) {
for _, alias := range aliases {
c := Command{
name: alias,
desc: desc,
example: example,
command: command}
CommandRegister[alias] = c
log.Printf("added %s to Register", alias)
}
}
func CommandInit() {
SetCommand(
[]string{"/ggd audio", "/ggd tts"},
"*Attach a TTS version of the message on the channel*",
"`/ggd [audio|tts] hello everyone`",
MessageAudio)
SetCommand(
[]string{"ping", "pong"},
"*Check if the bot is alive*",
"`[ping|pong]`",
MessagePing)
SetCommand(
[]string{"/ggd help", "/ggd info"},
"*Display the available list of commands*",
"`/ggd [help|info]`",
MessageHelp)
SetCommand(
[]string{"/ggd hat", "/ggd chapeau", "/ggd chapo"},
"*Hand over a set of fancy hats*",
"`/ggd [hat|chapeau|chapo]`",
MessageHat)
}

View File

@ -5,7 +5,6 @@ import (
"log" "log"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/bwmarrin/discordgo"
) )
type Config struct { type Config struct {
@ -20,16 +19,6 @@ type Config struct {
} `yaml:"talkback"` } `yaml:"talkback"`
} }
type Command struct {
name string
desc string
example string
command func(
s *discordgo.Session,
m *discordgo.MessageCreate,
message string)
}
func (c *Config) LoadConf() *Config { func (c *Config) LoadConf() *Config {
yamlFile, err := ioutil.ReadFile("config.yaml") yamlFile, err := ioutil.ReadFile("config.yaml")
if err != nil { if err != nil {
@ -42,23 +31,4 @@ func (c *Config) LoadConf() *Config {
return c return c
} }
var CommandRegister = make(
map[string]Command)
func SetCommand(
aliases []string,
desc string,
example string,
command func(s *discordgo.Session, m *discordgo.MessageCreate, message string)) {
for _, alias := range aliases {
c := Command{
name: alias,
desc: desc,
example: example,
command: command}
CommandRegister[alias] = c
log.Printf("added %s to Register", alias)
}
}
var ConfigRegister Config var ConfigRegister Config

View File

@ -1,13 +1,13 @@
package core package core
import ( import (
"fmt" "strings"
"testing" "testing"
) )
func TestCreateAudioSimple(t *testing.T) { func TestCreateAudioSimple(t *testing.T) {
var message string = "test 1 2 3" var message string = "test 1 2 3"
filename, file := createAudio(message) filename, file := getAudioMessage(message)
if file == nil || len(filename) <= 0 { if file == nil || len(filename) <= 0 {
t.Fail() t.Fail()
} }
@ -29,6 +29,7 @@ func TestPrepareTTSMessageBigText(t *testing.T) {
} }
} }
// strip custom emojis
func TestPrepareTTSEmoji(t *testing.T) { func TestPrepareTTSEmoji(t *testing.T) {
var message string = "This is awesome!!! :thumbsup: :nerd_face: :cop:" var message string = "This is awesome!!! :thumbsup: :nerd_face: :cop:"
cleanMessage := prepareTTSMessage(message) cleanMessage := prepareTTSMessage(message)
@ -36,3 +37,21 @@ func TestPrepareTTSEmoji(t *testing.T) {
t.Fail() t.Fail()
} }
} }
// contains every commands from the register + the welcome message
func TestGetHelpMessage(t *testing.T) {
//ConfigRegister.LoadConf()
CommandInit()
var help string = getHelpMessage()
if strings.Count(help, "\n") != (len(CommandRegister) + 1) {
t.Fail()
}
}
// it's just hats, what else is there to check
func TestGetHatMessage(t *testing.T) {
var hats string = getHatMessage()
if len(hats) <= 0 {
t.Fail()
}
}

View File

@ -12,7 +12,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func createAudio(msg string) (filename string, file *bytes.Reader) { func getAudioMessage(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)
var cmd_args string = fmt.Sprintf("espeak-ng -s 120 -v mb-fr2 -p 30 '%s' --stdout", var cmd_args string = fmt.Sprintf("espeak-ng -s 120 -v mb-fr2 -p 30 '%s' --stdout",
@ -26,7 +26,7 @@ func createAudio(msg string) (filename string, file *bytes.Reader) {
return return
} }
func createHelpMessage() string { func getHelpMessage() string {
// build list of possible commands // build list of possible commands
// TODO: currently, the help needs to get value initiated at startup time to be built. // TODO: currently, the help needs to get value initiated at startup time to be built.
// I don't like that and wish to test what is rendered without connecting to the server // I don't like that and wish to test what is rendered without connecting to the server
@ -46,6 +46,18 @@ func createHelpMessage() string {
return welcome + strings.Join(help, "") return welcome + strings.Join(help, "")
} }
func getHatMessage() string {
hat := []string{"🧢", "👒", "⛑", "🎩", "🎓"}
rand.Seed(time.Now().UnixNano())
var amount int = rand.Intn(len(hat))
var bag []string
for i := 0; i < amount; i++ {
rand.Seed(time.Now().UnixNano())
bag = append(bag, hat[rand.Intn(len(hat))])
}
return strings.Join(bag, " ")
}
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
for alias, _ := range CommandRegister { for alias, _ := range CommandRegister {
if strings.HasPrefix(m.Content, alias) { if strings.HasPrefix(m.Content, alias) {
@ -59,15 +71,7 @@ func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
func MessageHat(s *discordgo.Session, m *discordgo.MessageCreate, message string) { func MessageHat(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
hat := []string{"🧢", "👒", "⛑", "🎩", "🎓"} s.ChannelMessageSend(m.ChannelID, getHatMessage())
rand.Seed(time.Now().UnixNano())
var amount int = rand.Intn(len(hat))
var bag []string
for i := 0; i < amount; i++ {
rand.Seed(time.Now().UnixNano())
bag = append(bag, hat[rand.Intn(len(hat))])
}
s.ChannelMessageSend(m.ChannelID, strings.Join(bag, " "))
} }
func MessageTalkback(s *discordgo.Session, m *discordgo.MessageCreate) { func MessageTalkback(s *discordgo.Session, m *discordgo.MessageCreate) {
@ -88,7 +92,7 @@ func MessageTalkback(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate, message string) { func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
s.ChannelMessageSend(m.ChannelID, createHelpMessage()) s.ChannelMessageSend(m.ChannelID, getHelpMessage())
} }
func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message string) { func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
@ -103,6 +107,6 @@ func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message strin
func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate, message string) { func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
var cleanMessage string = prepareTTSMessage(message) var cleanMessage string = prepareTTSMessage(message)
filename, file := createAudio(cleanMessage) filename, file := getAudioMessage(cleanMessage)
s.ChannelFileSend(m.ChannelID, filename, file) s.ChannelFileSend(m.ChannelID, filename, file)
} }

22
main.go
View File

@ -13,33 +13,13 @@ import (
func main() { func main() {
core.ConfigRegister.LoadConf() core.ConfigRegister.LoadConf()
core.CommandInit()
discord, err := discordgo.New("Bot " + core.ConfigRegister.Token) discord, err := discordgo.New("Bot " + core.ConfigRegister.Token)
if err != nil { if err != nil {
log.Println("error creating Discord session, ", err) log.Println("error creating Discord session, ", err)
return return
} }
core.SetCommand(
[]string{"/ggd audio", "/ggd tts"},
"*Attach a TTS version of the message on the channel*",
"`/ggd [audio|tts] hello everyone`",
core.MessageAudio)
core.SetCommand(
[]string{"ping", "pong"},
"*Check if the bot is alive*",
"`[ping|pong]`",
core.MessagePing)
core.SetCommand(
[]string{"/ggd help", "/ggd info"},
"*Display the available list of commands*",
"`/ggd [help|info]`",
core.MessageHelp)
core.SetCommand(
[]string{"/ggd hat", "/ggd chapeau", "/ggd chapo"},
"*Hand over a set of fancy hats*",
"`/ggd [hat|chapeau|chapo]`",
core.MessageHat)
discord.AddHandler(core.MessageHandler) discord.AddHandler(core.MessageHandler)
discord.AddHandler(core.MessageTalkback) discord.AddHandler(core.MessageTalkback)