From 0829c2a39da4ff7cbd6ace29dc0aac08b6a0eb3d Mon Sep 17 00:00:00 2001 From: DutchEllie Date: Thu, 3 Jun 2021 12:50:42 +0200 Subject: [PATCH] ready for testing?? --- discord/commands.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ discord/discord.go | 6 ++++ discord/handlers.go | 7 +++++ discord/main.go | 5 +++ 4 files changed, 95 insertions(+) create mode 100644 discord/handlers.go diff --git a/discord/commands.go b/discord/commands.go index 128321a..7d28c76 100644 --- a/discord/commands.go +++ b/discord/commands.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "log" "strings" "github.com/bwmarrin/discordgo" @@ -13,20 +15,95 @@ type Command interface { Execute(s *discordgo.Session, m *discordgo.MessageCreate) } +type HandlerFunc func(s *discordgo.Session, m *discordgo.MessageCreate) + +func (f HandlerFunc) Execute(s *discordgo.Session, m *discordgo.MessageCreate) { + f(s, m) +} + /* The CommandMux struct is a type of mux for Discord commands. It's modelled after the net/http ServeMux */ type CommandMux struct { m map[string]muxEntry prefix string } +func NewCommandMux() *CommandMux { return new(CommandMux) } + +func (c *CommandMux) removeFirst(command string) (string, error) { + split := strings.SplitN(strings.TrimSpace(command), " ", 2) + if len(split) > 1 { + return split[1], nil + } + return "", fmt.Errorf("separation impossible on string: %s", command) +} + +func (c *CommandMux) firstCommand(command string) (string, error) { + split := strings.SplitN(strings.TrimSpace(command), " ", 2) + if len(split) > 0 { + return split[0], nil + } + return "", fmt.Errorf("separation impossible on string: %s", command) +} + func (c *CommandMux) Handler(m *discordgo.MessageCreate) (cmd Command, pattern string) { if strings.HasPrefix(m.Content, c.prefix) { + /* Special case for this bot alone. It has a command that is only it's prefix + So we check if the whole message is only the prefix before proceding. + So please don't forget to add the command, since it's totally hardcoded here. */ + if strings.TrimSpace(m.Content) == c.prefix { + return c.m[c.prefix].h, c.m[c.prefix].pattern + } + m, err := c.removeFirst(m.Content) /* Here the prefix is removed, so we're left with only the first keyword */ + if err != nil { + return nil, "" + } + fc, err := c.firstCommand(m) + if err != nil { + return nil, "" + } + cmd, ok := c.m[fc] + if ok { + return cmd.h, cmd.pattern + } } + + /* Here is where I might add the whole checking for bad words part */ + return nil, "" } func (c *CommandMux) Execute(s *discordgo.Session, m *discordgo.MessageCreate) { + h, _ := c.Handler(m) + if h == nil { + log.Printf("There exists no handler for %s\n", m.Content) + return + } + h.Execute(s, m) +} +func (c *CommandMux) Handle(pattern string, handler Command) { + if pattern == "" { + panic("commandmux: invalid pattern") + } + if handler == nil { + panic("commandmux: nil handler") + } + if _, exist := c.m[pattern]; exist { + panic("commandmux: multiple registrations for " + pattern) + } + + if c.m == nil { + c.m = make(map[string]muxEntry) + } + e := muxEntry{h: handler, pattern: pattern} + c.m[pattern] = e +} + +func (c *CommandMux) HandleFunc(pattern string, handler func(s *discordgo.Session, m *discordgo.MessageCreate)) { + if handler == nil { + panic("commandmux: nil handler") + } + c.Handle(pattern, HandlerFunc(handler)) } /* The muxEntry struct contains the actual Command implementation as well as the pattern (discord command) diff --git a/discord/discord.go b/discord/discord.go index 9515bf6..ddcef4f 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -10,6 +10,12 @@ func (app *application) messageCreate(s *discordgo.Session, m *discordgo.Message if m.Author.Bot { return } + + if strings.HasPrefix(m.Content, "!newpepe") { + app.commandMux.Execute(s, m) + return + } + app.limiter.LogInteraction(m.Author.ID, "messagecreate") /* Check if the user is even allowed by the rate limiter */ diff --git a/discord/handlers.go b/discord/handlers.go new file mode 100644 index 0000000..bde38c5 --- /dev/null +++ b/discord/handlers.go @@ -0,0 +1,7 @@ +package main + +import "github.com/bwmarrin/discordgo" + +func newCringe(s *discordgo.Session, m *discordgo.MessageCreate) { + s.ChannelMessageSend(m.ChannelID, "this is a test message right from the new command system!") +} diff --git a/discord/main.go b/discord/main.go index 6d61691..c69c5bf 100644 --- a/discord/main.go +++ b/discord/main.go @@ -62,6 +62,10 @@ func main() { Logs: make(map[string][]*limiter.Action), } + mux := NewCommandMux() + mux.prefix = "!newpepe" + mux.HandleFunc("newcringe", newCringe) + app := &application{ infoLog: infoLog, errorLog: errorLog, @@ -69,6 +73,7 @@ func main() { adminroles: &mysql.AdminRolesModel{DB: db}, trigger: "!pepe", limiter: limiter, + commandMux: mux, } app.allBadWords, err = app.badwords.AllWords()