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

7
lorebot/lorebot.go

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

Loading…
Cancel
Save