This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch.go
480 lines (401 loc) · 13.1 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package botsbyuberswe
import (
"encoding/json"
"fmt"
twitch "github.com/gempir/go-twitch-irc/v2"
"github.com/nicklaw5/helix"
"github.com/syndtr/goleveldb/leveldb/util"
"log"
"time"
)
// TODO refactor and comment this entire file
func twitchIRCHandler() {
// Iterate and connect bots
BotIter := db.NewIterator(util.BytesPrefix([]byte("bot:")), nil)
for BotIter.Next() {
var bot Bot
err := json.Unmarshal(BotIter.Value(), &bot)
if err != nil {
log.Println(err)
continue
}
// If the twitch id does not exist in the bot connections map we add it
if _, ok := botConnections[bot.UserTwitchID]; !ok {
if bot.Name == defaultBot {
log.Printf("Universal bot id found: %s\n", bot.UserTwitchID)
universalBotTwitchID = bot.UserTwitchID
}
bot.TwitchIRCClient = connectBotToTwitch(bot)
bot.Connected = true
botConnections[bot.UserTwitchID] = bot
}
}
BotIter.Release()
err := BotIter.Error()
if err != nil {
log.Println(err)
}
// Iterate and connect users
iter := db.NewIterator(util.BytesPrefix([]byte("user:")), nil)
for iter.Next() {
var user User
err := json.Unmarshal(iter.Value(), &user)
if err != nil {
log.Println(err)
continue
}
// If there is no client connection we make a new one
if _, ok := clientConnections[user.TwitchID]; !ok {
user.TwitchIRCClient = connectToTwitch(user)
clientConnections[user.TwitchID] = user
user.Connected = true
// We store the user object with the twitchID for reference
err = user.store()
if err != nil {
log.Println(err)
return
}
// Connect universal bot to channel if user doesn't have their own bot
if _, ok := botConnections[user.TwitchID]; !ok {
// Connect universal bot for new user
connect := ConnectChannel{
Name: user.Channel.Name,
Connect: true,
}
universalBot <- connect
}
}
}
iter.Release()
err = iter.Error()
if err != nil {
log.Println(err)
}
}
// refreshHandler refreshes tokens every 10 minutes if needed
func refreshHandler() {
for {
// After 10 minutes we try to refresh our tokens
iter := db.NewIterator(util.BytesPrefix([]byte("user:")), nil)
for iter.Next() {
// Use key/value.
log.Printf("Refreshing tokens of user %s: %s\n", string(iter.Key()), string(iter.Value()))
var user User
err := json.Unmarshal(iter.Value(), &user)
if err != nil {
log.Println(err)
continue
}
// if user token expires in the next 10 min
if user.TokenExpiry.Before(time.Now().Add(2 * time.Hour)) {
log.Printf("Refreshing tokens for: %s\n", user.TwitchID)
client, err := helix.NewClient(&helix.Options{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURI: redirectURL,
})
if err != nil {
log.Println(err)
continue
}
refreshResponse, err := client.RefreshUserAccessToken(user.RefreshToken)
if err != nil {
log.Println(err)
continue
}
user.RefreshToken = refreshResponse.Data.RefreshToken
user.AccessToken = refreshResponse.Data.AccessToken
tokenExpiry := time.Now().Add(time.Duration(refreshResponse.Data.ExpiresIn) * time.Second)
log.Printf("Refreshed: New tokens should refresh at %s", tokenExpiry.String())
user.TokenExpiry = tokenExpiry
// We store the user object with the twitchID for reference
err = user.store()
if err != nil {
log.Println(err)
return
}
}
}
iter.Release()
err := iter.Error()
if err != nil {
log.Println(err)
}
// Renew bot tokens
// After 10 minutes we try to refresh our tokens
botIter := db.NewIterator(util.BytesPrefix([]byte("bot:")), nil)
for botIter.Next() {
log.Printf("Refreshing tokens of bot %s: %s\n", string(botIter.Key()), string(botIter.Value()))
var bot Bot
err := json.Unmarshal(botIter.Value(), &bot)
if err != nil {
log.Println(err)
continue
}
// if user token expires in the next 10 min
if bot.TokenExpiry.Before(time.Now().Add(2 * time.Hour)) {
log.Printf("Refreshing tokens for bot: %s\n", bot.UserTwitchID)
client, err := helix.NewClient(&helix.Options{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURI: redirectURL,
})
if err != nil {
log.Println(err)
continue
}
refreshResponse, err := client.RefreshUserAccessToken(bot.RefreshToken)
if err != nil {
log.Println(err)
continue
}
bot.RefreshToken = refreshResponse.Data.RefreshToken
bot.AccessToken = refreshResponse.Data.AccessToken
tokenExpiry := time.Now().Add(time.Duration(refreshResponse.Data.ExpiresIn) * time.Second)
log.Printf("Bot Refreshed: New tokens should refresh at %s", tokenExpiry.String())
bot.TokenExpiry = tokenExpiry
// We store the user object with the twitchID for reference
err = bot.store()
if err != nil {
log.Println(err)
return
}
}
}
botIter.Release()
err = botIter.Error()
if err != nil {
log.Println(err)
}
// Sleep for 10 minutes
time.Sleep(10 * time.Minute)
}
}
func reconnectHandler(user User) {
log.Printf("Reconnecting to Twitch %s\n", user.TwitchID)
data, err := db.Get([]byte(fmt.Sprintf("user:%s", user.TwitchID)), nil)
if err != nil {
log.Println(err)
return
}
err = json.Unmarshal(data, &user)
if err != nil {
log.Println(err)
return
}
user.Connected = false
user.TwitchIRCClient = connectToTwitch(user)
user.Connected = true
user.ConnectAttempts = 0
user.TwitchConnectFailures++
clientConnections[user.TwitchID] = user
err = user.store()
if err != nil {
log.Printf("Error: %s", err)
}
log.Println("Connect started for reconnect")
}
func reconnectBotHandler(bot Bot) {
data, err := db.Get([]byte(fmt.Sprintf("bot:%s", bot.UserTwitchID)), nil)
if err != nil {
log.Println(err)
return
}
err = json.Unmarshal(data, &bot)
if err != nil {
log.Println(err)
return
}
log.Printf("Reconnecting bot to Twitch %s\n", bot.UserTwitchID)
bot.TwitchIRCClient = connectBotToTwitch(bot)
bot.ConnectAttempts = 0
bot.Connected = true
botConnections[bot.UserTwitchID] = bot
}
func connectBotToTwitch(bot Bot) *twitch.Client {
log.Println("creating twitch client")
client := twitch.NewClient(bot.Name, fmt.Sprintf("oauth:%s", bot.AccessToken))
log.Println("configuring twitch bot client")
client.OnConnect(func() {
log.Println("Client bot connected")
initmsg := WebsocketMessage{
Key: "channel",
Channel: bot.UserChannelName,
BotName: bot.Name,
TwitchID: bot.UserTwitchID,
}
broadcastMessage(initmsg)
})
client.OnPingMessage(func(message twitch.PingMessage) {
log.Printf("Bot Ping received: %s\n", message.Message)
})
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
go handleCommand(bot, message, client)
twitchID, err := getTwitchIDFromChannelName(message.Channel)
if err != nil {
log.Println(err)
return
}
initmsg := WebsocketMessage{
Key: "notice",
PrivateMessage: message,
TwitchID: twitchID,
}
broadcastMessage(initmsg)
})
client.Join(bot.UserChannelName)
if bot.UserTwitchID == universalBotTwitchID {
// loop through all users that don't have their own bot and connect to their channels if this is universal bot
iter := db.NewIterator(util.BytesPrefix([]byte("user:")), nil)
for iter.Next() {
var user User
err := json.Unmarshal(iter.Value(), &user)
if err != nil {
log.Println(err)
continue
}
if _, ok := botConnections[user.TwitchID]; !ok {
connect := ConnectChannel{
Name: user.Channel.Name,
Connect: true,
}
universalBot <- connect
}
}
iter.Release()
err := iter.Error()
if err != nil {
log.Println(err)
}
} else {
// if this is not the universal bot then remove this user from the universal bot
connect := ConnectChannel{
Name: bot.UserChannelName,
Connect: false,
}
universalBot <- connect
}
go func() {
err := client.Connect()
if err != nil {
log.Println(err)
if bot.ConnectAttempts > 20 {
delete(botConnections, bot.UserTwitchID)
return
}
bot.ConnectAttempts++
bot.Connected = false
time.Sleep(time.Duration(bot.ConnectAttempts*10) * time.Second)
reconnectBotHandler(bot)
}
}()
return client
}
func handleMainBotConnects() {
for {
// Grab the next message from the broadcast channel
connect := <-universalBot
if universalBotTwitchID != "" {
_, ok := botConnections[universalBotTwitchID]
if ok {
if connect.Connect {
log.Printf("Universal bot %s is joining %s\n", universalBotTwitchID, connect.Name)
botConnections[universalBotTwitchID].TwitchIRCClient.Join(connect.Name)
universalConnectedChannels = append(universalConnectedChannels, connect.Name)
} else {
log.Printf("Universal bot %s is leaving %s\n", universalBotTwitchID, connect.Name)
botConnections[universalBotTwitchID].TwitchIRCClient.Depart(connect.Name)
}
}
}
}
}
func connectToTwitch(user User) *twitch.Client {
log.Println("creating twitch client")
client := twitch.NewClient(user.Channel.Name, fmt.Sprintf("oauth:%s", user.AccessToken))
log.Println("configuring twitch client")
client.OnConnect(func() {
log.Println("Client connected")
initmsg := WebsocketMessage{
Key: "channel",
Channel: user.Channel.Name,
TwitchID: user.TwitchID,
}
broadcastMessage(initmsg)
})
client.OnPingMessage(func(message twitch.PingMessage) {
log.Printf("Ping received: %s\n", message.Message)
})
// Ths user listens to notices and the bot listens to commands
client.OnUserNoticeMessage(func(message twitch.UserNoticeMessage) {
jsonString, err := json.Marshal(message.MsgParams)
log.Println(fmt.Sprintf("New notice: %s %s", string(jsonString), message.MsgID))
// Check if the user is set for the message
if msgUser, ok := message.MsgParams["msg-param-login"]; ok {
// Check if message is a raid
if message.MsgID == "raid" {
if _, ok := clientConnections[user.TwitchID]; ok {
for i, v := range clientConnections[user.TwitchID].State.Variables {
if v.Name == "lastraid" || v.Name == "lasthostraid" {
if len(clientConnections[user.TwitchID].State.Variables) > i {
clientConnections[user.TwitchID].State.Variables[i].Value = msgUser
}
}
}
}
// 2019/08/25 12:20:15 New notice: {"msg-param-displayName":"El_Funko","msg-param-login":"el_funko",
// "msg-param-profileImageURL":"https://static-cdn.jtvnw.net/jtv_user_pictures/823e29e0-2bef-42a3-b0df-3d8755dbde53-profile_image-70x70.png",
// "msg-param-viewerCount":"38"} raid
// check if message is a host
} else if message.MsgID == "host" {
if _, ok := clientConnections[user.TwitchID]; ok {
for i, v := range clientConnections[user.TwitchID].State.Variables {
if v.Name == "lasthost" || v.Name == "lasthostraid" {
if len(clientConnections[user.TwitchID].State.Variables) > i {
clientConnections[user.TwitchID].State.Variables[i].Value = msgUser
}
}
}
}
}
}
// TODO These are examples of other messages that we could make variables for
// 2019/08/25 13:07:31 New notice: {"msg-param-cumulative-months":"1","msg-param-months":"0","msg-param-should-share-streak":"0","msg-param-sub-plan":"Prime","msg-param-sub-plan-name":"Conscript for war"} sub
// 2019/08/25 13:07:42 New notice: {"msg-param-cumulative-months":"7","msg-param-months":"0","msg-param-should-share-streak":"0","msg-param-sub-plan":"1000","msg-param-sub-plan-name":"Conscript for war"} resub
// 2019/08/25 11:33:06 New notice: {"msg-param-months":"1","msg-param-origin-id":"da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 af d8 07 09","msg-param-recipient-display-name":"clearancewater","msg-param-recipient-id":"229767697","msg-param-recipient-user-name":"clearancewater","msg-param-sender-count":"0","msg-param-sub-plan":"1000","msg-param-sub-plan-name":"Conscript for war"} subgift
// 2019/08/25 11:33:05 New notice: {"msg-param-mass-gift-count":"1","msg-param-origin-id":"22 a3 a4 cd 7e 82 bd e9 2d ba e8 12 34 54 44 08 11 15 a6 e5","msg-param-sender-count":"1","msg-param-sub-plan":"1000"} submysterygift
// 2019/08/25 11:32:14 New notice: {"msg-param-sender-login":"robust_meu","msg-param-sender-name":"RobUst_meu"} giftpaidupgrade
// 2019/08/25 11:02:35 New notice: {"msg-param-bits-amount":"500","msg-param-domain":"seasonal-food-fight","msg-param-min-cheer-amount":"200","msg-param-selected-count":"10"} rewardgift
if err != nil {
log.Println(err)
return
}
initmsg := WebsocketMessage{
Key: "notice",
MsgParams: message.MsgParams,
TwitchID: user.TwitchID,
}
broadcastMessage(initmsg)
})
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
log.Println(fmt.Sprintf("New message detected: [%s] %s", message.Channel, message.Message))
log.Printf("%v\n", message)
})
client.Join(user.Channel.Name)
go func() {
err := client.Connect()
if err != nil {
log.Printf("Error in twitch irc connection for %s\n", user.TwitchID)
log.Println(err)
if user.ConnectAttempts > 20 {
delete(clientConnections, user.TwitchID)
return
}
user.ConnectAttempts++
user.Connected = false
time.Sleep(time.Duration(10*user.ConnectAttempts) * time.Second)
reconnectHandler(user)
}
}()
return client
}