-
Notifications
You must be signed in to change notification settings - Fork 11
/
decide.go
40 lines (36 loc) · 946 Bytes
/
decide.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package decide
import (
"fmt"
"github.com/lovelaced/glitzz/config"
"github.com/lovelaced/glitzz/core"
"github.com/lovelaced/glitzz/util"
"regexp"
"strings"
)
func New(sender core.Sender, conf config.Config) (core.Module, error) {
rv := &decide{
Base: core.NewBase("decide", sender, conf),
}
rv.AddCommand("decide", rv.decide)
return rv, nil
}
type decide struct {
core.Base
}
func (d *decide) decide(arguments core.CommandArguments) ([]string, error) {
if len(arguments.Arguments) > 0 {
s := strings.Join(arguments.Arguments, " ")
elements := regexp.MustCompile(", | or ").Split(s, -1)
if len(elements) < 2 {
return nil, nil
}
element, err := util.GetRandomArrayElement(elements)
if err != nil {
d.Log.Debug("get random array element failed", "err", err, "arguments", arguments)
return nil, nil
}
text := fmt.Sprintf("%s: %s", arguments.Nick, element)
return []string{text}, nil
}
return nil, nil
}