Browse Source

Add newlore command

master
Matthew Faltys 7 years ago
parent
commit
8e21b83251
  1. 86
      lorebot/lorebot.go

86
lorebot/lorebot.go

@ -136,6 +136,16 @@ func initRedisConnection() (*redis.Client, error) {
}
func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
//nick := ev.User
//fmt.Printf("%s", ev.User)
user, err := rtm.GetUserInfo(ev.User)
if err != nil {
fmt.Printf("%s\n", err)
return
}
println(user.Name)
nick := fmt.Sprintf("%s", user.Name)
message := ev.Text
if strings.Count(message, " ") < 1 {
glogger.Debug.Println("not proper syntax")
@ -151,8 +161,12 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *
s := strings.SplitN(string(ev.Text), " ", 2)
contenthandler(rtm, ev, s[1], "lore", redisClient)
case "newlore":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
println(content)
if strings.Contains(content, " ") {
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("not proper syntax", ev.Channel))
return
}
newhandler(rtm, ev, nick, content, "lore", redisClient)
case "rmlore":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
case "rek":
@ -162,13 +176,13 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *
s := strings.SplitN(string(ev.Text), " ", 2)
gnuhandler(rtm, ev, s[1])
case "created":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
attrhandler(rtm, ev, "lore", content, "cdate", redisClient)
case "owner":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
attrhandler(rtm, ev, "lore", content, "owner", redisClient)
case "removedby":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
attrhandler(rtm, ev, "lore", content, "rmby", redisClient)
case "removed":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
attrhandler(rtm, ev, "lore", content, "rmdate", redisClient)
}
}
@ -238,6 +252,15 @@ func contenthandler(rtm *slack.RTM, ev *slack.MessageEvent, message, queryType s
}
}
func attrhandler(rtm *slack.RTM, ev *slack.MessageEvent, queryType, queryAttr, message string, redisClient *redis.Client) {
query, err := loreAttrQuery(message, queryType, queryAttr, redisClient)
if err == nil {
rtm.SendMessage(rtm.NewOutgoingMessage(query, ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("attribute does not exist", ev.Channel))
}
}
func rekhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string, redisClient *redis.Client) {
rektBit, _ := redisClient.SRandMember("index:rekt").Result()
rekFmt := fmt.Sprintf("%s %s", target, rektBit)
@ -270,6 +293,48 @@ func loreCheck(searchTerm, searchType string, redisClient *redis.Client) (string
return "", nil
}
func loreNewString(nick, newString, newType string, redisClient *redis.Client) error {
s := strings.SplitN(newString, " ", 2)
command, content := s[0], []byte(s[1])
t := time.Now()
// set created date
attribute := "cdate"
date := t.Format("2006-01-02 15:04:05")
setstring := fmt.Sprintf("%s:%s:%s", newType, command, attribute)
redisClient.Set(setstring, date, 0)
// set owner
attribute = "owner"
setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute)
redisClient.Set(setstring, nick, 0)
// set content
attribute = "content"
setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute)
redisClient.Set(setstring, content, 0)
// append to index
appendIndex := fmt.Sprintf("index:%s:added", newType)
appendString := fmt.Sprintf(" %s", command)
redisClient.Append(appendIndex, appendString)
return nil
}
func loreAttrQuery(queryString, queryType, queryAttr string, redisClient *redis.Client) (string, error) {
searchString := fmt.Sprintf("%s:%s:%s", queryType, queryAttr, queryString)
val, err := redisClient.Get(searchString).Result()
if err != nil {
return "", errors.New("String not found.")
} else {
return val, nil
}
return "", nil
}
func listhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEvent, listType, listAttr string) {
// aka index:command:added
newType := fmt.Sprintf("%s:%s:%s", "index", listType, listAttr)
@ -322,3 +387,12 @@ func lorestatus(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Clien
status := fmt.Sprintf("the lore db has %d entries", loreammount-1)
rtm.SendMessage(rtm.NewOutgoingMessage(status, ev.Channel))
}
func newhandler(rtm *slack.RTM, ev *slack.MessageEvent, nick, message, newType string, redisClient *redis.Client) {
err := loreNewString(nick, message, newType, redisClient)
if err == nil {
rtm.SendMessage(rtm.NewOutgoingMessage("entry created", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("entry could not be created", ev.Channel))
}
}

Loading…
Cancel
Save