From 00f781caff114374035b4996eb2ff166a2c9db0a Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Wed, 6 Sep 2017 21:59:37 -0500 Subject: [PATCH] Add rmlore command --- lorebot/lorebot.go | 55 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lorebot/lorebot.go b/lorebot/lorebot.go index 472dd22..d161e8e 100644 --- a/lorebot/lorebot.go +++ b/lorebot/lorebot.go @@ -158,7 +158,7 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient * } newhandler(rtm, ev, content, "lore", redisClient) case "rmlore": - rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) + rmhandler(rtm, ev, content, "lore", redisClient) case "rek": s := strings.SplitN(string(ev.Text), " ", 2) rekhandler(rtm, ev, s[1], redisClient) @@ -313,6 +313,42 @@ func loreNewString(nick, newString, newType string, redisClient *redis.Client) e return nil } +func loreRmString(nick, rmString, rmType string, redisClient *redis.Client) error { + t := time.Now() + + // remove content + keyString := fmt.Sprintf("%s:%s:%s", rmType, rmString, "content") + redisClient.Del(keyString) + //redisClient.Set(keyString, "--LORE HAS BEEN REMOVED--", 0) + + // set "removed by" + attribute := "rmby" + setstring := fmt.Sprintf("%s:%s:%s", rmType, rmString, attribute) + redisClient.Set(setstring, nick, 0) + + // set removed date + attribute = "rmdate" + date := t.Format("2006-01-02 15:04:05") + setstring = fmt.Sprintf("%s:%s:%s", rmType, rmString, attribute) + redisClient.Set(setstring, date, 0) + + // append to deleted index + appendIndex := fmt.Sprintf("index:%s:removed", rmType) + appendString := fmt.Sprintf(" %s", rmString) + redisClient.Append(appendIndex, appendString) + + // remove from added index + queryString := fmt.Sprintf("index:%s:added", rmType) + tmpIndex, _ := loreQuery(queryString, redisClient) + // add a space to the beginning of string to replace + formattedString := fmt.Sprintf(" %s ", rmString) + // find and replace + newIndex := strings.Replace(tmpIndex, formattedString, " ", -1) + redisClient.Set(queryString, newIndex, 0) + + return nil +} + func loreAttrQuery(queryString, queryType, queryAttr string, redisClient *redis.Client) (string, error) { searchString := fmt.Sprintf("%s:%s:%s", queryType, queryAttr, queryString) @@ -394,3 +430,20 @@ func newhandler(rtm *slack.RTM, ev *slack.MessageEvent, message, newType string, rtm.SendMessage(rtm.NewOutgoingMessage("entry could not be created", 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) + if err != nil { + fmt.Printf("%s\n", err) + return + } + nick := fmt.Sprintf("%s", user.Name) + + err = loreRmString(nick, message, rmType, redisClient) + if err == nil { + rtm.SendMessage(rtm.NewOutgoingMessage("entry removed", ev.Channel)) + } else { + rtm.SendMessage(rtm.NewOutgoingMessage("entry could not be removed", ev.Channel)) + } +}