Compare commits

...

36 Commits

Author SHA1 Message Date
Matthew Faltys f4cc4b99fa Add additional rng logging 6 years ago
Matthew Faltys d1b1bea366 Fix bug where lore wasn't being removed from proper indecies 6 years ago
Matthew Faltys 128c9294a1 Remove slack debug to reflect upstream slack API changes 6 years ago
Matthew Faltys 2fc6f9e66c Add function for ceelo to roll until it gets a hand 6 years ago
Matthew Faltys 1c54f2bb65 Add ceelo function 6 years ago
Matthew Faltys dafabb4c9e Add brb handlers 6 years ago
Matthew Faltys f87c243180 Add egg and lunch func 7 years ago
Matthew Faltys db7e379d8c Add dxx hanlder 7 years ago
Matthew Faltys 4f01524e98 Add error checking to not trace on empty db 7 years ago
Matthew Faltys 9bbdc048cc Fix rmlore parsing wrong slack url 7 years ago
Matthew Faltys 5684b3721c Update readme with dubs commands 7 years ago
Matthew Faltys 4f5ecbd838 Add .dubs, .newdubs, and .rmdubs 7 years ago
Matthew Faltys 66f38cc91c Update dice and d20 to be more efficient 7 years ago
Matthew Faltys de8a9a1d78 Fix typo 7 years ago
Matthew Faltys be08b9f185 Add readme file 7 years ago
Matthew Faltys ac99f6d0ba Add russian roulette rng roll 7 years ago
Matthew Faltys 506cc252d8 Add dice roll and 8 digit roll 7 years ago
Matthew Faltys e3ef92394f Add seed to rng 7 years ago
Matthew Faltys 9c767cb019 Update rng function 7 years ago
Matthew Faltys 36448b4667 Add betarng function 7 years ago
Matthew Faltys c083139598 Add summon function 7 years ago
Matthew Faltys 3f529521c4 Add RIP command 7 years ago
Matthew Faltys 7d624ef232 Remove unused command from switch 7 years ago
Matthew Faltys 8a4150c552 Add DMX easteregg 7 years ago
Matthew Faltys de1d6731ad Fix debugging method 7 years ago
Matthew Faltys 0b619605cd Add rng function 7 years ago
Matthew Faltys 7dbfbaba0a Update redis to be statically compiled 7 years ago
Matthew Faltys 9263e72a6b Update redis server 7 years ago
Matthew Faltys 6cdd33c23d Add docker build from scratch 7 years ago
Matthew Faltys 601add3340 Add aci build to makefile 7 years ago
Matthew Faltys 9910ae985d Fix redis daemon 7 years ago
Matthew Faltys a300e80f7e Re add command sanitation 7 years ago
Matthew Faltys 107b219219 Fix links from slack 7 years ago
Matthew Faltys c9ad4723e1 Remove unneded comments 7 years ago
Matthew Faltys c43acfc250 Add unwanted character parsing 7 years ago
Matthew Faltys 5df252aecd Add new lore data structure 7 years ago
  1. 3
      .gitignore
  2. 40
      Makefile
  3. 1
      config.gcfg
  4. 8
      deps/Dockerfile
  5. 11
      deps/lorebot.service
  6. 44
      deps/manifest.json
  7. 4
      deps/redis.conf
  8. BIN
      deps/rootfs.tar.gz
  9. 7
      deps/run.sh
  10. 113
      lorebot/botfunc.go
  11. 410
      lorebot/lorebot.go
  12. 72
      readme.md

3
.gitignore vendored

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

40
Makefile

