Browse Source

Add list function

Refactor query func
Add lorestatus command
master
Matthew Faltys 7 years ago
parent
commit
b7a697a1b3
  1. 93
      lorebot/lorebot.go

93
lorebot/lorebot.go

@ -1,9 +1,14 @@
package main package main
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"os" "os"
"strings" "strings"
"time" "time"
@ -144,7 +149,7 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *
switch trimmedPrefix { switch trimmedPrefix {
case "lore": case "lore":
s := strings.SplitN(string(ev.Text), " ", 2) s := strings.SplitN(string(ev.Text), " ", 2)
lorehandler(redisClient, rtm, ev, s[1]) contenthandler(rtm, ev, s[1], "lore", redisClient)
case "newlore": case "newlore":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
println(content) println(content)
@ -182,11 +187,15 @@ func staticCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *r
case "version": case "version":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
case "lorelist": case "lorelist":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) listhandler(redisClient, rtm, ev, "lore", "added")
case "removedlore": case "removedlore":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) listhandler(redisClient, rtm, ev, "lore", "removed")
case "commandlist":
listhandler(redisClient, rtm, ev, "command", "added")
case "removedcommands":
listhandler(redisClient, rtm, ev, "command", "removed")
case "lorestatus": case "lorestatus":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) lorestatus(rtm, ev, redisClient)
} }
} }
@ -214,12 +223,12 @@ func advancedhelpmsg(rtm *slack.RTM, ev *slack.MessageEvent) {
rtm.SendMessage(rtm.NewOutgoingMessage(help2, ev.Channel)) rtm.SendMessage(rtm.NewOutgoingMessage(help2, ev.Channel))
} }
func lorehandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEvent, rawMessage string) { func contenthandler(rtm *slack.RTM, ev *slack.MessageEvent, message, queryType string, redisClient *redis.Client) {
loreTerm := strings.Replace(rawMessage, ".lore", "", -1) loreTerm := strings.Replace(message, ".lore", "", -1)
println("searching for:", loreTerm) println("searching for:", loreTerm)
//rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("searching for '%s'\n", loreTerm), ev.Channel)) //rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("searching for '%s'\n", loreTerm), ev.Channel))
content, err := checkLore(loreTerm, redisClient) content, err := loreCheck(loreTerm, queryType, redisClient)
if err == nil { if err == nil {
// lore found, print it // lore found, print it
rtm.SendMessage(rtm.NewOutgoingMessage(content, ev.Channel)) rtm.SendMessage(rtm.NewOutgoingMessage(content, ev.Channel))
@ -240,12 +249,76 @@ func gnuhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEven
rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel)) rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel))
} }
func checkLore(searchTerm string, redisClient *redis.Client) (string, error) { func loreQuery(searchTerm string, redisClient *redis.Client) (string, error) {
val, err := redisClient.Get(fmt.Sprintf("lore:%s:content", searchTerm)).Result() val, err := redisClient.Get(searchTerm).Result()
if err != nil { if err != nil {
return "", errors.New("String not found.") return "", errors.New("String not found.")
} else { } else {
return val, nil return val, nil
} }
//return "", 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 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)
//command, err := botfunc.Query(newType, redisClient)
command, err := loreQuery(newType, redisClient)
if err != nil {
glogger.Error.Println(err)
println(err)
}
fCommand := strings.Replace(command, " ", "\n", -1)
fCommand = strings.Replace(fCommand, "\n", "", 1)
// initialize buffer
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
// create header and body. filename must be set
mh := make(textproto.MIMEHeader)
mh.Set("Content-Type", "application/octet-stream")
mh.Set("Content-Disposition", "form-data; name=\"file\"; filename=\" \"")
// write body with mime header
partWriter, _ := bodyWriter.CreatePart(mh)
io.Copy(partWriter, bytes.NewBufferString(fCommand))
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
// set content type to multipart/form-data, required for bitnuke.io
h := make(textproto.MIMEHeader)
h.Set("Content-Type", "multipart/form-data")
bodyWriter.CreatePart(h)
bodyWriter.Close()
// POST to bitnuke.io
resp, err := http.Post("https://bitnuke.io/upload", contentType, bodyBuf)
if err != nil {
println(err)
}
defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
println(err)
}
// format response to be link
fResponse := fmt.Sprintf("https://bitnuke.io/%s", resp_body[:])
// print to channel
rtm.SendMessage(rtm.NewOutgoingMessage(fResponse, ev.Channel))
}
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)
rtm.SendMessage(rtm.NewOutgoingMessage(status, ev.Channel))
} }

Loading…
Cancel
Save