all repos — paprika @ 90f2396d71393631a9f0df11815218cbd49b0071

go rewrite of taigabot

Init taigobot
Anirudh Oppiliappan x@icyphox.sh
Sun, 14 Nov 2021 13:23:49 +0530
commit

90f2396d71393631a9f0df11815218cbd49b0071

5 files changed, 91 insertions(+), 0 deletions(-)

jump to
A .gitignore

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

+taigobot
A go.mod

@@ -0,0 +1,5 @@

+module git.icyphox.sh/taigobot + +go 1.17 + +require gopkg.in/irc.v3 v3.1.4
A go.sum

@@ -0,0 +1,13 @@

+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/irc.v3 v3.1.4 h1:DYGMRFbtseXEh+NadmMUFzMraqyuUj4I3iWYFEzDZPc= +gopkg.in/irc.v3 v3.1.4/go.mod h1:shO2gz8+PVeS+4E6GAny88Z0YVVQSxQghdrMVGQsR9s= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
A main.go

@@ -0,0 +1,46 @@

+package main + +import ( + "log" + "net" + + "git.icyphox.sh/taigobot/plugins" + "gopkg.in/irc.v3" +) + +func ircHandler(c *irc.Client, m *irc.Message) { + switch m.Command { + 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) + } + } + } +} + +func main() { + // TODO: load this from config + conn, err := net.Dial("tcp", "irc.rizon.net:6667") + if err != nil { + log.Fatal(err) + } + + config := irc.ClientConfig{ + Nick: "taigobot", + Pass: "", + User: "taigobot", + Name: "taigobot", + Handler: irc.HandlerFunc(ircHandler), + } + + client := irc.NewClient(conn, config) + err = client.Run() + if err != nil { + log.Fatal(err) + } +}
A plugins/plugins.go

@@ -0,0 +1,26 @@

+package plugins + +type Plugin interface { + Triggers() []string + Execute() error +} + +var Plugins = make(map[string]Plugin) + +func Register(p Plugin) { + for _, t := range p.Triggers() { + Plugins[t] = p + } +} + +func ProcessCommands(cmd string) error { + for trigger, plugin := range Plugins { + if cmd == trigger { + err := plugin.Execute() + if err != nil { + return err + } + } + } + return nil +}