@ -5,6 +5,9 @@ IMAGE_NAME=smpldbot
all: lorebot
depends:
go get github.com/nlopes/slack
lorebot:
$(GOC) lorebot/lorebot.go
@ -22,6 +25,7 @@ docker: stat
cp bin/lorebot stage.tmp/
cp deps/rootfs.tar.gz stage.tmp/
cp deps/Dockerfile stage.tmp/
cp deps/redis.conf stage.tmp/
chmod +x deps/run.sh
cp deps/run.sh stage.tmp/
cp config.gcfg stage.tmp/
@ -29,6 +33,42 @@ docker: stat
sudo docker build -t $(IMAGE_NAME) .
@echo "$(IMAGE_NAME) built"
aci: stat
mkdir -p stage.tmp/lorebot-layout/rootfs/
tar -zxf deps/rootfs.tar.gz -C stage.tmp/lorebot-layout/rootfs/
cp bin/lorebot stage.tmp/lorebot-layout/rootfs/lorebot
chmod +x deps/run.sh
cp deps/run.sh stage.tmp/lorebot-layout/rootfs/
cp deps/redis.conf stage.tmp/lorebot-layout/rootfs/
cp config.gcfg stage.tmp/lorebot-layout/rootfs/
cp deps/manifest.json stage.tmp/lorebot-layout/manifest
cd stage.tmp/ && \
actool build lorebot-layout lorebot.aci && \
mv lorebot.aci ../
@echo "lorebot.aci built"
test_docker:
mkdir -p /tmp/lorebot/redis/
touch /tmp/lorebot/dump.rdb
cp .auth /tmp/lorebot/
sudo docker run \
-it \
-v /tmp/lorebot/redis:/redisbackup:rw \
-v /tmp/lorebot/.auth:/.auth:ro \
smpldbot
test_aci:
mkdir -p /tmp/lorebot/redis/
touch /tmp/lorebot/dump.rdb
cp .auth /tmp/lorebot/
sudo rkt run \
--volume redis,kind=host,source=/tmp/lorebot/redis/ \
--volume auth,kind=host,source=/tmp/lorebot/.auth \
--net=host \
--insecure-options=image \
./lorebot.aci
clean:
rm -rf bin/
rm -rf stage.tmp/
rm -rf lorebot.aci

1
config.gcfg

@ -1,5 +1,6 @@
[lorebot]
loglevel = "debug"
slackdebug = false
bootstrapdelay = 1
AuthFile = ".auth"

8
deps/Dockerfile vendored

@ -1,7 +1,9 @@
FROM alpine
FROM scratch
RUN apk --update add ca-certificates redis
RUN mkdir /redisbackup/
#RUN apk --update add ca-certificates redis
#RUN mkdir /redisbackup/
ADD rootfs.tar.gz /
COPY redis.conf /
COPY config.gcfg /
COPY lorebot /
COPY run.sh /

11
deps/lorebot.service vendored

@ -0,0 +1,11 @@
[Unit]
Description=automod of all lorez
[Service]
ExecStart=/usr/bin/rkt run \
--net=host \
--volume redis,kind=host,source=/tmp/dump.rdb \
unixvoid.com/smpldbot
[Install]
WantedBy=multi-user.target

44
deps/manifest.json vendored

@ -0,0 +1,44 @@
{
"acKind": "ImageManifest",
"acVersion": "0.7.1",
"name": "unixvoid.com/lorebot",
"labels": [
{
"name": "version",
"value": "latest"
},
{
"name": "arch",
"value": "amd64"
},
{
"name": "os",
"value": "linux"
}
],
"app": {
"user": "root",
"group": "root",
"exec": [
"/run.sh"
],
"mountPoints": [
{
"name": "redis",
"path": "/redisbackup/",
"readOnly": false
},
{
"name": "auth",
"path": "/.auth",
"readOnly": false
}
]
},
"annotations": [
{
"name": "authors",
"value": "Matthew Faltys <mfaltys@gmail.com>"
}
]
}

4
deps/redis.conf vendored

@ -0,0 +1,4 @@
daemonize yes
dbfilename dump.rdb
dir /redisbackup/
save 30 1

BIN
deps/rootfs.tar.gz vendored

Binary file not shown.

7
deps/run.sh vendored

@ -1,10 +1,5 @@
#!/bin/sh
#echo "daemonize yes" > /redis.conf
echo "dbfilename dump.rdb" >> /redis.conf
echo "dir /redisbackup/" >> /redis.conf
echo "save 30 1" >> /redis.conf
redis-server /redis.conf &
redis-server /redis.conf
/lorebot $@

113
lorebot/botfunc.go

