mirror of https://github.com/velour/catbase.git
Merge pull request #156 from velour/cowsayslash
cowsay: velour's first slash command
This commit is contained in:
commit
18013d8fca
|
@ -5,6 +5,8 @@ package talker
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -55,6 +57,7 @@ func New(b bot.Bot) *TalkerPlugin {
|
||||||
}
|
}
|
||||||
b.Register(tp, bot.Message, tp.message)
|
b.Register(tp, bot.Message, tp.message)
|
||||||
b.Register(tp, bot.Help, tp.help)
|
b.Register(tp, bot.Help, tp.help)
|
||||||
|
tp.registerWeb()
|
||||||
return tp
|
return tp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +67,13 @@ func (p *TalkerPlugin) message(kind bot.Kind, message msg.Message, args ...inter
|
||||||
lowermessage := strings.ToLower(body)
|
lowermessage := strings.ToLower(body)
|
||||||
|
|
||||||
if message.Command && strings.HasPrefix(lowermessage, "cowsay") {
|
if message.Command && strings.HasPrefix(lowermessage, "cowsay") {
|
||||||
return p.cowSay(message)
|
msg, err := p.cowSay(strings.TrimPrefix(message.Body, "cowsay "))
|
||||||
|
if err != nil {
|
||||||
|
p.Bot.Send(bot.Message, channel, "Error running cowsay: %s", err)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.Bot.Send(bot.Message, channel, msg)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Command && strings.HasPrefix(lowermessage, "list cows") {
|
if message.Command && strings.HasPrefix(lowermessage, "list cows") {
|
||||||
|
@ -105,40 +114,29 @@ func (p *TalkerPlugin) help(kind bot.Kind, message msg.Message, args ...interfac
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *TalkerPlugin) cowSay(message msg.Message) bool {
|
func (p *TalkerPlugin) cowSay(text string) (string, error) {
|
||||||
fields := strings.Split(message.Body, " ")
|
fields := strings.Split(text, " ")
|
||||||
text := strings.Join(fields[1:], " ")
|
|
||||||
cow := "default"
|
cow := "default"
|
||||||
if len(fields) > 1 && p.hasCow(fields[1]) {
|
if len(fields) > 1 && p.hasCow(fields[0]) {
|
||||||
cow = fields[1]
|
cow = fields[0]
|
||||||
text = strings.Join(fields[2:], " ")
|
text = strings.Join(fields[1:], " ")
|
||||||
}
|
}
|
||||||
cmd := exec.Command("cowsay", "-f", cow, text)
|
cmd := exec.Command("cowsay", "-f", cow, text)
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if p.checkErr(err, message.Channel) {
|
if err != nil {
|
||||||
return true
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.checkErr(cmd.Start(), message.Channel) {
|
if err = cmd.Start(); err != nil {
|
||||||
return true
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := ioutil.ReadAll(stdout)
|
output, err := ioutil.ReadAll(stdout)
|
||||||
if p.checkErr(err, message.Channel) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Bot.Send(bot.Message, message.Channel, fmt.Sprintf("```%s```", output))
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *TalkerPlugin) checkErr(err error, ch string) bool {
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Bot.Send(bot.Message, ch, "Error running cowsay: %s", err)
|
return "", err
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
|
return fmt.Sprintf("```%s```", output), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *TalkerPlugin) hasCow(cow string) bool {
|
func (p *TalkerPlugin) hasCow(cow string) bool {
|
||||||
|
@ -170,3 +168,16 @@ func (p *TalkerPlugin) allCows() []string {
|
||||||
}
|
}
|
||||||
return cows
|
return cows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *TalkerPlugin) registerWeb() {
|
||||||
|
http.HandleFunc("/slash/cowsay", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.ParseForm()
|
||||||
|
log.Printf("Cowsay:\n%+v", r.PostForm.Get("text"))
|
||||||
|
msg, err := p.cowSay(r.PostForm.Get("text"))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "Error running cowsay: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s", msg)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue