From 660b809679701ca96850213c4e91063c5be743e0 Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Wed, 6 Sep 2017 20:28:08 -0500 Subject: [PATCH] Fix nil message bug Add rek command --- lorebot/lorebot.go | 52 +++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/lorebot/lorebot.go b/lorebot/lorebot.go index 35be89c..c2fc109 100644 --- a/lorebot/lorebot.go +++ b/lorebot/lorebot.go @@ -62,36 +62,24 @@ Loop: select { case msg := <-rtm.IncomingEvents: switch ev := msg.Data.(type) { - //case *slack.ConnectedEvent: - // //botId = ev.Info.User.ID - //case *slack.TeamJoinEvent: - // // Handle new user to client case *slack.MessageEvent: if ev.Type == "message" { - //if ev.Text == "test" { - // rtm.SendMessage(rtm.NewOutgoingMessage("MY NAME IS ..JHEFFFFF", ev.Channel)) - //} - //if strings.Contains(ev.Text, ".lore") { - // //rtm.SendMessage(rtm.NewOutgoingMessage("THA LORE", ev.Channel)) - // s := strings.SplitN(string(ev.Text), " ", 2) - // //command, content := s[0], s[1] - // lorehandler(redisClient, rtm, ev, s[1]) - //} - //if strings.Contains(ev.Text, ".help") { - // helpmsg(rtm, ev) - //} - //if strings.Contains(ev.Text, ".help2") { - // helpmsg(rtm, ev) - //} + glogger.Debug.Printf("processing: %s", ev.Text) // see if the message begins with '.', it could be a command - comm := fmt.Sprintf("%c", ev.Text[0]) - if comm == "." { - if strings.Count(ev.Text, " ") == 0 { - // no spaces, its a static command - staticCommandHandler(rtm, ev, redisClient) - } else { - // spaces, dynamic handler - dynamicCommandHandler(rtm, ev, redisClient) + if len(ev.Text) == 0 { + // nil message, dont print + //glogger.Debug.Println("breaking on blank message") + break + } else { + comm := fmt.Sprintf("%c", ev.Text[0]) + if comm == "." { + if strings.Count(ev.Text, " ") == 0 { + // no spaces, its a static command + staticCommandHandler(rtm, ev, redisClient) + } else { + // spaces, dynamic handler + dynamicCommandHandler(rtm, ev, redisClient) + } } } } @@ -101,6 +89,7 @@ Loop: fmt.Printf("Invalid credentials") break Loop default: + // ignore all other events //fmt.Printf("Unknown error") //fmt.Printf("%v\n", msg.Data) } @@ -162,7 +151,8 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient * case "rmlore": rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) case "rek": - rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) + s := strings.SplitN(string(ev.Text), " ", 2) + rekhandler(redisClient, rtm, ev, s[1]) case "created": rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) case "owner": @@ -236,6 +226,12 @@ func lorehandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEve } } +func rekhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEvent, target string) { + rektBit, _ := redisClient.SRandMember("index:rekt").Result() + rekFmt := fmt.Sprintf("%s %s", target, rektBit) + rtm.SendMessage(rtm.NewOutgoingMessage(rekFmt, ev.Channel)) +} + func checkLore(searchTerm string, redisClient *redis.Client) (string, error) { val, err := redisClient.Get(fmt.Sprintf("lore:%s:content", searchTerm)).Result() if err != nil {