-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
78 lines (59 loc) · 1.36 KB
/
bot.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package lowbot
import (
"fmt"
"github.com/google/uuid"
)
type Bot struct {
BotID uuid.UUID
Consumer IConsumer
Channels map[uuid.UUID]IChannel
Running bool
}
func NewBot(consumer IConsumer, channels map[uuid.UUID]IChannel) *Bot {
return &Bot{
BotID: uuid.New(),
Consumer: consumer,
Channels: channels,
Running: false,
}
}
func (bot *Bot) Start() error {
for _, channel := range bot.Channels {
err := bot.StartChannel(channel)
if err != nil {
return err
}
go bot.StartConsumerChannel(channel)
}
bot.Running = true
return nil
}
func (bot *Bot) StartChannel(channel IChannel) error {
return channel.Start()
}
func (bot *Bot) StartConsumerChannel(channel IChannel) {
listener := channel.GetChannel().Broadcast.Listen()
for interaction := range listener {
answersInteraction, err := bot.Consumer.Run(interaction)
if answersInteraction == nil {
continue
}
for _, answerInteraction := range answersInteraction {
SendInteraction(channel, answerInteraction)
}
// TODO: improve how to receive the consumer errors
if err != nil {
printLog(fmt.Sprintf("%v: WhoID:<%v> ERR: %v\n", bot.Consumer.GetConsumer().Name, interaction.From.WhoID, err))
}
}
}
func (bot *Bot) Stop() error {
for _, channel := range bot.Channels {
err := channel.Stop()
if err != nil {
return err
}
}
bot.Running = false
return nil
}