-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
90 lines (72 loc) · 2.21 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
79
80
81
82
83
84
85
86
87
88
89
90
package glisten
import (
"fmt"
"log"
"strings"
"github.com/gempir/go-twitch-irc/v4"
)
// BotOptions are the values needed for connecting the bot to a channel/user
type BotOptions struct {
Channel string
Username string
Password string
}
// Bot is an abstraction over Twitch IRC that facilitates the creation of
// command-driven chat bots.
type Bot struct {
// Events is the channel where all events arrive after being handled by the
// registered handlers.
Events chan Event
// Errors is the channel were all connection and post-connection errors are
// sent for handling by the user.
Errors chan error
client *twitch.Client
handlers map[string]Handler
options BotOptions
}
// NewBot sets up a Bot with the provided options and opens its channels.
func NewBot(opts *BotOptions) (*Bot, error) {
var bot Bot
bot.handlers = make(map[string]Handler)
bot.options = *opts
bot.Events = make(chan Event)
bot.Errors = make(chan error)
return &bot, nil
}
// Add handler registers an event handler function on the bot's handler map.
func (b *Bot) AddHandler(trigger string, handler Handler) {
b.handlers[trigger] = handler
}
// Connect launches the OAuth flow and, if completed, connects to Twitch IRC
// and starts listening for messages, and should be launched in a goroutine.
func (b *Bot) Connect() {
b.client = twitch.NewClient(b.options.Username, b.options.Password)
b.client.OnConnect(func() {
log.Printf("bot connected to %s as %s\n", b.options.Channel, b.options.Username)
})
b.client.OnPrivateMessage(func(msg twitch.PrivateMessage) {
components := strings.Split(msg.Message, " ")
if len(components) < 1 {
return
}
cmd := strings.TrimSpace(components[0])
for trigger, handler := range b.handlers {
if cmd == trigger {
event := handler(Message(msg))
b.Events <- event
log.Println("handled event:", event.String())
return
}
}
})
b.client.OnNoticeMessage(func(msg twitch.NoticeMessage) {
b.Errors <- fmt.Errorf("error in notice callback: %s", msg.Message)
})
b.client.Join(b.options.Channel)
if err := b.client.Connect(); err != nil {
b.Errors <- fmt.Errorf("error connecting to channel: %w", err)
}
}
func (b Bot) Say(msg string) {
b.client.Say(b.options.Channel, msg)
}