-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch.go
54 lines (41 loc) · 1.09 KB
/
twitch.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
package main
import (
"fmt"
"strconv"
"time"
"github.com/gempir/go-twitch-irc/v4"
)
const maxConsecutiveReconnects = 10
const reconnectLimiterTimeout = time.Minute * 10
var client *twitch.Client
func fSay(format string, args ...interface{}) {
buf := fmt.Sprintf(format, args...)
client.Say(userSettings.UserName, buf)
}
func connectTwitch() {
client = twitch.NewClient(userSettings.UserName, "oauth:"+userSettings.AuthToken)
client.OnPrivateMessage(handleChat)
qlog("Joining channel: %v", userSettings.UserName)
client.Join(userSettings.UserName)
qlog("Connecting to twitch chat...")
go func() {
for x := 0; x < maxConsecutiveReconnects; x++ {
startTime := time.Now()
err := client.Connect()
if err != nil {
panic(err)
}
if time.Since(startTime) < reconnectLimiterTimeout {
val := x * x * 2 //Increasing delay
time.Sleep(time.Second * time.Duration(val))
} else {
//We were connected long enough that we can reset the reconnect limiter
x = 0
}
}
}()
}
func strToID(input string) int64 {
userid, _ := strconv.ParseInt(input, 10, 64)
return userid
}