-
Notifications
You must be signed in to change notification settings - Fork 16
/
hybrid.go
55 lines (45 loc) · 1.04 KB
/
hybrid.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
45
46
47
48
49
50
51
52
53
54
55
package slackbot
import (
"sync"
)
type hybridRunner struct {
runner BotCommandRunner
channel string
}
func newHybridRunner(runner BotCommandRunner, channel string) *hybridRunner {
return &hybridRunner{
runner: runner,
channel: channel,
}
}
func (r *hybridRunner) RunCommand(args []string, channel string) error {
return r.runner.RunCommand(args, r.channel)
}
type HybridBackendMember struct {
Backend BotBackend
Channel string
}
type HybridBackend struct {
backends []HybridBackendMember
}
func NewHybridBackend(backends ...HybridBackendMember) *HybridBackend {
return &HybridBackend{
backends: backends,
}
}
func (b *HybridBackend) SendMessage(text string, channel string) {
for _, backend := range b.backends {
backend.Backend.SendMessage(text, backend.Channel)
}
}
func (b *HybridBackend) Listen(runner BotCommandRunner) {
var wg sync.WaitGroup
for _, backend := range b.backends {
wg.Add(1)
go func(b HybridBackendMember) {
b.Backend.Listen(newHybridRunner(runner, b.Channel))
wg.Done()
}(backend)
}
wg.Wait()
}