1
0

commented code

This commit is contained in:
2022-05-07 11:03:14 +02:00
parent 6bbae4da79
commit e8d5293911
5 changed files with 24 additions and 5 deletions

View File

@ -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"},

View File

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

View File

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

View File

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