-
Notifications
You must be signed in to change notification settings - Fork 42
/
prefix.go
44 lines (36 loc) · 962 Bytes
/
prefix.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
41
42
43
44
package matcher
import (
"strings"
"github.com/innogames/slack-bot/bot/msg"
"github.com/innogames/slack-bot/bot/util"
)
// NewPrefixMatcher accepts command which starts with the given prefix
// Example: prefix for "random"
// "random" -> match
// "random 1 2 3" -> match
// "randomness" -> no match
func NewPrefixMatcher(prefix string, run Runner) Matcher {
return prefixMatcher{
strings.ToLower(prefix),
run,
}
}
type prefixMatcher struct {
loweredPrefix string
run Runner
}
func (m prefixMatcher) Match(message msg.Message) (Runner, Result) {
var match MapResult
if strings.HasPrefix(strings.ToLower(message.Text), m.loweredPrefix) {
suffix := message.Text[len(m.loweredPrefix):]
// avoid matching "randomness" if prefix is "random"
if len(suffix) > 0 && suffix[0] != ' ' {
return nil, match
}
match := MapResult{
util.FullMatch: strings.TrimPrefix(suffix, " "),
}
return m.run, match
}
return nil, match
}