-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (95 loc) · 2.62 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/gempir/go-twitch-irc/v4"
"github.com/nicklaw5/helix/v2"
"go.etcd.io/bbolt"
)
var (
// Twitch IRC/API clients
CLIENT_IRC *twitch.Client
CLIENT_API *helix.Client
// Points database
POINTS_DB *bbolt.DB
// Message templates
POINTS_MSG string = "You have %d points 💸"
WIN_MSG string = "%s held the potato for %s and wins 😎 +%d"
LOSS_MSG string = "%s lost to potato 💀 -%s"
)
func init() {
loadEnv()
}
func main() {
fmt.Println("🥔 Welcome to gotato, hot potato for Twitch chat.")
fmt.Println()
fmt.Println("🖥️ Please complete the authentication flow in your browser.")
fmt.Println()
// Run OAuth flow and build IRC client
if err := authenticate(); err != nil {
log.Fatal("error authenticating with Twitch: ", err)
}
fmt.Println("🚪 I'm in!")
fmt.Println()
fmt.Println("🚧 Now I just need to set up a few more things...")
fmt.Println()
// Open points database
if db, err := bbolt.Open("points.db", 0666, nil); err != nil {
log.Fatal("error opening points database: ", err)
} else {
POINTS_DB = db
}
defer POINTS_DB.Close()
fmt.Println("✅ Points database loaded")
// Build API client
if err := createAPIClient(); err != nil {
log.Fatal("error authenticating api: ", err)
}
fmt.Println("✅ API client created")
// Initialize event and error channels
events := make(chan Event)
errors := make(chan error)
// Set up token refresh timer
authTimer := time.NewTimer(TOKEN_TTL)
fmt.Println("✅ Auth token refresh timer set")
// Set up interrupt signals channel
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
fmt.Println("✅ Interrupt signal channel opened")
// Launch game loop and listener concurrently
fmt.Println()
fmt.Println("🥔 All set, see you in chat! Remember to type !enable to enable timeouts.")
fmt.Println()
go game(events, errors)
go listener(events, errors)
for {
select {
// Watch for token refresh signals
case <-authTimer.C:
if err := refreshToken(); err != nil {
log.Fatal("error authenticating irc: ", err)
}
authTimer.Reset(TOKEN_TTL)
log.Println("token refreshed")
// Watch for process errors
case err := <-errors:
log.Fatal("error received: ", err)
// Watch for manual interrupt signals
case sig := <-signals:
log.Println("received", sig.String())
CLIENT_IRC.Say(CHANNEL, "gotato disconnected")
CLIENT_IRC.Depart(CHANNEL)
time.Sleep(1 * time.Second)
// Clean up and exit
if err := CLIENT_IRC.Disconnect(); err != nil {
log.Println(err)
}
close(events)
close(errors)
os.Exit(0)
}
}
}