-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener.go
73 lines (59 loc) · 1.75 KB
/
listener.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/gempir/go-twitch-irc/v4"
)
func listener(events chan Event, errors chan error) {
// Initialize client with callbacks for game calls and connection issues
CLIENT_IRC = twitch.NewClient(USERNAME, "oauth:"+ACCESS_TOKEN)
// Watch messages (PrivateMessage = any message in the channel)
CLIENT_IRC.OnPrivateMessage(func(m twitch.PrivateMessage) {
if VERBOSE {
log.Println(m)
}
components := strings.Split(m.Message, " ")
cmd := strings.TrimSpace(components[0])
switch cmd {
case "!enable":
if m.User.Name == USERNAME {
BROADCASTER_ID = m.User.ID
log.Println("enabled timeouts")
return
}
case "!gotato":
events <- Event{Type: StartEvent}
case "!join":
events <- Event{Type: JoinEvent, UserID: m.User.ID, Username: m.User.Name}
case "!bet", "!wager":
if len(components) != 2 {
return
}
value, err := strconv.Atoi(components[1])
if err != nil {
return
}
events <- Event{Type: BetEvent, UserID: m.User.ID, Username: m.User.Name, Data: value}
case "!pass", "!toss":
events <- Event{Type: PassEvent, UserID: m.User.ID, Username: m.User.Name}
case "!points":
events <- Event{Type: PointsEvent, UserID: m.User.ID, Username: m.User.Name}
case "!reset":
events <- Event{Type: ResetEvent, UserID: m.User.ID, Username: m.User.Name}
}
})
// Watch for notices (i.e. login failures)
CLIENT_IRC.OnNoticeMessage(func(m twitch.NoticeMessage) {
if VERBOSE {
log.Println(m)
}
errors <- fmt.Errorf("error in notice callback: %s", m.Message)
})
// Join channel and connect client (blocking)
CLIENT_IRC.Join(CHANNEL)
if err := CLIENT_IRC.Connect(); err != nil {
errors <- fmt.Errorf("error connecting to channel: %w", err)
}
}