ready for testing??
This commit is contained in:
parent
6e65f9165b
commit
0829c2a39d
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
@ -13,20 +15,95 @@ type Command interface {
|
|||||||
Execute(s *discordgo.Session, m *discordgo.MessageCreate)
|
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 */
|
/* The CommandMux struct is a type of mux for Discord commands. It's modelled after the net/http ServeMux */
|
||||||
type CommandMux struct {
|
type CommandMux struct {
|
||||||
m map[string]muxEntry
|
m map[string]muxEntry
|
||||||
prefix string
|
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) {
|
func (c *CommandMux) Handler(m *discordgo.MessageCreate) (cmd Command, pattern string) {
|
||||||
if strings.HasPrefix(m.Content, c.prefix) {
|
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) {
|
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)
|
/* The muxEntry struct contains the actual Command implementation as well as the pattern (discord command)
|
||||||
|
@ -10,6 +10,12 @@ func (app *application) messageCreate(s *discordgo.Session, m *discordgo.Message
|
|||||||
if m.Author.Bot {
|
if m.Author.Bot {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(m.Content, "!newpepe") {
|
||||||
|
app.commandMux.Execute(s, m)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
app.limiter.LogInteraction(m.Author.ID, "messagecreate")
|
app.limiter.LogInteraction(m.Author.ID, "messagecreate")
|
||||||
|
|
||||||
/* Check if the user is even allowed by the rate limiter */
|
/* Check if the user is even allowed by the rate limiter */
|
||||||
|
7
discord/handlers.go
Normal file
7
discord/handlers.go
Normal file
@ -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!")
|
||||||
|
}
|
@ -62,6 +62,10 @@ func main() {
|
|||||||
Logs: make(map[string][]*limiter.Action),
|
Logs: make(map[string][]*limiter.Action),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mux := NewCommandMux()
|
||||||
|
mux.prefix = "!newpepe"
|
||||||
|
mux.HandleFunc("newcringe", newCringe)
|
||||||
|
|
||||||
app := &application{
|
app := &application{
|
||||||
infoLog: infoLog,
|
infoLog: infoLog,
|
||||||
errorLog: errorLog,
|
errorLog: errorLog,
|
||||||
@ -69,6 +73,7 @@ func main() {
|
|||||||
adminroles: &mysql.AdminRolesModel{DB: db},
|
adminroles: &mysql.AdminRolesModel{DB: db},
|
||||||
trigger: "!pepe",
|
trigger: "!pepe",
|
||||||
limiter: limiter,
|
limiter: limiter,
|
||||||
|
commandMux: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
app.allBadWords, err = app.badwords.AllWords()
|
app.allBadWords, err = app.badwords.AllWords()
|
||||||
|
Loading…
Reference in New Issue
Block a user