From 2c767fa8ff2925b9cb0208afe43169de077ff68e Mon Sep 17 00:00:00 2001 From: adminoo Date: Mon, 7 Dec 2020 19:33:33 +0100 Subject: [PATCH] dynamically generated help message --- core/config.go | 39 ++++++++++++++++++++++++++------------- core/core_test.go | 1 + core/messages.go | 24 +++++++++++++++++++----- main.go | 22 +++++++++++++++++----- 4 files changed, 63 insertions(+), 23 deletions(-) diff --git a/core/config.go b/core/config.go index 0a29039..2154754 100644 --- a/core/config.go +++ b/core/config.go @@ -8,12 +8,22 @@ import ( "github.com/bwmarrin/discordgo" ) -type ConfigRegister struct { +type Config struct { Token string `yaml:"token"` Name string `yaml:"name"` } -func (c *ConfigRegister) LoadConf() *ConfigRegister { +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 { log.Printf("yamlFile.Get err #%v ", err) @@ -26,19 +36,22 @@ func (c *ConfigRegister) LoadConf() *ConfigRegister { } var CommandRegister = make( - map[string]func( - s *discordgo.Session, - m *discordgo.MessageCreate, - message string)) + map[string]Command) -func SetCommand(aliases []string, command func( - s *discordgo.Session, - m *discordgo.MessageCreate, - message string)) { +func SetCommand( + aliases []string, + desc string, + example string, + command func(s *discordgo.Session, m *discordgo.MessageCreate, message string)) { for _, alias := range aliases { - CommandRegister[alias] = command - log.Printf("added %s to Register", alias) + c := Command{ + name: alias, + desc: desc, + example: example, + command: command} + CommandRegister[alias] = c + log.Printf("added %s to Register", alias) } } -var Config ConfigRegister +var ConfigRegister Config diff --git a/core/core_test.go b/core/core_test.go index 3ced1c7..66ee86f 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -1,6 +1,7 @@ package core import ( + "fmt" "testing" ) diff --git a/core/messages.go b/core/messages.go index 68efbe9..bd8b5a3 100644 --- a/core/messages.go +++ b/core/messages.go @@ -24,23 +24,37 @@ func createAudio(msg string) (filename string, file *bytes.Reader) { return } +func createHelpMessage() string { + // build list of possible commands + 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( + "%s : %s (Example : %s)\n", + command.name, + command.desc, + command.example)) + } + return strings.Join(help, "") +} + func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { - for alias, command := range CommandRegister { + for alias, _ := range CommandRegister { if strings.HasPrefix(m.Content, alias) { if m.Author.ID == s.State.User.ID { return } var message string = strings.TrimPrefix(m.Content, alias) fmt.Println(message) - command(s, m, message) + CommandRegister[alias].command(s, m, message) } } } func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate, message string) { - // build list of possible commands - fmt.Println(message) - return + s.ChannelMessageSend(m.ChannelID, createHelpMessage()) } func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message string) { diff --git a/main.go b/main.go index 7f84c37..ad5ceb9 100644 --- a/main.go +++ b/main.go @@ -12,16 +12,28 @@ import ( ) func main() { - core.Config.LoadConf() - discord, err := discordgo.New("Bot " + core.Config.Token) + core.ConfigRegister.LoadConf() + discord, err := discordgo.New("Bot " + core.ConfigRegister.Token) if err != nil { fmt.Println("error creating Discord session, ", err) return } - core.SetCommand([]string{"/ggd audio", "/gogodisco audio"}, core.MessageAudio) - core.SetCommand([]string{"ping", "pong"}, core.MessagePing) - core.SetCommand([]string{"/ggd help", "/gogodisco help"}, core.MessageHelp) + 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) discord.AddHandler(core.MessageHandler)