@ -6,21 +6,21 @@ import (
"strings"
"time"
"github.com/unixvoid/glogger"
"gopkg.in/redis.v5"
)
func loreQuery(searchTerm string, redisClient *redis.Client) (string, error) {
val, err := redisClient.Get(searchTerm).Result()
func loreQuery(searchTerm string, redisClient *redis.Client) ([]string, error) {
val, err := redisClient.Sort(searchTerm, redis.Sort{Order: "ALPHA"}).Result()
if err != nil {
return "", errors.New("String not found.")
return nil, 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()
val, err := redisClient.HGet(fmt.Sprintf("%s:%s", searchType, searchTerm), "content").Result()
if err != nil {
return "", errors.New("String not found.")
} else {
@ -31,29 +31,37 @@ func loreCheck(searchTerm, searchType string, redisClient *redis.Client) (string
func loreNewString(nick, newString, newType string, redisClient *redis.Client) error {
s := strings.SplitN(newString, " ", 2)
command, content := s[0], []byte(s[1])
stripContent := strings.Replace(fmt.Sprintf("%s", content), "<", "", -1)
stripLinks := strings.Replace(stripContent, ">", "", -1)
t := time.Now()
// dont allow certain chars
if strings.ContainsAny(command, ";:,.'\"!$%^*()/\\") {
// reject user input, it has unwanted chars
return errors.New("error: entry contains unwanted characters")
}
// 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)
t := time.Now()
// set owner
attribute = "owner"
setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute)
redisClient.Set(setstring, nick, 0)
// create a timestamp to use
cdate := t.Format("2006-01-02 15:04:05")
// set content
attribute = "content"
setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute)
redisClient.Set(setstring, content, 0)
// create the redis hash
_, err := redisClient.HMSet(fmt.Sprintf("%s:%s", newType, command), map[string]string{
"content": stripLinks,
"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
appendIndex := fmt.Sprintf("index:%s:added", newType)
appendString := fmt.Sprintf(" %s", command)
redisClient.Append(appendIndex, appendString)
// 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
}
@ -61,43 +69,40 @@ func loreNewString(nick, newString, newType string, redisClient *redis.Client) e
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)
// 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), rmString).Err()
if err != nil {
glogger.Error.Printf("error removing lore from 'index:%s:removed'\n", rmType)
}
// remove lore form added index
redisClient.SRem(fmt.Sprintf("index:%s:added", rmType), rmString).Err()
if err != nil {
glogger.Error.Printf("error removing lore from 'index:%s:added'\n", rmType)
}
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.HGet(fmt.Sprintf("%s:%s", queryType, queryAttr), queryString).Result()
val, err := redisClient.Get(searchString).Result()
if err != nil {
return "", errors.New("String not found.")
} else {

410
lorebot/lorebot.go

@ -5,10 +5,12 @@ import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"strconv"
"strings"
"time"
@ -21,6 +23,7 @@ import (
type Config struct {
Lorebot struct {
Loglevel string
SlackDebug bool
BootstrapDelay time.Duration
AuthFile string
}
@ -61,7 +64,6 @@ func main() {
auth := lines[0]
api := slack.New(auth)
//api.SetDebug(true)
rtm := api.NewRTM()
go rtm.ManageConnection()
@ -156,16 +158,40 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *
contenthandler(rtm, ev, s[1], "lore", redisClient)
case "newlore":
if strings.Contains(content, " ") {
newhandler(rtm, ev, content, "lore", redisClient)
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("not proper syntax", ev.Channel))
return
}
newhandler(rtm, ev, content, "lore", redisClient)
case "newdubs":
s := strings.SplitN(string(ev.Text), " ", 2)
newdubshandler(rtm, ev, s[1], redisClient)
case "rmdubs":
s := strings.SplitN(string(ev.Text), " ", 2)
rmdubshandler(rtm, ev, s[1], redisClient)
case "newlunch":
s := strings.SplitN(string(ev.Text), " ", 2)
newlunchhandler(rtm, ev, s[1], redisClient)
case "rmlunch":
s := strings.SplitN(string(ev.Text), " ", 2)
rmlunchhandler(rtm, ev, s[1], redisClient)
case "newbrb":
s := strings.SplitN(string(ev.Text), " ", 2)
newbrbhandler(rtm, ev, s[1], redisClient)
case "rmbrb":
s := strings.SplitN(string(ev.Text), " ", 2)
rmbrbhandler(rtm, ev, s[1], redisClient)
case "rmlore":
rmhandler(rtm, ev, content, "lore", redisClient)
case "rek":
s := strings.SplitN(string(ev.Text), " ", 2)
rekhandler(rtm, ev, s[1], redisClient)
case "rip":
s := strings.SplitN(string(ev.Text), " ", 2)
riphandler(rtm, ev, s[1])
case "summon":
s := strings.SplitN(string(ev.Text), " ", 2)
summonhandler(rtm, ev, s[1])
case "gnu":
s := strings.SplitN(string(ev.Text), " ", 2)
gnuhandler(rtm, ev, s[1])
@ -191,9 +217,29 @@ func staticCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *r
case "help2":
advancedhelpmsg(rtm, ev)
case "rng":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
case "version":
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel))
rnghandler(rtm, ev, redisClient)
case "egg":
egghandler(rtm, ev, redisClient)
case "giveup":
giveuphandler(rtm, ev, redisClient)
case "depricatedrng":
depricatedrnghandler(rtm, ev, redisClient)
case "dubs":
dubshandler(rtm, ev, redisClient)
case "lunch":
lunchhandler(rtm, ev, redisClient)
case "brb":
brbhandler(rtm, ev, redisClient)
case "roll":
rollhandler(rtm, ev)
case "rr":
rrhandler(rtm, ev)
case "dice":
dicerollhandler(rtm, ev)
case "ceelo":
ceelorollhandler(rtm, ev)
case "listen":
rtm.SendMessage(rtm.NewOutgoingMessage("*it's what you hearin*", ev.Channel))
case "lorelist":
listhandler(redisClient, rtm, ev, "lore", "added")
case "removedlore":
@ -205,6 +251,20 @@ func staticCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *r
case "lorestatus":
lorestatus(rtm, ev, redisClient)
}
// get that dice handler
if len(ev.Text) >= 2 {
if strings.Contains(ev.Text, ".d") {
trm := strings.Replace(ev.Text, ".d", "", -1)
// check if the rest of the string is an int
num, err := strconv.Atoi(trm)
if err == nil {
//glogger.Debug.Printf("rolling for %d", num)
dhandler(rtm, ev, num)
}
}
}
}
func helpmsg(rtm *slack.RTM, ev *slack.MessageEvent) {
@ -233,8 +293,7 @@ func advancedhelpmsg(rtm *slack.RTM, ev *slack.MessageEvent) {
func contenthandler(rtm *slack.RTM, ev *slack.MessageEvent, message, queryType string, redisClient *redis.Client) {
loreTerm := strings.Replace(message, ".lore", "", -1)
println("searching for:", loreTerm)
//rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("searching for '%s'\n", loreTerm), ev.Channel))
glogger.Debug.Printf("searching for: %s\n", loreTerm)
content, err := loreCheck(loreTerm, queryType, redisClient)
if err == nil {
@ -261,6 +320,179 @@ func rekhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string, redisClie
rtm.SendMessage(rtm.NewOutgoingMessage(rekFmt, ev.Channel))
}
func summonhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string) {
sumFmt := fmt.Sprintf("つ◕_◕)つ %s つ◕_◕)つ", target)
rtm.SendMessage(rtm.NewOutgoingMessage(sumFmt, ev.Channel))
}
func riphandler(rtm *slack.RTM, ev *slack.MessageEvent, target string) {
// create a timestamp to use
t := time.Now()
tstamp := t.Format("01-02-2006")
ripFmt := fmt.Sprintf("here lies %s, disrespected to death %s..", target, tstamp)
rtm.SendMessage(rtm.NewOutgoingMessage(ripFmt, ev.Channel))
}
func rollhandler(rtm *slack.RTM, ev *slack.MessageEvent) {
//fmt.Print(rand.Intn(100))
rndStr := fmt.Sprintf("%08d", rand.Int63n(1e8))
rtm.SendMessage(rtm.NewOutgoingMessage(rndStr, ev.Channel))
}
func dicerollhandler(rtm *slack.RTM, ev *slack.MessageEvent) {
//fmt.Print(rand.Intn(100))
rand.Seed(time.Now().UnixNano())
rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("%d", rand.Intn(6)+1), ev.Channel))
}
func ceelorollhandler(rtm *slack.RTM, ev *slack.MessageEvent) {
rand.Seed(time.Now().UnixNano())
// set condition
hand := false
for !hand {
roll0 := rand.Intn(6) + 1
roll1 := rand.Intn(6) + 1
roll2 := rand.Intn(6) + 1
rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("%d, %d, %d", roll0, roll1, roll2), ev.Channel))
// check for pair
if (roll0 == roll1) || (roll0 == roll2) || (roll1 == roll2) {
hand = true
}
a := fmt.Sprintf("%d%d%d", roll0, roll1, roll2)
// check for instant win
if strings.Contains(a, "4") && strings.Contains(a, "5") && strings.Contains(a, "6") {
hand = true
}
// check for instant loss
if strings.Contains(a, "1") && strings.Contains(a, "2") && strings.Contains(a, "3") {
hand = true
}
time.Sleep(1000 * time.Millisecond)
}
}
func rrhandler(rtm *slack.RTM, ev *slack.MessageEvent) {
//fmt.Print(rand.Intn(100))
dice := []string{"*click*", "*BANG*", "*click*", "*click*", "*click*", "*click*"}
rand.Seed(time.Now().UnixNano())
rndStr := dice[rand.Intn(len(dice)-1)]
rtm.SendMessage(rtm.NewOutgoingMessage(rndStr, ev.Channel))
}
func dhandler(rtm *slack.RTM, ev *slack.MessageEvent, num int) {
if (num > 0) && (num < 99999999) {
rand.Seed(time.Now().UnixNano())
rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("%d", rand.Intn(num)+1), ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("na fam, we need a different number. stay lit tho", ev.Channel))
}
}
func depricatedrnghandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
// get random lore from list
rngLore, err := redisClient.SRandMember("index:lore:added").Result()
if err != nil {
fmt.Printf("%v\n", err)
}
// get lore content
rngContent, err := loreCheck(rngLore, "lore", redisClient)
if err != nil {
fmt.Printf("%v\n", err)
rtm.SendMessage(rtm.NewOutgoingMessage("beep boop.. broken bot..", ev.Channel))
return
}
rngMsg := fmt.Sprintf("Random lore: `%s`\n%s", rngLore, rngContent)
rtm.SendMessage(rtm.NewOutgoingMessage(rngMsg, ev.Channel))
}
func rnghandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
// aka index:command:added
newType := fmt.Sprintf("index:lore:added")
//command, err := botfunc.Query(newType, redisClient)
command, err := loreQuery(newType, redisClient)
if err != nil {
glogger.Error.Println(err)
println(err)
}
// if the array is empty, exit early
if len(command) <= 0 {
return
}
// pick a random one
rand.Seed(time.Now().UnixNano())
rngLore := fmt.Sprint(command[rand.Intn(len(command))])
glogger.Debug.Printf("rng lore: %s\n", rngLore)
// get lore content
rngContent, err := loreCheck(rngLore, "lore", redisClient)
if err != nil {
glogger.Error.Printf("%v\n", err)
rtm.SendMessage(rtm.NewOutgoingMessage("beep boop.. broken bot..", ev.Channel))
return
}
glogger.Debug.Printf("rng content: %s\n", rngContent)
rngMsg := fmt.Sprintf("Random lore: `%s`\n%s", rngLore, rngContent)
rtm.SendMessage(rtm.NewOutgoingMessage(rngMsg, ev.Channel))
}
func egghandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
// aka index:command:added
newType := fmt.Sprintf("index:lore:added")
//command, err := botfunc.Query(newType, redisClient)
command, err := loreQuery(newType, redisClient)
if err != nil {
glogger.Error.Println(err)
println(err)
}
// if the array is empty, exit early
if len(command) <= 0 {
return
}
// pick a random one
rand.Seed(time.Now().UnixNano())
rngLore := fmt.Sprint(command[rand.Intn(len(command))])
println(rngLore)
// get lore content
rngContent, err := loreCheck(rngLore, "lore", redisClient)
if err != nil {
fmt.Printf("%v\n", err)
rtm.SendMessage(rtm.NewOutgoingMessage("beep boop.. broken bot..", ev.Channel))
return
}
// set the lore in case we cant find it.
redisClient.Del("index:egg")
err = redisClient.Set("index:egg", rngLore, 0).Err()
if err != nil {
glogger.Error.Println("error updating redis egg index")
}
rngMsg := fmt.Sprintf("begin the hunt boiiz...\n ```%s```", rngContent)
rtm.SendMessage(rtm.NewOutgoingMessage(rngMsg, ev.Channel))
}
func giveuphandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
giveup, err := redisClient.Get("index:egg").Result()
if err != nil {
glogger.Error.Println("error getting redis egg index")
glogger.Error.Println(err)
}
giveupMsg := fmt.Sprintf("foolz.. it was `%s`", giveup)
rtm.SendMessage(rtm.NewOutgoingMessage(giveupMsg, ev.Channel))
}
func gnuhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string) {
gnuFmt := fmt.Sprintf("slaps the nonfree software out of %s's hands", target)
rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel))
@ -275,8 +507,7 @@ func listhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEve
glogger.Error.Println(err)
println(err)
}
fCommand := strings.Replace(command, " ", "\n", -1)
fCommand = strings.Replace(fCommand, "\n", "", 1)
fCommand := strings.Join(command, "\n")
// initialize buffer
bodyBuf := &bytes.Buffer{}
@ -314,8 +545,8 @@ func listhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEve
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)
loreammount := len(command)
status := fmt.Sprintf("the lore db has %d entries", loreammount)
rtm.SendMessage(rtm.NewOutgoingMessage(status, ev.Channel))
}
@ -336,6 +567,163 @@ func newhandler(rtm *slack.RTM, ev *slack.MessageEvent, message, newType string,
}
}
func newdubshandler(rtm *slack.RTM, ev *slack.MessageEvent, newContent string, redisClient *redis.Client) {
// prep content for dubsification
// aka remove the '<' and '>' that slack adds
newContent = strings.Replace(newContent, "<", "", -1)
newContent = strings.Replace(newContent, ">", "", -1)
err := redisClient.SAdd("index:dubs", newContent)
if err != nil {
rtm.SendMessage(rtm.NewOutgoingMessage("dubs checked", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("dubs check failed", ev.Channel))
}
}
func rmdubshandler(rtm *slack.RTM, ev *slack.MessageEvent, rmContent string, redisClient *redis.Client) {
// prep content for dubs removal
// aka remove the '<' and '>' that slack adds
rmContent = strings.Replace(rmContent, "<", "", -1)
rmContent = strings.Replace(rmContent, ">", "", -1)
err := redisClient.SRem("index:dubs", rmContent)
if err != nil {
rtm.SendMessage(rtm.NewOutgoingMessage("dubs unchecked", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("dubs check failed", ev.Channel))
}
}
func dubshandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
// aka index:command:added
newType := fmt.Sprintf("index:dubs")
command, err := loreQuery(newType, redisClient)
if err != nil {
glogger.Error.Println(err)
}
// if the array is empty, exit early
if len(command) <= 0 {
return
}
// pick a random one
rand.Seed(time.Now().UnixNano())
rngDubs := fmt.Sprint(command[rand.Intn(len(command))])
rtm.SendMessage(rtm.NewOutgoingMessage(rngDubs, ev.Channel))
// roll after we've proccessed dubs
// you already know tho..
//re := regexp.MustCompile(`taylor|swift|2725225`)
//if re.MatchString(rngDubs) {
// rndInt := 1 + rand.Intn(10-1)
// rndStr := fmt.Sprintf("%06d", rand.Int63n(1e6))
// finStr := fmt.Sprintf("%s%d%d", rndStr, rndInt, rndInt)
// rtm.SendMessage(rtm.NewOutgoingMessage(finStr, ev.Channel))
//} else {
// rollhandler(rtm, ev)
//}
rollhandler(rtm, ev)
}
func newlunchhandler(rtm *slack.RTM, ev *slack.MessageEvent, newContent string, redisClient *redis.Client) {
// prep content for lunchification
// aka remove the '<' and '>' that slack adds
newContent = strings.Replace(newContent, "<", "", -1)
newContent = strings.Replace(newContent, ">", "", -1)
err := redisClient.SAdd("index:lunch", newContent)
if err != nil {
rtm.SendMessage(rtm.NewOutgoingMessage("lunch location added", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("lunch location addition failed", ev.Channel))
}
}
func rmlunchhandler(rtm *slack.RTM, ev *slack.MessageEvent, rmContent string, redisClient *redis.Client) {
// prep content for lunch removal
// aka remove the '<' and '>' that slack adds
rmContent = strings.Replace(rmContent, "<", "", -1)
rmContent = strings.Replace(rmContent, ">", "", -1)
err := redisClient.SRem("index:lunch", rmContent)
if err != nil {
rtm.SendMessage(rtm.NewOutgoingMessage("lunch location removed", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("lunch location removal failed", ev.Channel))
}
}
func lunchhandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
// aka index:command:added
newType := fmt.Sprintf("index:lunch")
command, err := loreQuery(newType, redisClient)
if err != nil {
glogger.Error.Println(err)
}
// if the array is empty, exit early
if len(command) <= 0 {
return
}
// pick a random one
rand.Seed(time.Now().UnixNano())
rngDubs := fmt.Sprint(command[rand.Intn(len(command))])
rtm.SendMessage(rtm.NewOutgoingMessage(rngDubs, ev.Channel))
}
func newbrbhandler(rtm *slack.RTM, ev *slack.MessageEvent, newContent string, redisClient *redis.Client) {
// prep content for brbification
// aka remove the '<' and '>' that slack adds
newContent = strings.Replace(newContent, "<", "", -1)
newContent = strings.Replace(newContent, ">", "", -1)
err := redisClient.SAdd("index:brb", newContent)
if err != nil {
rtm.SendMessage(rtm.NewOutgoingMessage("tjancism added", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("tjanceism addition failed", ev.Channel))
}
}
func rmbrbhandler(rtm *slack.RTM, ev *slack.MessageEvent, rmContent string, redisClient *redis.Client) {
// prep content for brb removal
// aka remove the '<' and '>' that slack adds
rmContent = strings.Replace(rmContent, "<", "", -1)
rmContent = strings.Replace(rmContent, ">", "", -1)
err := redisClient.SRem("index:brb", rmContent)
if err != nil {
rtm.SendMessage(rtm.NewOutgoingMessage("tjancism removed", ev.Channel))
} else {
rtm.SendMessage(rtm.NewOutgoingMessage("tjancism removal failed", ev.Channel))
}
}
func brbhandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) {
// aka index:command:added
newType := fmt.Sprintf("index:brb")
command, err := loreQuery(newType, redisClient)
if err != nil {
glogger.Error.Println(err)
}
// if the array is empty, exit early
if len(command) <= 0 {
return
}
// pick a random one
rand.Seed(time.Now().UnixNano())
rngDubs := fmt.Sprint(command[rand.Intn(len(command))])
rtm.SendMessage(rtm.NewOutgoingMessage(rngDubs, ev.Channel))
}
func rmhandler(rtm *slack.RTM, ev *slack.MessageEvent, message, rmType string, redisClient *redis.Client) {
// get nick from slack api
user, err := rtm.GetUserInfo(ev.User)

72
readme.md

@ -0,0 +1,72 @@
# Lorebot
This is the bot running over at the sampled slack.
## Commands
### 2 parameters
* `lore`: get a specific lore
* `rip`: rest in piece a target
* `summon`: summon the specified target
* `gnu`: educate the target on freedoms
* `newdubs`: add a new dubs image
* `rmdubs`: remove an existing dubs image
* `created`: lookup when lore was created (some old lore wont have this field)
* `owner`: lookup who created a lore (some old lore wont have this field)
* `removedby`: lookup who removed a lore
* `removed`: lookup when lore was removed
### 1 parameter
* `help`: display help message 1
* `help2`: display help message 2
* `rng`: display random lore with the new rng algorithm
* `depricatedrng`: select random lore with the old rng algorithm
* `roll`: generate a random 8 digit integer
* `d20`: roll a 20 sided die
* `rr`: play russian roulette
* `dice`: roll a 6 sided die
* `dubs`: pick a random 'dubs checkum' image and roll
* `listen`: its what you hearin
* `lorelist`: generate a list of all lore
* `removedlore`: generate a list of removed lore
* `lorestatus`: display the number of active lore
## Building
This project makes use of the `make` command. The following commands are supported.
* `all`: dynamically compiles/builds the bot. outputs binary file to `bin/`
* `run`: runs the bot
* `stat`: statically compiles/builds the bot. outputs binary file to `bin/`
* `docker`: builds a docker image
* `aci`: builds an ACI image
* `test_docker`: tests the docker image
* `test_aci`: tests the ACI image
* `clean`: cleans the project directory of any temp files
## Configuration
This bot takes advantage of the gcfg configuration which follows the INI
syntax.
`config.gcfg`
```
[lorebot]
loglevel = "debug"
slackdebug = false
bootstrapdelay = 1
AuthFile = ".auth"
[redis]
host = "127.0.0.1:6379"
password = ""
```
* `loglevel`: loglevel for the app (debug/info/error)
* `slackdebug`: wheather or not to show slacks API debugging
* `bootstrapdeplay`: number in seconds to delay binding the bot to redis (useful in containers)
* `AuthFile`: name/location of file containing the slack key
* `host`: location of the redis server
* `password`: password of the redis server
`.auth`
```
xoxb-86456556-IJDJksfkejsjdfj
```
this is a key generated by slack for the bot
Loading…
Cancel
Save