diff options
author | alex | 2018-10-21 12:57:23 +0100 |
---|---|---|
committer | alex | 2018-10-21 13:04:35 +0100 |
commit | 61b84d9fd997d61f4e5bb6adc63a2310036a242e (patch) | |
tree | e15d066c7ac3ea61785018b37da8dcaae26c03e5 /wat/bot.go | |
parent | 55bc749f896c25adb57125fd168a1e3ccfc0cd48 (diff) | |
download | watbot-61b84d9fd997d61f4e5bb6adc63a2310036a242e.tar.gz watbot-61b84d9fd997d61f4e5bb6adc63a2310036a242e.tar.bz2 watbot-61b84d9fd997d61f4e5bb6adc63a2310036a242e.zip |
Reimplemented python bot in golang, skipped a few features
Diffstat (limited to 'wat/bot.go')
-rw-r--r-- | wat/bot.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/wat/bot.go b/wat/bot.go new file mode 100644 index 0000000..170f77e --- /dev/null +++ b/wat/bot.go @@ -0,0 +1,90 @@ +package wat + +import ( + "github.com/go-irc/irc" + "crypto/tls" + "fmt" + "strings" +) + +type WatBot struct { + client *irc.Client + conn *tls.Conn + game *WatGame + Db *WatDb + Nick string +} + +func NewWatBot(config *irc.ClientConfig, serverConn *tls.Conn) *WatBot { + wat := WatBot{conn:serverConn, Nick:config.Nick} + wat.Db = NewWatDb() + wat.game = NewWatGame(&wat, wat.Db) + config.Handler = irc.HandlerFunc(wat.HandleIrcMsg) + wat.client = irc.NewClient(wat.conn, *config) + return &wat +} + +func CleanNick(nick string) string { + return string(nick[0])+"\u200c"+nick[1:] +} + +func (w *WatBot) HandleIrcMsg(c *irc.Client, m *irc.Message) { + fmt.Println(m) + switch cmd := m.Command; cmd { + case "PING": + w.write("PONG", m.Params[0]) + case "PRIVMSG": + w.Msg(m) + } +} + +func (w *WatBot) Msg(m *irc.Message) { + if m.Params[0] == w.Nick && m.Prefix.Host == "tripsit/operator/hibs" { + if "join" == m.Params[1] { + w.write("JOIN", "##wat") + } + } + if strings.Contains(m.Prefix.Host, "tripsit") && m.Params[0] == "##wat" { + args := strings.FieldsFunc(m.Params[1], func(c rune) bool {return c == ' '}) + if len(args) < 1 && args[0] != "wat" && args[0][0] != '#' { + return + } + if args[0][0] == '#' { + args[0] = args[0][1:] + } + user := strings.ToLower(m.Prefix.Name) + player := w.Db.User(user, m.Prefix.Host, true) + w.game.Msg(m, &player, args) + } +} + +func (w *WatBot) Run() { + defer w.conn.Close() + err := w.client.Run() + if err != nil { + fmt.Println(err.Error()) + } +} + +func (w *WatBot) say(dest, msg string) { + if len(msg) == 0 { + return + } + fmt.Printf("MSG %s: %s\n", dest, msg) + w.write("PRIVMSG", dest, msg) +} + +func (w *WatBot) reply(s *irc.Message, r string) { + sender := s.Params[0] + if sender == w.Nick { + sender = s.Prefix.Name + } + w.say(sender, r) +} + +func (w *WatBot) write(cmd string, params ...string) { + w.client.WriteMessage(&irc.Message{ + Command: cmd, + Params: params, + }) +} |