From 95c0d12fa3c7a77b3d51362818d2b305a2418270 Mon Sep 17 00:00:00 2001 From: adminoo Date: Mon, 7 Dec 2020 17:44:45 +0100 Subject: [PATCH] messageHandler, changing project name slightly --- core/config.go | 8 ++++---- core/messages.go | 42 ++++++++++++++++++++---------------------- go.mod | 2 +- main.go | 10 ++++++---- readme.md | 4 ++-- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/core/config.go b/core/config.go index 5323f64..0a29039 100644 --- a/core/config.go +++ b/core/config.go @@ -28,17 +28,17 @@ func (c *ConfigRegister) LoadConf() *ConfigRegister { var CommandRegister = make( map[string]func( s *discordgo.Session, - m *discordgo.MessageCreate)) + m *discordgo.MessageCreate, + message string)) func SetCommand(aliases []string, command func( s *discordgo.Session, - m *discordgo.MessageCreate), - discord *discordgo.Session) { + m *discordgo.MessageCreate, + message string)) { for _, alias := range aliases { CommandRegister[alias] = command log.Printf("added %s to Register", alias) } - discord.AddHandler(command) } var Config ConfigRegister diff --git a/core/messages.go b/core/messages.go index c21785f..68efbe9 100644 --- a/core/messages.go +++ b/core/messages.go @@ -24,21 +24,26 @@ func createAudio(msg string) (filename string, file *bytes.Reader) { return } -func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } - if m.Content == "/ggd help" { - // build list of possible commands - return +func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { + for alias, command := 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) + } } } -func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } +func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate, message string) { + // build list of possible commands + fmt.Println(message) + return +} +func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message string) { if m.Content == "ping" { s.ChannelMessageSend(m.ChannelID, "Pong!") } @@ -48,15 +53,8 @@ func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate) { } } -func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } - var prefix string = "/ggd tts" - if strings.HasPrefix(m.Content, prefix) { - var message string = strings.TrimPrefix(m.Content, prefix) - var cleanMessage string = prepareTTSMessage(message) - filename, file := createAudio(cleanMessage) - s.ChannelFileSend(m.ChannelID, filename, file) - } +func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate, message string) { + var cleanMessage string = prepareTTSMessage(message) + filename, file := createAudio(cleanMessage) + s.ChannelFileSend(m.ChannelID, filename, file) } diff --git a/go.mod b/go.mod index 2de9384..25d3a2f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module ariona.fr/git/gator/gogodisco +module ariona.fr/git/gator/gogodiscordo go 1.15 diff --git a/main.go b/main.go index ee1a003..7f84c37 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "ariona.fr/git/gator/gogodisco/core" + "ariona.fr/git/gator/gogodiscordo/core" "fmt" "os" @@ -19,9 +19,11 @@ func main() { return } - core.SetCommand([]string{"/ggd audio", "/gogodisco audio"}, core.MessageAudio, discord) - core.SetCommand([]string{"/ggd ping", "/gogodisco ping"}, core.MessagePing, discord) - core.SetCommand([]string{"/ggd help", "/gogodisco help"}, core.MessageHelp, discord) + 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) + + discord.AddHandler(core.MessageHandler) err = discord.Open() if err != nil { diff --git a/readme.md b/readme.md index 4e289c1..d4f9b6c 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,5 @@ -# Gogodisco +# Gogodiscordo ## About -This is a simple bot in Go for the Discord messaging service, created for learning purpose and to quickly experiment with features discussed in different Discord channels I frequent. +This is a simple bot in Go for the Discord messaging service, created for learning purpose and to quickly experiment with features discussed in different Discord channels I frequent. The dumb name is an opportunistic reference to Inspector Gadget.