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" "log"
) )
// represent a command to input to the bot through a Discord message
type Command struct { type Command struct {
name string name string // name identifier
desc string desc string // description
example string example string // usage example
command func( command func(
s *discordgo.Session, s *discordgo.Session,
m *discordgo.MessageCreate, m *discordgo.MessageCreate,
message string) message string) // command (a callback)
} }
// register containing commands associated with their aliases
var CommandRegister = make( var CommandRegister = make(
map[string]Command) map[string]Command)
// associate a Command with an alias within the command register
func SetCommand( func SetCommand(
aliases []string, aliases []string,
desc string, desc string,
@ -34,6 +37,7 @@ func SetCommand(
} }
} }
// add commands to the register
func CommandInit() { func CommandInit() {
SetCommand( SetCommand(
[]string{"/ggd audio", "/ggd tts"}, []string{"/ggd audio", "/ggd tts"},

View File

@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// entity storing configuration values loaded from a yaml file
type Config struct { type Config struct {
Token string `yaml:"token"` Token string `yaml:"token"`
Name string `yaml:"name"` Name string `yaml:"name"`
@ -19,6 +20,7 @@ type Config struct {
} `yaml:"talkback"` } `yaml:"talkback"`
} }
// loads the values from the yaml configuration file into the configuration register
func (c *Config) LoadConf() *Config { func (c *Config) LoadConf() *Config {
yamlFile, err := ioutil.ReadFile("config.yaml") yamlFile, err := ioutil.ReadFile("config.yaml")
if err != nil { if err != nil {
@ -31,4 +33,5 @@ func (c *Config) LoadConf() *Config {
return c return c
} }
// global configuration register
var ConfigRegister Config var ConfigRegister Config

View File

@ -3,6 +3,7 @@ package core
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log"
"math/rand" "math/rand"
"sort" "sort"
"strings" "strings"
@ -47,6 +48,7 @@ func getHelpMessage() string {
return welcome + strings.Join(help, "") return welcome + strings.Join(help, "")
} }
// return random hats
func getHatMessage() string { func getHatMessage() string {
hat := []string{"🧢", "👒", "⛑", "🎩", "🎓"} hat := []string{"🧢", "👒", "⛑", "🎩", "🎓"}
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@ -59,6 +61,8 @@ func getHatMessage() string {
return strings.Join(bag, " ") 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 { func getTalkbackMessage(message string) string {
for _, t := range ConfigRegister.Talkback { for _, t := range ConfigRegister.Talkback {
for _, p := range t.Domain.Patterns { for _, p := range t.Domain.Patterns {
@ -72,6 +76,7 @@ func getTalkbackMessage(message string) string {
return "" return ""
} }
// check if a message is a valid command. if yes, execute the command
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
for alias, _ := range CommandRegister { for alias, _ := range CommandRegister {
if strings.HasPrefix(m.Content, alias) { 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) { func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
log.Println("Messageaudio :", message)
var cleanMessage string = prepareTTSMessage(message) var cleanMessage string = prepareTTSMessage(message)
filename, out := getAudioMessage(cleanMessage) filename, out := getAudioMessage(cleanMessage)
file := bytes.NewReader(out) file := bytes.NewReader(out)
s.ChannelFileSend(m.ChannelID, filename, file) s.ChannelFileSend(m.ChannelID, filename, file)
// delete the receveid command after processing it. less clutter // delete the receveid command after processing it. less clutter
log.Println("Messageaudio :", cleanMessage)
s.ChannelMessageDelete(m.ChannelID, m.ID) 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 // 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 { func maxString(s string, max int) string {
if len(s) > max { if len(s) > max {
r := 0 r := 0
@ -45,6 +46,7 @@ func stripEmoji(msg string) string {
} }
} }
// sanitize received message for text-to-speech
func prepareTTSMessage(msg string) string { func prepareTTSMessage(msg string) string {
t1 := maxString(msg, 300) t1 := maxString(msg, 300)
t2 := stripEmoji(t1) t2 := stripEmoji(t1)

View File

@ -12,8 +12,11 @@ import (
) )
func main() { func main() {
// load yaml config file into register
core.ConfigRegister.LoadConf() core.ConfigRegister.LoadConf()
core.CommandInit() core.CommandInit()
discord, err := discordgo.New("Bot " + core.ConfigRegister.Token) discord, err := discordgo.New("Bot " + core.ConfigRegister.Token)
if err != nil { if err != nil {
log.Println("error creating Discord session, ", err) log.Println("error creating Discord session, ", err)
@ -24,7 +27,6 @@ func main() {
discordgo.IntentsGuildMessages | discordgo.IntentsGuildMessages |
discordgo.IntentsGuildVoiceStates) discordgo.IntentsGuildVoiceStates)
discord.AddHandler(core.MessageHandler) discord.AddHandler(core.MessageHandler)
discord.AddHandler(core.MessageTalkback) discord.AddHandler(core.MessageTalkback)
discord.AddHandler(core.GuildCreate) discord.AddHandler(core.GuildCreate)