Browse Source

Break out lorebot into seperate files

Pull auth out of config for portability
master
Matthew Faltys 7 years ago
parent
commit
cb39de0983
  1. 1
      .gitignore
  2. 4
      Makefile
  3. 2
      config.gcfg
  4. 106
      lorebot/botfunc.go
  5. 109
      lorebot/lorebot.go

1
.gitignore vendored

@ -1,3 +1,4 @@
*.swp
bin/
stage.tmp/
.auth

4
Makefile

@ -9,7 +9,9 @@ lorebot:
$(GOC) lorebot/lorebot.go
run:
go run lorebot/lorebot.go
go run \
lorebot/lorebot.go \
lorebot/botfunc.go
stat:
mkdir -p bin/

2
config.gcfg

@ -1,7 +1,7 @@
[lorebot]
loglevel = "debug"
bootstrapdelay = 1
APIToken = "xoxb-189788161987-7FK5NKnBtrFGGtv4yszr2URb"
AuthFile = ".auth"
[redis]
host = "127.0.0.1:6379"

106
lorebot/botfunc.go

@ -0,0 +1,106 @@
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
}
}

109
lorebot/lorebot.go

@ -2,7 +2,6 @@ package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
@ -23,7 +22,7 @@ type Config struct {
Lorebot struct {
Loglevel string
BootstrapDelay time.Duration
APIToken string
AuthFile string
}
Redis struct {
Host string
@ -56,7 +55,12 @@ func main() {
glogger.Debug.Println("connection to redis succeeded.")
glogger.Info.Println("link to redis on", config.Redis.Host)
api := slack.New(config.Lorebot.APIToken)
// read in creds
credentials, _ := ioutil.ReadFile(config.Lorebot.AuthFile)
lines := strings.Split(string(credentials), "\n")
auth := lines[0]
api := slack.New(auth)
//api.SetDebug(true)
rtm := api.NewRTM()
@ -262,105 +266,6 @@ func gnuhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string) {
rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel))
}
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
}
return "", 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
}
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 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
}
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)

Loading…
Cancel
Save