diff --git a/Makefile b/Makefile index 86859b2..3a38242 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,9 @@ IMAGE_NAME=smpldbot all: lorebot +depends: + go get github.com/nlopes/slack + lorebot: $(GOC) lorebot/lorebot.go diff --git a/lorebot/lorebot.go b/lorebot/lorebot.go index 5369a53..d5d9af7 100644 --- a/lorebot/lorebot.go +++ b/lorebot/lorebot.go @@ -178,6 +178,12 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient * case "rmdubs": s := strings.SplitN(string(ev.Text), " ", 2) rmdubshandler(rtm, ev, s[1], redisClient) + case "newlunch": + s := strings.SplitN(string(ev.Text), " ", 2) + newlunchhandler(rtm, ev, s[1], redisClient) + case "rmlunch": + s := strings.SplitN(string(ev.Text), " ", 2) + rmlunchhandler(rtm, ev, s[1], redisClient) case "rmlore": rmhandler(rtm, ev, content, "lore", redisClient) case "rek": @@ -215,10 +221,16 @@ func staticCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *r advancedhelpmsg(rtm, ev) case "rng": rnghandler(rtm, ev, redisClient) + case "egg": + egghandler(rtm, ev, redisClient) + case "giveup": + giveuphandler(rtm, ev, redisClient) case "depricatedrng": depricatedrnghandler(rtm, ev, redisClient) case "dubs": dubshandler(rtm, ev, redisClient) + case "lunch": + lunchhandler(rtm, ev, redisClient) case "roll": rollhandler(rtm, ev) case "rr": @@ -399,6 +411,56 @@ func rnghandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Clien rtm.SendMessage(rtm.NewOutgoingMessage(rngMsg, ev.Channel)) } +func egghandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) { + // aka index:command:added + newType := fmt.Sprintf("index:lore:added") + //command, err := botfunc.Query(newType, redisClient) + command, err := loreQuery(newType, redisClient) + if err != nil { + glogger.Error.Println(err) + println(err) + } + + // if the array is empty, exit early + if len(command) <= 0 { + return + } + + // pick a random one + rand.Seed(time.Now().UnixNano()) + rngLore := fmt.Sprint(command[rand.Intn(len(command))]) + println(rngLore) + + // get lore content + rngContent, err := loreCheck(rngLore, "lore", redisClient) + if err != nil { + fmt.Printf("%v\n", err) + rtm.SendMessage(rtm.NewOutgoingMessage("beep boop.. broken bot..", ev.Channel)) + return + } + + // set the lore in case we cant find it. + redisClient.Del("index:egg") + err = redisClient.Set("index:egg", rngLore, 0).Err() + if err != nil { + glogger.Error.Println("error updating redis egg index") + } + + rngMsg := fmt.Sprintf("begin the hunt boiiz...\n ```%s```", rngContent) + rtm.SendMessage(rtm.NewOutgoingMessage(rngMsg, ev.Channel)) +} + +func giveuphandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) { + giveup, err := redisClient.Get("index:egg").Result() + if err != nil { + glogger.Error.Println("error getting redis egg index") + glogger.Error.Println(err) + } + + giveupMsg := fmt.Sprintf("foolz.. it was `%s`", giveup) + rtm.SendMessage(rtm.NewOutgoingMessage(giveupMsg, ev.Channel)) +} + func gnuhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string) { gnuFmt := fmt.Sprintf("slaps the nonfree software out of %s's hands", target) rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel)) @@ -534,6 +596,54 @@ func dubshandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Clie rollhandler(rtm, ev) } +func newlunchhandler(rtm *slack.RTM, ev *slack.MessageEvent, newContent string, redisClient *redis.Client) { + // prep content for lunchification + // aka remove the '<' and '>' that slack adds + newContent = strings.Replace(newContent, "<", "", -1) + newContent = strings.Replace(newContent, ">", "", -1) + + err := redisClient.SAdd("index:lunch", newContent) + if err != nil { + rtm.SendMessage(rtm.NewOutgoingMessage("lunch location added", ev.Channel)) + } else { + rtm.SendMessage(rtm.NewOutgoingMessage("lunch location addition failed", ev.Channel)) + } +} + +func rmlunchhandler(rtm *slack.RTM, ev *slack.MessageEvent, rmContent string, redisClient *redis.Client) { + // prep content for lunch removal + // aka remove the '<' and '>' that slack adds + rmContent = strings.Replace(rmContent, "<", "", -1) + rmContent = strings.Replace(rmContent, ">", "", -1) + + err := redisClient.SRem("index:lunch", rmContent) + if err != nil { + rtm.SendMessage(rtm.NewOutgoingMessage("lunch location removed", ev.Channel)) + } else { + rtm.SendMessage(rtm.NewOutgoingMessage("lunch location removal failed", ev.Channel)) + } +} + +func lunchhandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) { + // aka index:command:added + newType := fmt.Sprintf("index:lunch") + + command, err := loreQuery(newType, redisClient) + if err != nil { + glogger.Error.Println(err) + } + + // if the array is empty, exit early + if len(command) <= 0 { + return + } + + // pick a random one + rand.Seed(time.Now().UnixNano()) + rngDubs := fmt.Sprint(command[rand.Intn(len(command))]) + rtm.SendMessage(rtm.NewOutgoingMessage(rngDubs, ev.Channel)) +} + func rmhandler(rtm *slack.RTM, ev *slack.MessageEvent, message, rmType string, redisClient *redis.Client) { // get nick from slack api user, err := rtm.GetUserInfo(ev.User)