-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_director.go
More file actions
33 lines (28 loc) · 961 Bytes
/
Copy pathmessage_director.go
File metadata and controls
33 lines (28 loc) · 961 Bytes
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
package qbot
import (
"strings"
"github.com/doozr/guac"
"github.com/doozr/qbot/queue"
"github.com/doozr/qbot/util"
)
// CreateMessageDirector creates a message handler that forwards messages to a public or private handler.
func CreateMessageDirector(id string, name string, publicHandler MessageHandler, privateHandler MessageHandler) MessageHandler {
isDirectedAtUs := func(text string) bool {
return strings.HasPrefix(text, name) || strings.HasPrefix(text, "<@"+id+">")
}
isPrivateChannel := func(channel string) bool {
return strings.HasPrefix(channel, "D")
}
return func(oq queue.Queue, m guac.MessageEvent) (q queue.Queue, err error) {
q = oq
if isPrivateChannel(m.Channel) {
// Private channels should never cause state change
_, err = privateHandler(q, m)
} else if isDirectedAtUs(m.Text) {
// Public channels can cause state change
_, m.Text = util.StringPop(m.Text)
q, err = publicHandler(q, m)
}
return
}
}