forked from DigitalPulseSoftware/NotaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_twitch.lua
904 lines (768 loc) · 24.9 KB
/
module_twitch.lua
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
-- 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 = {}
Module.ProfileCache = {}
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 timer = require("timer")
local twitchAPI = require('./twitchapi.lua')
local function tablesearchstr(tab, val)
for k,v in pairs(tab) do
if (tostring(v) == val) then
return true
end
end
return false
end
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 = {},
ValidateConfig = function (value)
if (type(value) ~= "table" or #value ~= 0) then
return false, "TwitchConfig must be an object"
end
for channelId, notificationData in pairs(value) do
if (not util.ValidateSnowflake(channelId)) then
return false, "TwitchConfig keys must be channel snowflakes"
end
if (type(notificationData) ~= "table" or #notificationData ~= table.count(notificationData)) then
return false, "TwitchConfig[" .. channelId .. "] must be an array"
end
for i, channelData in pairs(notificationData) do
local hasChannel = false
local hasMessage = false
for fieldName, fieldValue in pairs(channelData) do
if (fieldName == "AllowedGames") then
if (type(fieldValue) ~= "table" or #fieldValue ~= table.count(fieldValue)) then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " must be an array"
end
for i, value in pairs(fieldValue) do
if (type(value) ~= "number" or math.floor(value) ~= value) then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. "[" .. i .. "] is not an integer"
end
end
elseif (fieldName == "Channel") then
if (not util.ValidateSnowflake(fieldValue)) then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " must be a channel snowflake"
end
hasChannel = true
elseif (fieldName == "Message") then
if (type(fieldValue) ~= "string") then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " must be a string"
end
hasMessage = true
elseif (fieldName == "TitlePattern") then
if type(fieldValue) == "table" and #fieldValue == table.count(fieldValue) then
for j, value in pairs(fieldValue) do
if type(value) ~= "string" then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. "[" .. j .. "] is not a string"
end
end
elseif type(fieldValue) ~= "string" then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " must be a string or a table of string"
end
elseif (fieldName == "ShouldCreateDiscordEvent") then
if (type(fieldValue) ~= "boolean") then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " must be a boolean"
end
elseif (fieldName == "CreateDiscordEventDuration") then
if (type(fieldValue) ~= "number") then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " must be a number"
end
else
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "]." .. fieldName .. " is not a valid field"
end
end
if (not hasChannel) then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "] is lacking a Channel field"
end
if (not hasMessage) then
return false, "TwitchConfig[" .. channelId .. "][" .. i .. "] is lacking a Message field"
end
end
end
return true
end
},
{
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 = "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()
self.API = twitchAPI(discordia, client, self.GlobalConfig.TwitchClientId, self.GlobalConfig.TwitchClientSecret)
local secretLifespan = 24 * 60 * 60
self.ChannelAlerts = {}
for channelId, channelData in pairs(self:GetWatchedChannels()) do
channelData.WaitingForConfirm = false
end
self.Clock = discordia.Clock()
self.Clock:on("sec", function ()
local watchedChannels = self:GetWatchedChannels()
local now = os.time()
for channelId, channelData in pairs(watchedChannels) do
if (channelData.RenewTime <= now and not channelData.Subscribed) then
local channelAlerts = self.ChannelAlerts[channelId]
if (channelAlerts and not table.empty(channelAlerts)) then
channelData.RenewTime = now + 30 -- Retry in 30 seconds 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
})
self:RegisterCommand({
Name = "twitchstream",
Args = {
{Name = "channel", Type = Bot.ConfigType.String}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Query twitch informations about a ongoing stream",
Func = function (commandMessage, channel)
local channelData, err
if (channel:match("^%d+$")) then
channelData, err = self.API:GetStreamByUserId(channel)
else
channelData, err = self.API:GetStreamByUserName(channel)
end
if (channelData) then
bot:CallModuleFunction(self, "SendChannelNotification", commandMessage.guild, commandMessage.channel, "", channelData)
else
if (err) then
commandMessage:reply(string.format("An error occurred: %s", err))
else
commandMessage:reply(string.format("Stream `%s` not found", channel))
end
end
end
})
return true
end
function Module:OnEnable(guild)
local config = self:GetConfig(guild)
self:HandleConfig(guild, config)
return true
end
function Module:HandleConfig(guild, config)
local watchedChannels = self:GetWatchedChannels()
-- Remove all alerts for this guild before reapplying them
for channelId, channelAlerts in pairs(self.ChannelAlerts) do
channelAlerts[guild.id] = nil
end
for channelId,channelData in pairs(config.TwitchConfig) do
local watchedData = watchedChannels[channelId]
if (not watchedData) then
watchedData = {
LastAlert = 0,
RenewTime = os.time(),
Subscribed = 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
end
function Module:HandleConfigUpdate(guild, config, configName)
if (not configName or configName == "TwitchConfig") then
self:HandleConfig(guild, config)
end
end
function Module:OnDisable(guild)
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,
encoder = httpCodec.encoder,
decoder = 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 headers = {}
for _, keyvalue in ipairs(head) do
local key, value = unpack(keyvalue)
headers[key:lower()] = value
end
local messageType = headers["twitch-eventsub-message-type"]
local headerSignature = headers["twitch-eventsub-message-signature"]
local subscriptionType = headers["twitch-eventsub-subscription-type"]
-- Check signature to ensure it's coming from twitch
if (not messageType) then
self:LogError("Twitch Server: no message type")
return Forbidden()
end
if (not headerSignature) then
self:LogError("Twitch Server: no message signature")
return Forbidden()
end
-- Check hash
local hash, hashValue = string.match(headerSignature, "(%w+)=(%w+)")
if (not hash or not hashValue or hash ~= "sha256") then
self:LogError("Twitch server: Invalid header signature %s", headerSignature)
return Forbidden()
end
local payload = json.decode(body)
-- Retrieve user id from subscription data
local userId = assert(payload.subscription.condition.broadcaster_user_id)
local watchedChannels = self:GetWatchedChannels()
local channelData = watchedChannels[userId]
if (not channelData) then
self:LogError("%s is not a watched channel, ignoring...", userId)
return Forbidden()
end
local hmacMessage = assert(headers["twitch-eventsub-message-id"])
.. assert(headers["twitch-eventsub-message-timestamp"])
.. body
assert(channelData.Secret)
local signature = sha256.hmac_sha256(channelData.Secret, hmacMessage)
if (hashValue ~= signature) then
self:LogError("Hash doesn't match")
return Forbidden()
end
-- Okay message is from Twitch, handle it
if (messageType == "notification") then
self:LogInfo("Twitch server: Received %s notification for channel %s", subscriptionType, userId)
coroutine.wrap(function()
bot:CallModuleFunction(self, "HandleChannelNotification", userId, channelData, subscriptionType, payload.event)
end)()
elseif (messageType == "webhook_callback_verification") then
self:LogInfo("Twitch server: Subscribed to %s for channel %s", subscriptionType, userId)
if (not channelData.WaitingForConfirm) then
self:LogError("Twitch server: Channel \"%s\" is not waiting for Twitch confirmation", tostring(userId))
return Forbidden()
end
channelData.Subscribed = true
channelData.WaitingForConfirm = false
local challenge = payload.challenge
local header = {
code = 200,
{"Content-Type", "charset=utf-8"},
{"Content-Length", #challenge}
}
return header, challenge
elseif (messageType == "revocation") then
self:LogInfo("Twitch server: Unsubscribed from %s for channel %s", subscriptionType, channelId)
channelData.RenewTime = os.time()
channelData.Subscribed = false
channelData.WaitingForConfirm = false
channelData.ChannelUpEventId = nil
else
self:LogError("Twitch server: Unknown messageType %s", messageType)
return ServerError()
end
return Ok()
end)
end
function Module:HandleChannelNotification(channelId, channelData, type, eventData)
if (type == "stream.online") then
local channelAlerts = self.ChannelAlerts[channelId]
if (not channelAlerts) then
self:LogError("%s has no active alerts, ignoring...", channelId)
return
end
local now = os.time()
if (now - channelData.LastAlert < self.GlobalConfig.SilenceDuration) then
self:LogInfo("Dismissed alert event because last one occured %s ago", util.FormatTime(now - channelData.LastAlert))
return
end
local startDate = discordia.Date.parseISO(eventData.started_at)
if (channelData.LastAlert > startDate) then
self:LogInfo("Dismissed alert event because last one occured while the stream was active (%s ago)", util.FormatTime(now - channelData.LastAlert))
return
end
-- There may be a race condition between Twitch notifying a stream started and stream info fetching, try multiple times with a small delay
local streamData, err
for i=1,10 do
self:LogInfo("trying to retrieve stream info for %s (attempt %d/10)", channelId, i)
streamData, err = self.API:GetStreamByUserId(channelId)
if (streamData) then
break
else
if (err) then
self:LogError("couldn't retrieve stream info for %s: %s", channelId, err.msg)
end
timer.sleep(1000)
end
end
if (not streamData) then
return
end
channelData.LastAlert = now
local title = streamData.title
local gameId = streamData.game_id
for guildId, guildPatterns in pairs(channelAlerts) do
local guild = client:getGuild(guildId)
if (guild) then
local function CheckPattern(pattern)
if pattern.TitlePattern then
local doesMatch = false
for _, titlePattern in ipairs(table.wrap(pattern.TitlePattern)) do
if title:match(titlePattern) then
doesMatch = true
break
end
end
if not doesMatch then
return false
end
end
if (pattern.AllowedGames) then
if (not tablesearchstr(pattern.AllowedGames, gameId)) then
return false
end
elseif (pattern.ForbiddenGames) then
if (tablesearchstr(pattern.ForbiddenGames, gameId)) then
return false
end
end
return true
end
for _, pattern in pairs(guildPatterns) do
if (CheckPattern(pattern)) then
local channel = guild:getChannel(pattern.Channel)
if (channel) then
bot:CallModuleFunction(self, "SendChannelNotification", guild, channel, pattern.Message, streamData)
if (pattern.ShouldCreateDiscordEvent) then
bot:CallModuleFunction(self, "CreateScheduledEvent", guild, channelId, title, pattern.CreateDiscordEventDuration or 3600)
end
else
self:LogError(guild, "Channel %s doesn't exist", pattern.Channel)
end
break
end
end
end
end
else
self:LogWarning("unexpected event %s for channel %s", type, channelId)
end
end
function Module:CreateScheduledEvent(guild, channelId, title, duration)
local profileData, err = self:GetProfileData(channelId)
if (not profileData) then
self:LogError("failed to query user %s info: %s", channelData.user_id, err.msg)
return
end
local eventTitle = "🎬 Stream: " .. title
if #eventTitle > 100 then
eventTitle = eventTitle:sub(1, 97) .. "..."
end
local now = os.time()
-- TODO: Add support for images
local eventData = {
entity_type = enums.scheduledEventsEntityTypes.external,
entity_metadata = {
location = "https://twitch.tv/" .. profileData.Name
},
name = eventTitle,
description = profileData.Name .. " is currently streaming!",
privacy_level = enums.scheduledEventsPrivacyLevel.guild_only,
scheduled_start_time = os.date("!%Y-%m-%dT%TZ", now + 5),
scheduled_end_time = os.date("!%Y-%m-%dT%TZ", now + duration),
}
local success, err = guild:createScheduledEvents(eventData)
if not success then
self:LogError(guild, "failed to create scheduled event")
end
end
function Module:GetProfileData(userId)
local now = os.time()
local profileData = self.ProfileCache[userId]
if (not profileData or now - profileData.CachedAt > 3600) then
local userInfo, err = self.API:GetUserById(userId)
if (err) then
return nil, err
end
profileData = {}
if (userInfo) then
profileData.CachedAt = now
profileData.DisplayName = userInfo.display_name
profileData.Name = userInfo.login
profileData.Image = userInfo.profile_image_url
end
self.ProfileCache[userId] = profileData
end
return profileData
end
function Module:GetGameData(gameId)
local now = os.time()
local gameData = self.GameCache[gameId]
if (not gameData or now - gameData.CachedAt > 3600) then
local gameInfo, err = self.API:GetGameById(gameId)
if (err) then
return nil, err
end
gameData = {}
if (gameInfo) then
gameData.CachedAt = now
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:SendChannelNotification(guild, channel, message, channelData)
local profileData, err = self:GetProfileData(channelData.user_id)
if (not profileData) then
self:LogError("Failed to query user %s info: %s", channelData.user_id, err.msg)
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.msg)
end
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)
message = message:gsub("{([%w_]+)}", {
display_name = profileData.DisplayName,
game_name = gameName,
title = channelData.title
})
local channelUrl = "https://www.twitch.tv/" .. profileData.Name
local thumbnail = channelData.thumbnail_url .. "?" .. os.time() -- Bypass Discord image caching
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
local now = os.time()
local startDate = discordia.Date.parseISO(channelData.started_at)
table.insert(fields, {
name = "Started",
value = util.DiscordRelativeTimestamp(startDate)
})
local success, err = channel:send({
content = message,
embed = {
title = channelData.title,
url = channelUrl,
author = {
name = profileData.Name,
url = channelUrl,
icon_url = profileData.Image
},
thumbnail = {
url = profileData.Image
},
fields = fields,
image = {
url = thumbnail
},
timestamp = channelData.started_at
}
})
if (not success) then
self:LogError(guild, "Failed to send twitch notification message: %s", err)
end
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
end
function Module:SubscribeToTwitch(channelId)
self:LogInfo("Subscribing to channel %s", channelId)
local watchedChannels = self:GetWatchedChannels()
local channelData = watchedChannels[channelId]
assert(channelData)
if (channelData.ChannelUpEventId) then
self:UnsubscribeFromTwitch(channelId)
end
channelData.WaitingForConfirm = true
channelData.Secret = self:GenerateSecret(32)
local succeeded, ret, err = pcall(function () return self.API:SubscribeToStreamUp(channelId, self.GlobalConfig.CallbackEndpoint, channelData.Secret) end)
if (not succeeded or not ret) then
channelData.WaitingForConfirm = false
if (err.code == 409) then -- conflict, this subscription already exists
self:LogInfo("subscription already exist")
-- Try to remove subscription
local subscriptions, err = self.API:ListSubscriptions()
if (not subscriptions) then
self:LogError("failed to list current subscriptions: %s", err.msg)
return false, err.msg
end
for _, subscription in pairs(subscriptions.data) do
if (subscription.condition.broadcaster_user_id == channelId) then
self.API:Unsubscribe(subscription.id)
break
end
end
-- Try again
return
end
self:LogError("An error occurred: %s", err.msg)
return false, err.msg
end
channelData.ChannelUpEventId = ret.data.id
return ret, err
end
function Module:UnsubscribeFromTwitch(channelId)
local channelData = watchedChannels[channelId]
if (channelData and channelData.ChannelUpEventId) then
self:LogInfo("Unsubscribing from channel %s", channelId)
return self.API:Unsubscribe(channelData.ChannelUpEventId)
end
end