diff --git a/.gitignore b/.gitignore index 1ab9e5c..239b3eb 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -config.yaml \ No newline at end of file +config.yaml +gogodiscordo \ No newline at end of file diff --git a/core/commands.go b/core/commands.go new file mode 100644 index 0000000..b33d45c --- /dev/null +++ b/core/commands.go @@ -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) +} diff --git a/core/config.go b/core/config.go index 04ed052..91fb112 100644 --- a/core/config.go +++ b/core/config.go @@ -5,7 +5,6 @@ import ( "log" "gopkg.in/yaml.v2" - "github.com/bwmarrin/discordgo" ) type Config struct { @@ -20,16 +19,6 @@ type Config struct { } `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 { yamlFile, err := ioutil.ReadFile("config.yaml") if err != nil { @@ -42,23 +31,4 @@ func (c *Config) LoadConf() *Config { 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 diff --git a/core/core_test.go b/core/core_test.go index 66ee86f..194f75e 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -1,13 +1,13 @@ package core import ( - "fmt" + "strings" "testing" ) func TestCreateAudioSimple(t *testing.T) { var message string = "test 1 2 3" - filename, file := createAudio(message) + filename, file := getAudioMessage(message) if file == nil || len(filename) <= 0 { t.Fail() } @@ -29,10 +29,29 @@ func TestPrepareTTSMessageBigText(t *testing.T) { } } +// strip custom emojis func TestPrepareTTSEmoji(t *testing.T) { var message string = "This is awesome!!! :thumbsup: :nerd_face: :cop:" cleanMessage := prepareTTSMessage(message) if cleanMessage != "This is awesome!!!" { - 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() } } diff --git a/core/messages.go b/core/messages.go index 2be3e3c..83bd493 100644 --- a/core/messages.go +++ b/core/messages.go @@ -12,7 +12,7 @@ import ( "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() 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", @@ -26,7 +26,7 @@ func createAudio(msg string) (filename string, file *bytes.Reader) { return } -func createHelpMessage() string { +func getHelpMessage() string { // build list of possible commands // 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 @@ -46,6 +46,18 @@ func createHelpMessage() string { 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) { for alias, _ := range CommandRegister { 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) { - 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))]) - } - s.ChannelMessageSend(m.ChannelID, strings.Join(bag, " ")) + s.ChannelMessageSend(m.ChannelID, getHatMessage()) } 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) { - s.ChannelMessageSend(m.ChannelID, createHelpMessage()) + s.ChannelMessageSend(m.ChannelID, getHelpMessage()) } 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) { var cleanMessage string = prepareTTSMessage(message) - filename, file := createAudio(cleanMessage) + filename, file := getAudioMessage(cleanMessage) s.ChannelFileSend(m.ChannelID, filename, file) } diff --git a/main.go b/main.go index 215dde4..23b7397 100644 --- a/main.go +++ b/main.go @@ -13,33 +13,13 @@ import ( func main() { core.ConfigRegister.LoadConf() + core.CommandInit() discord, err := discordgo.New("Bot " + core.ConfigRegister.Token) if err != nil { log.Println("error creating Discord session, ", err) 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.MessageTalkback)