diff --git a/core/commands.go b/core/commands.go index df08a9b..73abeca 100755 --- a/core/commands.go +++ b/core/commands.go @@ -5,19 +5,22 @@ import ( "log" ) +// represent a command to input to the bot through a Discord message type Command struct { - name string - desc string - example string + name string // name identifier + desc string // description + example string // usage example command func( s *discordgo.Session, m *discordgo.MessageCreate, - message string) + message string) // command (a callback) } +// register containing commands associated with their aliases var CommandRegister = make( map[string]Command) +// associate a Command with an alias within the command register func SetCommand( aliases []string, desc string, @@ -34,6 +37,7 @@ func SetCommand( } } +// add commands to the register func CommandInit() { SetCommand( []string{"/ggd audio", "/ggd tts"}, diff --git a/core/config.go b/core/config.go index 91fb112..29f307b 100755 --- a/core/config.go +++ b/core/config.go @@ -7,6 +7,7 @@ import ( "gopkg.in/yaml.v2" ) +// entity storing configuration values loaded from a yaml file type Config struct { Token string `yaml:"token"` Name string `yaml:"name"` @@ -19,6 +20,7 @@ type Config struct { } `yaml:"talkback"` } +// loads the values from the yaml configuration file into the configuration register func (c *Config) LoadConf() *Config { yamlFile, err := ioutil.ReadFile("config.yaml") if err != nil { @@ -31,4 +33,5 @@ func (c *Config) LoadConf() *Config { return c } +// global configuration register var ConfigRegister Config diff --git a/core/messages.go b/core/messages.go index 8d3cbf7..17c378a 100755 --- a/core/messages.go +++ b/core/messages.go @@ -3,6 +3,7 @@ package core import ( "bytes" "fmt" + "log" "math/rand" "sort" "strings" @@ -47,6 +48,7 @@ func getHelpMessage() string { return welcome + strings.Join(help, "") } +// return random hats func getHatMessage() string { hat := []string{"๐Ÿงข", "๐Ÿ‘’", "โ›‘", "๐ŸŽฉ", "๐ŸŽ“"} rand.Seed(time.Now().UnixNano()) @@ -59,6 +61,8 @@ func getHatMessage() string { return strings.Join(bag, " ") } +// returns a random message if the processed message matchs with a pattern. +// patterns are substrings belonging to a "domain" of lexical items func getTalkbackMessage(message string) string { for _, t := range ConfigRegister.Talkback { for _, p := range t.Domain.Patterns { @@ -72,6 +76,7 @@ func getTalkbackMessage(message string) string { return "" } +// check if a message is a valid command. if yes, execute the command func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { for alias, _ := range CommandRegister { if strings.HasPrefix(m.Content, alias) { @@ -113,12 +118,15 @@ func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message strin } } +// generate TTS version of the message and upload it to the channel func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate, message string) { + log.Println("Messageaudio :", message) var cleanMessage string = prepareTTSMessage(message) filename, out := getAudioMessage(cleanMessage) file := bytes.NewReader(out) s.ChannelFileSend(m.ChannelID, filename, file) // delete the receveid command after processing it. less clutter + log.Println("Messageaudio :", cleanMessage) s.ChannelMessageDelete(m.ChannelID, m.ID) } diff --git a/core/text.go b/core/text.go index 59d243b..ef7f9c4 100755 --- a/core/text.go +++ b/core/text.go @@ -17,6 +17,7 @@ func removeCharacters(input string, characters string) string { } // https://stackoverflow.com/questions/34839659/how-can-i-easily-get-a-substring-in-go-while-guarding-against-slice-bounds-out +// get a substring of size "max" from a string "s" func maxString(s string, max int) string { if len(s) > max { r := 0 @@ -45,6 +46,7 @@ func stripEmoji(msg string) string { } } +// sanitize received message for text-to-speech func prepareTTSMessage(msg string) string { t1 := maxString(msg, 300) t2 := stripEmoji(t1) diff --git a/main.go b/main.go index 4d6af70..75adb65 100755 --- a/main.go +++ b/main.go @@ -12,8 +12,11 @@ import ( ) func main() { + // load yaml config file into register core.ConfigRegister.LoadConf() + core.CommandInit() + discord, err := discordgo.New("Bot " + core.ConfigRegister.Token) if err != nil { log.Println("error creating Discord session, ", err) @@ -24,7 +27,6 @@ func main() { discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates) - discord.AddHandler(core.MessageHandler) discord.AddHandler(core.MessageTalkback) discord.AddHandler(core.GuildCreate)