Slackbot for the smpld domain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9 KiB

package main
import (
"errors"
"fmt"
"strings"
"time"
"gopkg.in/redis.v5"
)
func loreQuery(searchTerm string, redisClient *redis.Client) (string, error) {
val, err := redisClient.Get(searchTerm).Result()
if err != nil {
return "", 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()
if err != nil {
return "", errors.New("String not found.")
} else {
return val, 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 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)
val, err := redisClient.Get(searchString).Result()
if err != nil {
return "", errors.New("String not found.")
} else {
return val, nil
}
}