1
0

dynamically generated help message

This commit is contained in:
2020-12-07 19:33:33 +01:00
parent 95c0d12fa3
commit 2c767fa8ff
4 changed files with 63 additions and 23 deletions

View File

@ -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

View File

@ -1,6 +1,7 @@
package core
import (
"fmt"
"testing"
)

View File

@ -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) {

22
main.go
View File

@ -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)