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)
React(string, string, msg.Message)
GetEmojiList() map[string]string
Serve()
Serve() error
Who(string) []string
}

View File

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

View File

@ -68,5 +68,8 @@ func main() {
// catches anything left, will always return true
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)
}
func (s *Slack) Serve() {
func (s *Slack) Serve() error {
s.connect()
s.populateEmojiList()
for {
@ -269,8 +269,7 @@ func (s *Slack) Serve() {
if err != nil && err == io.EOF {
log.Fatalf("Slack API EOF")
} else if err != nil {
log.Printf("Slack API error: %s", err)
continue
return fmt.Errorf("Slack API error: %s", err)
}
switch msg.Type {
case "message":