forked from DigitalPulseSoftware/NotaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_twitch.lua
More file actions
674 lines (575 loc) · 17.9 KB
/
module_twitch.lua
File metadata and controls
674 lines (575 loc) · 17.9 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
-- Copyright (C) 2018 Jérôme Leclercq
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
local client = Client
local config = Config
local discordia = Discordia
local bot = Bot
local enums = discordia.enums
local wrap = coroutine.wrap
Module.Name = "twitch"
Module.GameCache = {}
discordia.extensions()
local httpCodec = require('http-codec')
local net = require('coro-net')
local json = require('json')
local sha256 = require('sha256')
local querystring = require('querystring')
local twitchAPI = require('./twitchapi.lua')
function Module:GetConfigTable()
return {
{
Name = "TwitchConfig",
Description = "List of watched channels with title patterns for messages to post on channel goes up",
Type = bot.ConfigType.Custom,
Default = {}
},
{
Global = true,
Name = "CallbackEndpoint",
Description = "URI which will be sent to Twitch for channel events",
Type = bot.ConfigType.String,
Default = ""
},
{
Global = true,
Name = "ListenPort",
Description = "Port on which internal server listens",
Type = bot.ConfigType.Integer,
Default = 14793
},
{
Global = true,
Name = "SilenceDuration",
Description = "Duration during which a stream won't trigger other notifications after a notification",
Type = bot.ConfigType.Duration,
Default = 30 * 60
},
{
Global = true,
Name = "SubscribeDuration",
Description = "How long should subscriptions to a channel last before being renewed",
Type = bot.ConfigType.Duration,
Default = 60 * 60
},
{
Global = true,
Name = "TwitchClientId",
Description = "Twitch application client id",
Type = bot.ConfigType.String,
Default = "",
Sensitive = true
},
{
Global = true,
Name = "TwitchClientSecret",
Description = "Twitch application secret",
Type = bot.ConfigType.String,
Default = "",
Sensitive = true
}
}
end
function Module:GetWatchedChannels()
local persistentData = self:GetPersistentData(nil)
persistentData.watchedChannels = persistentData.watchedChannels or {}
return persistentData.watchedChannels
end
function Module:OnLoaded()
-- Regenerate secret
self.API = twitchAPI(discordia, client, self.GlobalConfig.TwitchClientId, self.GlobalConfig.TwitchClientSecret)
local secretLifespan = 24 * 60 * 60
self.ChannelAlerts = {}
self.Secret = self:GenerateSecret(128)
self.SecretTimeout = os.time() + secretLifespan
for channelId, channelData in pairs(self:GetWatchedChannels()) do
channelData.Guilds = nil -- Was removed in a previous version (replaced by self.ChannelAlerts)
channelData.Subscribing = false
channelData.WaitingForConfirm = false
end
self.Clock = discordia.Clock()
self.Clock:on("sec", function ()
local watchedChannels = self:GetWatchedChannels()
local now = os.time()
if (now >= self.SecretTimeout) then
self.Secret = self:GenerateSecret(128)
self.SecretTimeout = now + secretLifespan
end
for channelId, channelData in pairs(watchedChannels) do
if (channelData.RenewTime <= now and not channelData.Subscribing) then
local channelAlerts = self.ChannelAlerts[channelId]
if (channelAlerts and not table.empty(channelAlerts)) then
channelData.RenewTime = now + 2*60 -- Retry in two minutes if twitch didn't answer or subscribing failed
wrap(function () bot:CallModuleFunction(self, "SubscribeToTwitch", channelId) end)()
else
watchedChannels[channelId] = nil
self.ChannelAlerts[channelId] = nil
end
end
end
end)
self.Server = self:SetupServer()
if (not self.Server) then
return false, "Failed to setup server"
end
self.Clock:start()
self:RegisterCommand({
Name = "twitchinfo",
Args = {
{Name = "channel", Type = Bot.ConfigType.String}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Query twitch informations about a channel",
Func = function (commandMessage, channel)
local profileData, err
if (channel:match("^%d+$")) then
profileData, err = self.API:GetUserById(channel)
else
profileData, err = self.API:GetUserByName(channel)
end
if (profileData) then
local channelUrl = "https://www.twitch.tv/" .. profileData.login
commandMessage:reply({
embed = {
title = profileData.display_name,
description = profileData.description,
url = channelUrl,
author = {
name = profileData.login,
url = channelUrl,
icon_url = profileData.profile_image_url
},
thumbnail = {
url = profileData.profile_image_url
},
fields = {
{
name = "View count",
value = profileData.view_count
},
{
name = "Type",
value = #profileData.broadcaster_type > 0 and profileData.broadcaster_type or "regular"
}
},
image = {
url = profileData.offline_image_url
},
footer = {
text = "ID: " .. profileData.id
}
}
})
else
if (err) then
commandMessage:reply(string.format("An error occurred: %s", err))
else
commandMessage:reply(string.format("Profile `%s` not found", channel))
end
end
end
})
self:RegisterCommand({
Name = "twitchgameinfo",
Args = {
{Name = "channel", Type = Bot.ConfigType.String}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Query twitch informations about a game",
Func = function (commandMessage, channel)
local gameData, err
if (channel:match("^%d+$")) then
gameData, err = self.API:GetGameById(channel)
else
gameData, err = self.API:GetGameByName(channel)
end
if (gameData) then
local thumbnail = gameData.box_art_url
thumbnail = thumbnail:gsub("{width}", 285)
thumbnail = thumbnail:gsub("{height}", 380)
commandMessage:reply({
embed = {
title = gameData.name,
image = {
url = thumbnail
},
footer = {
text = "ID: " .. gameData.id
}
}
})
else
if (err) then
commandMessage:reply(string.format("An error occurred: %s", err))
else
commandMessage:reply(string.format("Game `%s` not found", channel))
end
end
end
})
return true
end
function Module:OnEnable(guild)
local watchedChannels = self:GetWatchedChannels()
local config = self:GetConfig(guild)
for channelId,channelData in pairs(config.TwitchConfig) do
local watchedData = watchedChannels[channelId]
if (not watchedData) then
watchedData = {
LastAlert = 0,
RenewTime = os.time(),
Subscribed = false,
Subscribing = false,
WaitingForConfirm = false
}
watchedChannels[channelId] = watchedData
end
local channelAlerts = self.ChannelAlerts[channelId]
if (not channelAlerts) then
channelAlerts = {}
self.ChannelAlerts[channelId] = channelAlerts
end
channelAlerts[guild.id] = channelData
end
return true
end
function Module:OnDisable(guild)
local watchedChannels = self:GetWatchedChannels()
local config = self:GetConfig(guild)
for channelId,channelData in pairs(config.TwitchConfig) do
local channelAlerts = self.ChannelAlerts[channelId]
if (channelAlerts) then
channelAlerts[guild.id] = nil
end
end
end
function Module:OnUnload()
if (self.Clock) then
self.Clock:stop()
end
self.Server:close()
end
function Module:CreateServer(host, port, onConnect)
return net.createServer({
host = host,
port = port,
encode = httpCodec.encoder(),
decode = httpCodec.decoder(),
}, function (read, write, socket)
for head in read do
local parts = {}
for part in read do
if #part > 0 then
parts[#parts + 1] = part
else
break
end
end
local body = table.concat(parts)
local head, body = onConnect(head, body, socket)
write(head)
if body then write(body) end
write("")
if not head.keepAlive then break end
end
write() --FIXME: This should be done by coro-net
end)
end
local charset = {} do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(charset, string.char(c)) end
for c = 65, 90 do table.insert(charset, string.char(c)) end
for c = 97, 122 do table.insert(charset, string.char(c)) end
end
function Module:GenerateSecret(length)
local res = ""
for i = 1, length do
res = res .. charset[math.random(1, #charset)]
end
return res
end
function Module:SetupServer()
return self:CreateServer("127.0.0.1", self.GlobalConfig.ListenPort, function (head, body, socket)
local Forbidden = function()
self:LogWarning("Twitch server: Access forbidden")
local header = {
code = 403,
{"Content-Type", "charset=utf-8"},
{"Content-Length", 0}
}
return header, ""
end
local Ok = function ()
local header = {
code = 200,
reason = "OK",
{"Content-Type", "charset=utf-8"},
{"Content-Length", 0}
}
return header, ""
end
local ServerError = function ()
self:LogWarning("Twitch server: Server error")
local header = {
code = 500,
{"Content-Type", "charset=utf-8"},
{"Content-Length", 0}
}
return header, ""
end
-- Check signature
local headerSignature
for _, keyvalue in ipairs(head) do
local key, value = unpack(keyvalue)
if (key:lower() == "x-hub-signature") then
headerSignature = value
break
end
end
-- If we have a signature, then a channel came up, else it's twitch checking
if (headerSignature) then
local hash, hashValue = string.match(headerSignature, "(%w+)=(%w+)")
if (hash and hashValue and hash == "sha256") then
headerSignature = hashValue
else
self:LogError("Twitch server: Invalid header signature %s", headerSignature)
return Forbidden()
end
local payload = json.decode(body)
if (payload and payload.data) then
local channelData = payload.data[1]
if (channelData) then
-- Channel up
self:LogInfo("Twitch server: Channel %s went up", channelData.user_id)
bot:CallModuleFunction(self, "HandleChannelUp", channelData, headerSignature, body)
else
-- Channel down
self:LogInfo("Twitch server: A channel went down") -- And ... we have have no idea which channel is concerned, ty twitch
end
end
return Ok()
else
-- Decode path
local query = head.path:match("^[^?]*%??(.*)")
if (query) then
local parameters = querystring.parse(query)
local mode = parameters["hub.mode"]
local twitchTopic = parameters["hub.topic"]
local token = parameters["hub.challenge"]
if (not mode or not twitchTopic or not token) then
self:LogError("Twitch server: Invalid parameters (mode=%s, topic=%s, challenge=%s)", tostring(mode), tostring(twitchTopic), tostring(token))
return Forbidden()
end
local channelId = twitchTopic:match("https://api%.twitch%.tv/helix/streams%?user_id=(%d+)")
if (not channelId) then
self:LogError("Twitch server: Invalid topic \"%s\"", tostring(twitchTopic))
return Forbidden()
end
local watchedChannels = self:GetWatchedChannels()
local channelData = watchedChannels[channelId]
if (not channelData or not channelData.WaitingForConfirm) then
-- May occurs when reloading
if (mode == "unsubscribe") then
return Ok()
end
self:LogError("Twitch server: Channel \"%s\" is not watched", tostring(channelId))
return Forbidden()
end
if (mode == "subscribe") then
local subscribeTime = parameters["hub.lease_seconds"]
if (not subscribeTime) then
self:LogError("Twitch server: Invalid parameter hub.lease_seconds (%s)", tostring(subscribeTime))
return Forbidden()
end
self:LogInfo("Twitch server: Subscribed to %s for %s", channelId, subscribeTime)
channelData.RenewTime = os.time() + subscribeTime
channelData.Subscribed = true
channelData.WaitingForConfirm = false
elseif (mode == "unsubscribe") then
self:LogInfo("Twitch server: Unsubscribed from %s", channelId)
channelData.RenewTime = os.time()
channelData.Subscribed = false
channelData.WaitingForConfirm = false
else
self:LogError("Twitch server: Unknown mode %s", mode)
return ServerError()
end
local header = {
code = 200,
{"Content-Type", "charset=utf-8"},
{"Content-Length", #token}
}
return header, token
end
return Forbidden()
end
end)
end
function Module:HandleChannelUp(channelData, headerSignature, body)
local channelId = channelData.user_id
self:LogInfo("Channel %s went up", channelId)
local watchedChannels = self:GetWatchedChannels()
local watchedData = watchedChannels[channelId]
if (not watchedData) then
self:LogError("%s is not a watched channel, ignoring...", channelId)
return
end
local channelAlerts = self.ChannelAlerts[channelId]
if (not channelAlerts) then
self:LogError("%s has no active alerts, ignoring...", channelId)
return
end
-- Ensure Twitch has sent this
assert(watchedData.Secret)
local signature = sha256.hmac_sha256(watchedData.Secret, body)
if (headerSignature ~= signature) then
self:LogError("Header signature doesn't match")
return
end
local now = os.time()
if (now - watchedData.LastAlert < self.GlobalConfig.SilenceDuration) then
self:LogInfo("Dismissed alert event because last one occured %s ago", util.FormatTime(now - watchedData.LastAlert))
return
end
local startDate = discordia.Date.parseISO(channelData.started_at)
if (watchedData.LastAlert > startDate) then
self:LogInfo("Dismissed alert event because last one occured while the stream was active (%s ago)", util.FormatTime(now - watchedData.LastAlert))
return
end
local profileData, err = self.API:GetUserById(channelId)
if (not profileData) then
self:LogError("Failed to query user %s info: %s", channelId, err)
return
end
local gameData, err = self:GetGameData(channelData.game_id)
if (not gameData) then
self:LogError("Failed to query game info about game %s: %s", channelData.game_id, err)
end
watchedData.LastAlert = now
local title = channelData.title
for guildId, guildPatterns in pairs(channelAlerts) do
local guild = client:getGuild(guildId)
if (guild) then
for _, pattern in pairs(guildPatterns) do
if (not pattern.TitlePattern or title:match(pattern.TitlePattern)) then
local channel = guild:getChannel(pattern.Channel)
if (channel) then
local message = pattern.Message
local nonMentionableRoles = {}
for roleId in message:gmatch("<@&(%d+)>") do
local role = guild:getRole(roleId)
if (role) then
if (not role.mentionable) then
nonMentionableRoles[roleId] = role
end
else
self:LogWarning(guild, "Role %s doesn't exist", roleId)
end
end
local gameName = gameData and gameData.Name or string.format("<game %s>", channelData.game_id)
local fields = {
display_name = profileData.display_name,
game_name = gameName,
title = channelData.title
}
message = message:gsub("{(%w+)}", fields)
local channelUrl = "https://www.twitch.tv/" .. profileData.login
local thumbnail = channelData.thumbnail_url .. "?" .. os.time() -- Prevent Discord cache
thumbnail = thumbnail:gsub("{width}", 320)
thumbnail = thumbnail:gsub("{height}", 180)
for roleId, role in pairs(nonMentionableRoles) do
local success, err = role:enableMentioning()
if (not success) then
self:LogWarning(guild, "Failed to enable mentioning on role %s (%s): %s", roleId, role.name, err)
end
end
local fields = {nil, nil, nil}
if (gameData) then
table.insert(fields, {
name = "Game",
value = gameName
})
end
if (channelData.viewer_count > 0) then
table.insert(fields, {
name = "Viewers",
value = channelData.viewer_count
})
end
table.insert(fields, {
name = "Started",
value = util.FormatTime(now - startDate, 1) .. " ago"
})
channel:send({
content = message,
embed = {
title = channelData.title,
url = channelUrl,
author = {
name = profileData.login,
url = channelUrl,
icon_url = profileData.profile_image_url
},
thumbnail = {
url = profileData.profile_image_url
},
fields = fields,
image = {
url = thumbnail
},
timestamp = channelData.started_at
}
})
for roleId, role in pairs(nonMentionableRoles) do
local success, err = role:disableMentioning()
if (not success) then
self:LogWarning(guild, "Failed to re-disable mentioning on role %s (%s): %s", roleId, role.name, err)
end
end
else
self:LogError(guild, "Channel %s doesn't exist", pattern.Channel)
end
break
end
end
end
end
end
function Module:GetGameData(gameId)
local gameData = self.GameCache[gameId]
if (not gameData) then
local gameInfo, err = self.API:GetGameById(gameId)
if (err) then
return nil, err
end
gameData = {}
if (gameInfo) then
gameData.Id = gameInfo.id
gameData.Image = gameInfo.box_art_url
gameData.Name = gameInfo.name
end
self.GameCache[gameId] = gameData
end
return gameData
end
function Module:SubscribeToTwitch(channelId)
self:LogInfo("Subscribing to channel %s", channelId)
local watchedChannels = self:GetWatchedChannels()
local channelData = watchedChannels[channelId]
assert(channelData)
channelData.Subscribing = true
channelData.WaitingForConfirm = true
local secret = self.Secret
channelData.Secret = secret
local succeeded, ret, err = pcall(function () self.API:SubscribeToStreamUpDown(channelId, self.GlobalConfig.CallbackEndpoint, self.GlobalConfig.SubscribeDuration, secret) end)
channelData.Subscribing = false
if (not succeeded) then
channelData.WaitingForConfirm = false
self:LogError("An error occurred: %s", ret)
return false, ret
end
return ret, err
end
function Module:UnsubscribeFromTwitch(channelId)
self:LogInfo("Unsubscribing to channel %s", channelId)
return self.API:UnsubscribeFromStreamUpDown(channelId, self.GlobalConfig.CallbackEndpoint)
end