|
|
@ -1,6 +1,7 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os" |
|
|
@ -37,18 +38,18 @@ func main() { |
|
|
|
initLogger(config.Lorebot.Loglevel) |
|
|
|
initLogger(config.Lorebot.Loglevel) |
|
|
|
|
|
|
|
|
|
|
|
// initialize redis connection
|
|
|
|
// initialize redis connection
|
|
|
|
//redisClient, err := initRedisConnection()
|
|
|
|
redisClient, err := initRedisConnection() |
|
|
|
//if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// glogger.Debug.Printf("redis conneciton cannot be made, trying again in %d seconds", config.Lorebot.BootstrapDelay)
|
|
|
|
glogger.Debug.Printf("redis conneciton cannot be made, trying again in %d seconds", config.Lorebot.BootstrapDelay) |
|
|
|
// time.Sleep(config.Lorebot.BootstrapDelay * time.Second)
|
|
|
|
time.Sleep(config.Lorebot.BootstrapDelay * time.Second) |
|
|
|
// redisClient, err = initRedisConnection()
|
|
|
|
redisClient, err = initRedisConnection() |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// glogger.Error.Println("redis connection cannot be made.")
|
|
|
|
glogger.Error.Println("redis connection cannot be made.") |
|
|
|
// os.Exit(1)
|
|
|
|
os.Exit(1) |
|
|
|
// }
|
|
|
|
} |
|
|
|
//}
|
|
|
|
} |
|
|
|
//glogger.Debug.Println("connection to redis succeeded.")
|
|
|
|
glogger.Debug.Println("connection to redis succeeded.") |
|
|
|
//glogger.Info.Println("link to redis on", config.Redis.Host)
|
|
|
|
glogger.Info.Println("link to redis on", config.Redis.Host) |
|
|
|
|
|
|
|
|
|
|
|
api := slack.New(config.Lorebot.APIToken) |
|
|
|
api := slack.New(config.Lorebot.APIToken) |
|
|
|
//api.SetDebug(true)
|
|
|
|
//api.SetDebug(true)
|
|
|
@ -67,14 +68,31 @@ Loop: |
|
|
|
// // Handle new user to client
|
|
|
|
// // Handle new user to client
|
|
|
|
case *slack.MessageEvent: |
|
|
|
case *slack.MessageEvent: |
|
|
|
if ev.Type == "message" { |
|
|
|
if ev.Type == "message" { |
|
|
|
if ev.Text == "test" { |
|
|
|
//if ev.Text == "test" {
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("MY NAME IS ..JHEFFFFF", ev.Channel)) |
|
|
|
// rtm.SendMessage(rtm.NewOutgoingMessage("MY NAME IS ..JHEFFFFF", ev.Channel))
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if strings.Contains(ev.Text, ".lore") {
|
|
|
|
|
|
|
|
// //rtm.SendMessage(rtm.NewOutgoingMessage("THA LORE", ev.Channel))
|
|
|
|
|
|
|
|
// s := strings.SplitN(string(ev.Text), " ", 2)
|
|
|
|
|
|
|
|
// //command, content := s[0], s[1]
|
|
|
|
|
|
|
|
// lorehandler(redisClient, rtm, ev, s[1])
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if strings.Contains(ev.Text, ".help") {
|
|
|
|
|
|
|
|
// helpmsg(rtm, ev)
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if strings.Contains(ev.Text, ".help2") {
|
|
|
|
|
|
|
|
// helpmsg(rtm, ev)
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
// see if the message begins with '.', it could be a command
|
|
|
|
|
|
|
|
comm := fmt.Sprintf("%c", ev.Text[0]) |
|
|
|
|
|
|
|
if comm == "." { |
|
|
|
|
|
|
|
if strings.Count(ev.Text, " ") == 0 { |
|
|
|
|
|
|
|
// no spaces, its a static command
|
|
|
|
|
|
|
|
staticCommandHandler(rtm, ev, redisClient) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// spaces, dynamic handler
|
|
|
|
|
|
|
|
dynamicCommandHandler(rtm, ev, redisClient) |
|
|
|
} |
|
|
|
} |
|
|
|
if strings.Contains(ev.Text, ".lore") { |
|
|
|
|
|
|
|
//rtm.SendMessage(rtm.NewOutgoingMessage("THA LORE", ev.Channel))
|
|
|
|
|
|
|
|
s := strings.SplitN(string(ev.Text), " ", 2) |
|
|
|
|
|
|
|
//command, content := s[0], s[1]
|
|
|
|
|
|
|
|
lorehandler(rtm, ev, s[1]) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
case *slack.RTMError: |
|
|
|
case *slack.RTMError: |
|
|
@ -123,8 +141,107 @@ func initRedisConnection() (*redis.Client, error) { |
|
|
|
return redisClient, redisErr |
|
|
|
return redisClient, redisErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func lorehandler(rtm *slack.RTM, ev *slack.MessageEvent, rawMessage string) { |
|
|
|
func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) { |
|
|
|
|
|
|
|
message := ev.Text |
|
|
|
|
|
|
|
if strings.Count(message, " ") < 1 { |
|
|
|
|
|
|
|
glogger.Debug.Println("not proper syntax") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// verify syntax
|
|
|
|
|
|
|
|
s := strings.SplitN(message, " ", 2) |
|
|
|
|
|
|
|
command, content := s[0], s[1] |
|
|
|
|
|
|
|
trimmedPrefix := strings.TrimPrefix(command, ".") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch trimmedPrefix { |
|
|
|
|
|
|
|
case "lore": |
|
|
|
|
|
|
|
s := strings.SplitN(string(ev.Text), " ", 2) |
|
|
|
|
|
|
|
lorehandler(redisClient, rtm, ev, s[1]) |
|
|
|
|
|
|
|
case "newlore": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
println(content) |
|
|
|
|
|
|
|
case "rmlore": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "rek": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "created": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "owner": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "removedby": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "removed": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func staticCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) { |
|
|
|
|
|
|
|
s := strings.SplitN(ev.Text, " ", 2) |
|
|
|
|
|
|
|
command := s[0] |
|
|
|
|
|
|
|
trimmedPrefix := strings.TrimPrefix(command, ".") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch trimmedPrefix { |
|
|
|
|
|
|
|
case "help": |
|
|
|
|
|
|
|
helpmsg(rtm, ev) |
|
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
case "lorelist": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "removedlore": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
case "lorestatus": |
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func helpmsg(rtm *slack.RTM, ev *slack.MessageEvent) { |
|
|
|
|
|
|
|
help1 := "I am the lore archival bot, I support the following commands:\n" + |
|
|
|
|
|
|
|
"```" + |
|
|
|
|
|
|
|
".lore: view lore\n" + |
|
|
|
|
|
|
|
".newlore: add new lore\n" + |
|
|
|
|
|
|
|
".rmlore: remove lore\n" + |
|
|
|
|
|
|
|
".lorelist: view lore database\n" + |
|
|
|
|
|
|
|
"use '.help2' to see advanced lookup commands." + |
|
|
|
|
|
|
|
"```" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage(help1, ev.Channel)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func advancedhelpmsg(rtm *slack.RTM, ev *slack.MessageEvent) { |
|
|
|
|
|
|
|
help2 := "```" + |
|
|
|
|
|
|
|
".created view creation timestamp\n" + |
|
|
|
|
|
|
|
".owner: who created the lore\n" + |
|
|
|
|
|
|
|
".removed: view removal timestamp\n" + |
|
|
|
|
|
|
|
".removedby: who removed the lore" + |
|
|
|
|
|
|
|
"```" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage(help2, ev.Channel)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func lorehandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEvent, rawMessage string) { |
|
|
|
loreTerm := strings.Replace(rawMessage, ".lore", "", -1) |
|
|
|
loreTerm := strings.Replace(rawMessage, ".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) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
// lore found, print it
|
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage(content, ev.Channel)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// lore not found, print error
|
|
|
|
|
|
|
|
rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("lore '%s' not found\n", loreTerm), ev.Channel)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func checkLore(searchTerm string, redisClient *redis.Client) (string, error) { |
|
|
|
|
|
|
|
val, err := redisClient.Get(fmt.Sprintf("lore:%s:content", searchTerm)).Result() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", errors.New("String not found.") |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return val, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//return "", nil
|
|
|
|
} |
|
|
|
} |
|
|
|