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.

98 lines
2.5 KiB

package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/unixvoid/glogger"
"gopkg.in/redis.v5"
)
func loreQuery(searchTerm string, redisClient *redis.Client) ([]string, error) {
val, err := redisClient.Sort(searchTerm, redis.Sort{Order: "ALPHA"}).Result()
if err != nil {
return nil, errors.New("String not found.")
} else {
return val, nil
}
}
func loreCheck(searchTerm, searchType string, redisClient *redis.Client) (string, error) {
val, err := redisClient.HGet(fmt.Sprintf("%s:%s", searchType, searchTerm), "content").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()
// create a timestamp to use
cdate := t.Format("2006-01-02 15:04:05")
// 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")
}
// 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
}
func loreRmString(nick, rmString, rmType string, redisClient *redis.Client) error {
t := time.Now()
// 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) {
val, err := redisClient.HGet(fmt.Sprintf("%s:%s", queryType, queryAttr), queryString).Result()
if err != nil {
return "", errors.New("String not found.")
} else {
return val, nil
}
}