diff --git a/config.example.yaml b/config.example.yaml index 6ee4f13..a000f83 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,2 +1,2 @@ token: "some_really_long_string" -name: "gogodiscordo" +name: "Gogodiscordo" diff --git a/core/config.go b/core/config.go index 2154754..04ed052 100644 --- a/core/config.go +++ b/core/config.go @@ -11,6 +11,13 @@ import ( type Config struct { Token string `yaml:"token"` Name string `yaml:"name"` + Talkback[] struct { + Domain struct { + Name string `yaml:"name"` + Patterns []string `yaml:"patterns"` + Answers []string `yaml:"answers"` + } `yaml:"domain"` + } `yaml:"talkback"` } type Command struct { diff --git a/core/messages.go b/core/messages.go index bd8b5a3..5d7cfa2 100644 --- a/core/messages.go +++ b/core/messages.go @@ -4,6 +4,8 @@ import ( "bytes" "fmt" "os/exec" + "math/rand" + "sort" "strings" "time" @@ -26,9 +28,9 @@ func createAudio(msg string) (filename string, file *bytes.Reader) { func createHelpMessage() 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 var help []string - help = append(help, - fmt.Sprintf("Hello, I'm **%s**. Here are the things I can do :\n", ConfigRegister.Name)) for _, command := range CommandRegister { help = append(help, fmt.Sprintf( @@ -37,7 +39,11 @@ func createHelpMessage() string { command.desc, command.example)) } - return strings.Join(help, "") + sort.Strings(help) + welcome := fmt.Sprintf( + "Hello, I'm **%s**. Here are the things I can do :\n", + ConfigRegister.Name) + return welcome + strings.Join(help, "") } func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { @@ -47,12 +53,28 @@ func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { return } var message string = strings.TrimPrefix(m.Content, alias) - fmt.Println(message) CommandRegister[alias].command(s, m, message) } } } +func MessageTalkback(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID == s.State.User.ID { + return + } + for _, t := range ConfigRegister.Talkback { + for _, p := range t.Domain.Patterns { + rand.Seed(time.Now().UnixNano()) + if strings.Contains(strings.ToLower(m.Content), strings.ToLower(p)) && + rand.Intn(2) == 1 { + rand.Seed(time.Now().UnixNano()) + answer := t.Domain.Answers[rand.Intn(len(t.Domain.Answers))] + s.ChannelMessageSend(m.ChannelID, answer) + } + } + } +} + func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate, message string) { s.ChannelMessageSend(m.ChannelID, createHelpMessage()) } diff --git a/main.go b/main.go index ad5ceb9..b704797 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "ariona.fr/git/gator/gogodiscordo/core" - "fmt" + "log" "os" "os/signal" "syscall" @@ -15,7 +15,7 @@ func main() { core.ConfigRegister.LoadConf() discord, err := discordgo.New("Bot " + core.ConfigRegister.Token) if err != nil { - fmt.Println("error creating Discord session, ", err) + log.Println("error creating Discord session, ", err) return } @@ -36,15 +36,16 @@ func main() { core.MessageHelp) discord.AddHandler(core.MessageHandler) + discord.AddHandler(core.MessageTalkback) err = discord.Open() if err != nil { - fmt.Println("error opening connection, ", err) + log.Println("error opening connection, ", err) return } // Wait here until CTRL-C or other term signal is received. - fmt.Println("Bot is now running. Press CTRL-C to exit.") + log.Println("Bot is now running. Press CTRL-C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) <-sc