all repos — paprika @ 1a98ec30e37891299d2615ec709eb46f9302bc57

go rewrite of taigabot

Simple sanity check plugin
Anirudh Oppiliappan x@icyphox.sh
Sun, 14 Nov 2021 14:40:37 +0530
commit

1a98ec30e37891299d2615ec709eb46f9302bc57

parent

90f2396d71393631a9f0df11815218cbd49b0071

3 files changed, 49 insertions(+), 11 deletions(-)

jump to
M main.gomain.go

@@ -14,12 +14,17 @@ case "001":

// TODO: load this from config c.Write("JOIN #taigobot-test") case "PRIVMSG": - if m.Trailing()[:1] == "." { - err := plugins.ProcessCommands(m.Trailing()) - if err != nil { - c.Writef("error: %v", err) - } + response, err := plugins.ProcessTrigger(m) + if err != nil { + c.Writef("error: %v", err) } + c.WriteMessage(&irc.Message{ + Command: "PRIVMSG", + Params: []string{ + m.Params[0], + response, + }, + }) } }
A plugins/hello.go

@@ -0,0 +1,20 @@

+// Sanity check plugin. +package plugins + +import ( + "gopkg.in/irc.v3" +) + +func init() { + Register(Hello{}) +} + +type Hello struct{} + +func (Hello) Triggers() []string { + return []string{".hello", "taigobot"} +} + +func (Hello) Execute(m *irc.Message) (string, error) { + return "hello, " + m.Prefix.Name, nil +}
M plugins/plugins.goplugins/plugins.go

@@ -1,8 +1,14 @@

package plugins +import ( + "strings" + + "gopkg.in/irc.v3" +) + type Plugin interface { Triggers() []string - Execute() error + Execute(m *irc.Message) (string, error) } var Plugins = make(map[string]Plugin)

@@ -13,14 +19,21 @@ Plugins[t] = p

} } -func ProcessCommands(cmd string) error { +// Checks for triggers in a message and executes its +// corresponding plugin, returning the response/error. +func ProcessTrigger(m *irc.Message) (string, error) { + var ( + response string + err error + ) for trigger, plugin := range Plugins { - if cmd == trigger { - err := plugin.Execute() + if strings.HasPrefix(m.Trailing(), trigger) { + response, err = plugin.Execute(m) if err != nil { - return err + return "", err } + return response, nil } } - return nil + return "", nil }