I will not test this

This commit is contained in:
Steve McCoy 2018-08-30 13:49:51 -04:00
parent 807e235489
commit 80d44a144d
1 changed files with 64 additions and 40 deletions

View File

@ -3,6 +3,7 @@
package picker package picker
import ( import (
"regexp"
"strings" "strings"
"fmt" "fmt"
@ -27,33 +28,17 @@ func New(bot bot.Bot) *PickerPlugin {
// This function returns true if the plugin responds in a meaningful way to the users message. // This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *PickerPlugin) Message(message msg.Message) bool { func (p *PickerPlugin) Message(message msg.Message) bool {
body := message.Body n, items, err := p.parse(message.Body)
pfx, sfx := "pick {", "}" if err != nil {
p.Bot.SendMessage(message.Channel, err.Error())
if strings.HasPrefix(body, pfx) && strings.HasSuffix(body, sfx) {
body = strings.TrimSuffix(strings.TrimPrefix(body, pfx), sfx)
items := strings.Split(body, ",")
item := items[rand.Intn(len(items))]
out := fmt.Sprintf("I've chosen \"%s\" for you.", strings.TrimSpace(item))
p.Bot.SendMessage(message.Channel, out)
return true
} else if strings.HasPrefix(body, "pick") && strings.HasSuffix(body, sfx) {
var n int
var q string
_, err := fmt.Sscanf(body, "pick %d %s", &n, &q)
if err != nil || q != "{" {
return false return false
} }
prefix := fmt.Sprintf("pick %d %s", n, q) if n == 1 {
body = strings.TrimSuffix(strings.TrimPrefix(body, prefix), sfx) item := items[rand.Intn(len(items))]
out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(item))
items := strings.Split(body, ",") p.Bot.SendMessage(message.Channel, out)
if n < 1 || n > len(items) { return true
return false
} }
rand.Shuffle(len(items), func(i, j int) { rand.Shuffle(len(items), func(i, j int) {
@ -63,15 +48,54 @@ func (p *PickerPlugin) Message(message msg.Message) bool {
var b strings.Builder var b strings.Builder
b.WriteString("I've chosen these hot picks for you: { ") b.WriteString("I've chosen these hot picks for you: { ")
for _, item := range items { fmt.Fprintf(&b, "%q", items[0])
fmt.Fprintf(&b, "%q ", item) for _, item := range items[1:] {
fmt.Fprintf(&b, ", %q", item)
} }
b.WriteString("}") b.WriteString(" }")
p.Bot.SendMessage(message.Channel, b.String()) p.Bot.SendMessage(message.Channel, b.String())
return true return true
}
var pickerListPrologue = regexp.MustParse("^pick[ \t]+([0-9]*)[ \t]+\{[ \t]+")
var pickerListItem = regexp.MustParse("^([^,]+),[ \t]+")
func (p * PickerPlugin) parse(body string) (int, []string, error) {
subs := pickerListPrologue.FindStringSubmatch(body)
if subs == nil {
return 0, nil, errors.New("saddle up for a syntax error")
} }
return false
n := 0
var err error
if subs[1] != "" {
n, err = strconv.Atoi(subs[1])
if err != nil {
return 0, nil, err
}
}
var items []string
rest := body[len(subs[0]):]
for {
subs = pickerListItem.FindStringSubmatch(rest)
if subs == nil {
break
}
items = append(items, subs[1])
rest = rest[len(subs[0]):]
}
if strings.TrimSpace(rest) = "}" {
return 0, nil, errors.New("lasso yerself that final curly brace, compadre")
}
if n < 1 || n > len(items) {
return 0, nil, errors.New("whoah there, bucko, I can't create something out of nothing")
}
return n, items, nil
} }
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.