Browse Source

Add new lore data structure

Add lorelist sorting
develop
Matthew Faltys 7 years ago
parent
commit
5df252aecd
  1. 99
      lorebot/botfunc.go
  2. 7
      lorebot/lorebot.go

99
lorebot/botfunc.go

@ -6,21 +6,21 @@ import (
"strings"
"time"
"github.com/unixvoid/glogger"
"gopkg.in/redis.v5"
)
func loreQuery(searchTerm string, redisClient *redis.Client) (string, error) {
val, err := redisClient.Get(searchTerm).Result()
func loreQuery(searchTerm string, redisClient *redis.Client) ([]string, error) {
val, err := redisClient.Sort(searchTerm, redis.Sort{Order: "ALPHA"}).Result()
if err != nil {
return "", errors.New("String not found.")
return nil, errors.New("String not found.")
} else {
return val, nil
}
}
func loreCheck(searchTerm, searchType string, redisClient *redis.Client) (string, error) {
searchString := fmt.Sprintf("%s:%s:%s", searchType, searchTerm, "content")
val, err := redisClient.Get(searchString).Result()
val, err := redisClient.HGet(fmt.Sprintf("%s:%s", searchType, searchTerm), "content").Result()
if err != nil {
return "", errors.New("String not found.")
} else {
@ -34,26 +34,26 @@ func loreNewString(nick, newString, newType string, redisClient *redis.Client) e
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)
// create a timestamp to use
cdate := t.Format("2006-01-02 15:04:05")
// set content
attribute = "content"
setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute)
redisClient.Set(setstring, content, 0)
// create the redis hash
_, err := redisClient.HMSet(fmt.Sprintf("%s:%s", newType, command), map[string]string{
"content": string(content),
"cdate": cdate,
"owner": nick,
}).Result()
if err != nil {
glogger.Error.Println("error setting redis hash")
return errors.New("error setting redis hash")
}
// append to index
appendIndex := fmt.Sprintf("index:%s:added", newType)
appendString := fmt.Sprintf(" %s", command)
redisClient.Append(appendIndex, appendString)
// add lore to index
err = redisClient.SAdd(fmt.Sprintf("index:%s:added", newType), command).Err()
if err != nil {
glogger.Error.Println("error updating redis index")
return errors.New("error updating redis index")
}
return nil
}
@ -61,43 +61,34 @@ func loreNewString(nick, newString, newType string, redisClient *redis.Client) e
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)
// remove redis hash containing lore and metadata
redisClient.Del(fmt.Sprintf("%s:%s", rmType, rmString))
// create a timestamp to use
rmdate := t.Format("2006-01-02 15:04:05")
// create the redis hash
_, err := redisClient.HMSet(fmt.Sprintf("%s:%s", rmType, rmString), map[string]string{
"rmdate": rmdate,
"rmby": nick,
}).Result()
if err != nil {
glogger.Error.Println("error setting redis hash")
return errors.New("error setting redis hash")
}
// add lore to index
redisClient.SAdd(fmt.Sprintf("index:%s:removed", rmType))
// remove lore form added index
redisClient.SRem(fmt.Sprintf("index:%s:added", rmType))
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.HGet(fmt.Sprintf("%s:%s", queryType, queryAttr), queryString).Result()
val, err := redisClient.Get(searchString).Result()
if err != nil {
return "", errors.New("String not found.")
} else {

7
lorebot/lorebot.go

@ -275,8 +275,7 @@ func listhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEve
glogger.Error.Println(err)
println(err)
}
fCommand := strings.Replace(command, " ", "\n", -1)
fCommand = strings.Replace(fCommand, "\n", "", 1)
fCommand := strings.Join(command, "\n")
// initialize buffer
bodyBuf := &bytes.Buffer{}
@ -314,8 +313,8 @@ func listhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEve
func lorestatus(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
command, _ := loreQuery("index:lore:added", redisClient)
loreammount := strings.Count(command, " ")
status := fmt.Sprintf("the lore db has %d entries", loreammount-1)
loreammount := len(command)
status := fmt.Sprintf("the lore db has %d entries", loreammount)
rtm.SendMessage(rtm.NewOutgoingMessage(status, ev.Channel))
}

Loading…
Cancel
Save