bot: change serve() to return an error

slack: don't fatal on EOF, reconnect
irc: don't fatal, just return error
This commit is contained in:
cws 2017-09-07 00:32:53 -04:00
parent d2fdb01f08
commit e56604f507
4 changed files with 12 additions and 8 deletions

View File

@ -34,7 +34,7 @@ type Connector interface {
SendAction(channel, message string) SendAction(channel, message string)
React(string, string, msg.Message) React(string, string, msg.Message)
GetEmojiList() map[string]string GetEmojiList() map[string]string
Serve() Serve() error
Who(string) []string Who(string) []string
} }

View File

@ -3,6 +3,7 @@
package irc package irc
import ( import (
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -107,9 +108,9 @@ func (i *Irc) GetEmojiList() map[string]string {
return make(map[string]string) return make(map[string]string)
} }
func (i *Irc) Serve() { func (i *Irc) Serve() error {
if i.eventReceived == nil || i.messageReceived == nil { if i.eventReceived == nil || i.messageReceived == nil {
log.Fatal("Missing an event handler") return fmt.Errorf("Missing an event handler")
} }
var err error var err error
@ -121,7 +122,7 @@ func (i *Irc) Serve() {
true, true,
) )
if err != nil { if err != nil {
log.Fatal(err) return fmt.Errorf("%s", err)
} }
for _, c := range i.config.Channels { for _, c := range i.config.Channels {
@ -131,6 +132,7 @@ func (i *Irc) Serve() {
i.quit = make(chan bool) i.quit = make(chan bool)
go i.handleConnection() go i.handleConnection()
<-i.quit <-i.quit
return nil
} }
func (i *Irc) handleConnection() { func (i *Irc) handleConnection() {

View File

@ -68,5 +68,8 @@ func main() {
// catches anything left, will always return true // catches anything left, will always return true
b.AddHandler("factoid", fact.New(b)) b.AddHandler("factoid", fact.New(b))
client.Serve() for {
err := client.Serve()
log.Println(err)
}
} }

View File

@ -261,7 +261,7 @@ func slackTStoTime(t string) time.Time {
return time.Unix(sec, nsec) return time.Unix(sec, nsec)
} }
func (s *Slack) Serve() { func (s *Slack) Serve() error {
s.connect() s.connect()
s.populateEmojiList() s.populateEmojiList()
for { for {
@ -269,8 +269,7 @@ func (s *Slack) Serve() {
if err != nil && err == io.EOF { if err != nil && err == io.EOF {
log.Fatalf("Slack API EOF") log.Fatalf("Slack API EOF")
} else if err != nil { } else if err != nil {
log.Printf("Slack API error: %s", err) return fmt.Errorf("Slack API error: %s", err)
continue
} }
switch msg.Type { switch msg.Type {
case "message": case "message":