forked from Abev08/TwitchBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifications.cs
More file actions
963 lines (847 loc) · 36.8 KB
/
Copy pathNotifications.cs
File metadata and controls
963 lines (847 loc) · 36.8 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
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Serilog;
namespace AbevBot
{
public static class Notifications
{
/// <summary> Path to random videos directory. </summary>
public const string RANDOM_VIDEO_PATH = "Resources/Videos";
/// <summary> Path to directory with sound samples. </summary>
public const string SOUNDS_PATH = "Resources/Sounds";
/// <summary> Notifications thread started. </summary>
public static bool Started { get; private set; }
/// <summary> Are notifications paused? </summary>
public static bool NotificationsPaused { get; set; }
/// <summary> Is "stop follow bots" method active? </summary>
public static bool StopFollowBotsActive { get; set; }
/// <summary> Is notification pause active from "stop follow bots" method? </summary>
public static bool StopFollowBotsPause { get; set; }
public static bool SkipNotification { get; set; }
public static bool ChatTTSEnabled { get; set; }
public static bool WelcomeMessagesEnabled { get; set; }
/// <summary> Queue of notifiactions. </summary>
static readonly List<Notification> NotificationQueue = new();
/// <summary> Collection of past notifications.
/// When notification ends it should be added to this collection and cleared from it after some time. </summary>
static readonly List<Notification> PastNotifications = new();
/// <summary> Collection of maybe notifications - notifications that come from different sources than events.
/// After some time (if correct event message doesn't appear) notifications from this list are moved to correct notification queue. </summary>
static readonly List<Notification> MaybeNotificationQueue = new();
private static Thread NotificationsThread;
public static readonly HttpClient Client = new();
public static string VoicesLink { get; private set; }
private static readonly Dictionary<string, FileInfo> SampleSounds = new();
/// <summary> Recently played list of random videos. </summary>
private static readonly List<string> RandomVideosRecentlyPlayed = new();
private static bool SoundsAvailable;
private static string s_soundsSamplePasteLink;
/// <summary> Supported video formats by WPF MediaElement. </summary>
public static readonly string[] SupportedVideoFormats = new[] { ".avi", ".gif", ".mkv", ".mov", ".mp4", ".wmv" };
public static readonly string[] SupportedAudioFormats = new[] { ".wav", ".mp3", ".ogg" };
public static readonly string[] SupportedImageFormats = new[] { ".png", ".jpg", ".bmp" };
/// <summary> Configurations of different notification types. </summary>
public static readonly Dictionary<string, NotificationsConfig> Configs = new()
{
{"Follow", new(NotificationType.FOLLOW)},
{"Subscription", new(NotificationType.SUBSCRIPTION)},
{"SubscriptionExt", new(NotificationType.SUBSCRIPTION)},
{"SubscriptionGift", new(NotificationType.SUBSCRIPTIONGIFT)},
{"SubscriptionGiftReceived", new(NotificationType.SUBSCRIPTION)},
{"Cheer", new(NotificationType.CHEER)},
{"CheerRange", new(NotificationType.CHEERRANGE)},
{"Raid", new(NotificationType.RAID)},
{"Timeout", new(NotificationType.TIMEOUT)},
{"Ban", new(NotificationType.BAN)},
{"OnScreenCelebration", new(NotificationType.ONSCREENCELEBRATION)}
};
private static readonly string[] NotificationData = new string[14];
public static readonly List<ChannelRedemption> ChannelRedemptions = new();
private static readonly List<(DateTime time, string name)> GiftedSubs = new();
private static readonly TimeSpan GiftSubMaxTimeout = new(0, 0, 10);
public static VideoParameters RandomVideoParameters;
public static readonly List<NotificationsConfig> CheerRangeNotifications = new();
public enum TextPosition { TOPLEFT, TOP, TOPRIGHT, LEFT, CENTER, RIGHT, BOTTOMLEFT, BOTTOM, BOTTOMRIGHT, VIDEOABOVE, VIDEOCENTER, VIDEOBELOW }
public static void Start()
{
if (Started) return;
Started = true;
Log.Information("Starting notifications thread.");
CreateVoicesPaste();
// CreateVoicesResponse();
NotificationsThread = new Thread(Update)
{
Name = "Notifications Thread",
IsBackground = true
};
NotificationsThread.Start();
}
private static void Update()
{
bool notificationEnded = false;
while (true)
{
if (MainWindow.CloseRequested) { return; }
// Clean up past notifications
if (PastNotifications.Count > 0 && DateTime.Now >= PastNotifications[0].RelevanceTime)
{
lock (PastNotifications)
{
// The notification ended more than 30 sec ago, remove it from past notifications collection
PastNotifications.RemoveAt(0);
}
}
// During "stop follow bots" don't update notifications
if (StopFollowBotsPause)
{
Thread.Sleep(500);
continue;
}
// Check maybe notifications
if (MaybeNotificationQueue.Count > 0)
{
if (DateTime.Now >= MaybeNotificationQueue[0].StartAfter)
{
lock (MaybeNotificationQueue)
{
lock (NotificationQueue)
{
// Maybe notification should start - move it to the real queue
NotificationQueue.Add(MaybeNotificationQueue[0]);
MaybeNotificationQueue.RemoveAt(0);
MainWindow.I.SetNotificationQueueCount(NotificationQueue.Count, MaybeNotificationQueue.Count);
}
}
}
else if (NotificationQueue.Count == 0 && SkipNotification)
{
// No real notifications to skip - skip queued maybe notification
lock (MaybeNotificationQueue)
{
SkipNotification = false;
MaybeNotificationQueue.RemoveAt(0);
MainWindow.I.SetNotificationQueueCount(NotificationQueue.Count, MaybeNotificationQueue.Count);
}
}
}
if ((NotificationQueue.Count > 0) && (!NotificationsPaused || NotificationQueue[0].Started))
{
lock (NotificationQueue)
{
if (NotificationQueue[0].Started == false)
{
SkipNotification = false;
NotificationQueue[0].Start();
}
if (NotificationQueue[0].Update())
{
SkipNotification = false;
// Update returned true == notificaion has ended, remove it from queue
lock (PastNotifications)
{
PastNotifications.Add(NotificationQueue[0]);
PastNotifications[^1].RelevanceTime = DateTime.Now.AddSeconds(30);
}
NotificationQueue.RemoveAt(0);
notificationEnded = true;
MainWindow.I.SetNotificationQueueCount(NotificationQueue.Count, MaybeNotificationQueue.Count);
}
}
if (notificationEnded)
{
if (NotificationQueue.Count > 0) Thread.Sleep(500); // 500 ms delay between notifications
notificationEnded = false;
}
else { Thread.Sleep(10); } // Slow down the loop while notification is playing
}
else
{
Thread.Sleep(500); // Check for new notification every 500 ms
}
}
}
/// <summary> Adds notification to the queue. </summary>
public static void AddNotification(Notification notification)
{
Task.Run(() =>
{
lock (NotificationQueue)
{
// Check if maybe notification was created with the same data
lock (MaybeNotificationQueue)
{
for (int i = MaybeNotificationQueue.Count - 1; i >= 0; i--)
{
var n = MaybeNotificationQueue[i];
if (n.Type == notification.Type && n.Sender == notification.Sender)
{
// Notification type and sender name is the same - it has to be the same notification?
MaybeNotificationQueue.RemoveAt(i);
break;
}
}
}
NotificationQueue.Add(notification);
MainWindow.I.SetNotificationQueueCount(NotificationQueue.Count, MaybeNotificationQueue.Count);
}
});
}
/// <summary> Adds notification to the queue. </summary>
public static void AddMaybeNotification(Notification notification)
{
Task.Run(() =>
{
// Check previous / current notifications - maybe the notification was or already is being played
lock (NotificationQueue)
{
for (int i = 0; i < NotificationQueue.Count; i++)
{
var n = NotificationQueue[i];
// Notification type and sender name is the same - it has to be the same notification?
if (n.Type == notification.Type && n.Sender == notification.Sender) { return; }
}
lock (PastNotifications)
{
for (int i = 0; i < PastNotifications.Count; i++)
{
var n = PastNotifications[i];
// Notification type and sender name is the same - it has to be the same notification?
if (n.Type == notification.Type && n.Sender == notification.Sender) { return; }
}
}
}
lock (MaybeNotificationQueue)
{
notification.StartAfter = DateTime.Now.AddSeconds(5); // Lets give it 5 sec, maybe propper event message will come?
MaybeNotificationQueue.Add(notification);
MainWindow.I.SetNotificationQueueCount(NotificationQueue.Count, MaybeNotificationQueue.Count);
}
});
}
/// <summary> Creates and adds to queue Follow notification. </summary>
public static void CreateFollowNotification(string userName)
{
var config = Configs["Follow"];
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Subscription notification. </summary>
public static void CreateSubscriptionNotification(string userName, string tier, string message)
{
var config = Configs["Subscription"];
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[1] = tier[..1];
NotificationData[7] = message;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> This is more advanced CreateSubscriptionNotification version - more info in message variable. </summary>
public static void CreateSubscriptionNotification(string userName, string tier, int duration, int streak, int cumulative, EventPayloadMessage message)
{
var config = Configs["SubscriptionExt"];
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[1] = tier[..1];
NotificationData[2] = duration.ToString();
NotificationData[3] = streak.ToString();
NotificationData[5] = cumulative.ToString();
NotificationData[7] = message.Text; // TODO: Create message to read - remove emotes from the message using message.Emotes[], don't read them
NotificationData[10] = duration > 1 ? "s" : string.Empty;
NotificationData[11] = streak > 1 ? "s" : string.Empty;
NotificationData[13] = cumulative > 1 ? "s" : string.Empty;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> This is more advanced CreateSubscriptionNotification version - more info in message variable. </summary>
public static void CreateSubscriptionNotification(string userName, string tier, int duration, int streak, int cumulative, JsonNode message)
{
var config = Configs["SubscriptionExt"];
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[1] = tier[..1];
NotificationData[2] = duration.ToString();
NotificationData[3] = streak.ToString();
NotificationData[5] = cumulative.ToString();
NotificationData[7] = message["text"]?.ToString(); // TODO: Create message to read - remove emotes from the message using message.Emotes[], don't read them
NotificationData[10] = duration > 1 ? "s" : string.Empty;
NotificationData[11] = streak > 1 ? "s" : string.Empty;
NotificationData[13] = cumulative > 1 ? "s" : string.Empty;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Received Gifted Subscription notification. </summary>
public static void CreateGiftSubscriptionNotification(string userName, string tier, int count, string message, string timeStamp)
{
var config = Configs["SubscriptionGift"];
if (!config.Enable) return;
// Check the gift sub list
if (DateTime.TryParse(timeStamp, out DateTime time))
{
for (int i = GiftedSubs.Count - 1; i >= 0; i--)
{
if (GiftedSubs[i].time - time >= GiftSubMaxTimeout)
{
GiftedSubs.RemoveAt(i);
}
}
}
StringBuilder sb = new();
if (GiftedSubs.Count > 0)
{
for (int i = 0; i < GiftedSubs.Count; i++)
{
sb.Append(GiftedSubs[i].name);
if (i != GiftedSubs.Count - 1) sb.Append(", ");
}
}
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[1] = tier[..1];
NotificationData[4] = count.ToString();
NotificationData[6] = sb.ToString();
NotificationData[7] = message;
NotificationData[12] = count > 1 ? "s" : string.Empty;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
GiftedSubs.Clear();
}
/// <summary> Creates and adds to queue Gifted Subscription notification. </summary>
public static void CreateReceiveGiftSubscriptionNotification(string userName, string timeStamp)
{
var config = Configs["SubscriptionGiftReceived"];
// Add gifted sub to the list
if (userName?.Length > 0 && DateTime.TryParse(timeStamp, out DateTime time))
{
GiftedSubs.Add((time, userName));
}
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Cheer notification. </summary>
public static void CreateCheerNotification(string userName, int count, string message)
{
// Notifications.CheerRangeNotifications
NotificationsConfig config = null;
foreach (var c in CheerRangeNotifications)
{
if (!c.Enable) { continue; }
if (count >= c.BitsRange[0] && count <= c.BitsRange[1])
{
config = c;
break;
}
}
if (config is null) { config = Configs["Cheer"]; }
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[4] = count.ToString();
NotificationData[7] = message;
NotificationData[12] = count > 1 ? "s" : string.Empty;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Channel Points Redemption notification. </summary>
public static void CreateRedemptionNotificaiton(string userName, string redemptionID, string messageID, string message)
{
string msgID = messageID;
if (Config.Data[Config.Keys.ChannelRedemption_RandomVideo_ID].Equals(redemptionID)) { CreateRandomVideoNotification(msgID); }
else if (Config.Data[Config.Keys.ChannelRedemption_SongRequest_ID].Equals(redemptionID))
{
Chat.SongRequest(Chatter.GetChatterByName(userName), message, null, true);
if (Config.Data[Config.Keys.ChannelRedemption_SongRequest_MarkAsFulfilled].Equals("True")) MarkRedemptionAsFulfilled(redemptionID, msgID);
}
else if (Config.Data[Config.Keys.ChannelRedemption_SongSkip_ID].Equals(redemptionID))
{
Spotify.SkipSong();
if (Config.Data[Config.Keys.ChannelRedemption_SongSkip_MarkAsFulfilled].Equals("True")) MarkRedemptionAsFulfilled(redemptionID, msgID);
}
else
{
// Look throught channel redemptions list
foreach (var redemption in ChannelRedemptions)
{
if (redemption.ID.Equals(redemptionID))
{
// Create notification
Array.Clear(NotificationData);
Chat.AddMessageToQueue(redemption.Config.ChatMessage);
AddNotification(new Notification(redemption.Config, NotificationData, redemption)
{
ExtraActionAtStartup = () => { if (redemption.MarkAsFulfilled) MarkRedemptionAsFulfilled(redemption.ID, msgID); }
});
break;
}
}
}
}
/// <summary> Creates and adds to queue TTS notification (mainly for chat messages). </summary>
public static void CreateTTSNotification(string text)
{
if (string.IsNullOrWhiteSpace(text)) return;
AddNotification(new Notification()
{
TextToRead = text.Replace("#", ""), // Remove '#' symbols - they are not allowed in TTS request messages
AudioVolume = Config.VolumeAudio
});
}
public static void CreateRaidNotification(string userName, string userID, int count)
{
var config = Configs["Raid"];
if (!config.Enable) return;
if (count < config.MinimumRaiders) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[4] = count.ToString();
NotificationData[12] = count > 1 ? "s" : string.Empty;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
if (config.DoShoutout) Chat.Shoutout(userID);
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Random Video notification. </summary>
public static void CreateRandomVideoNotification(string messageID)
{
var video = GetRandomVideoNotPlayedRecently();
if (video is null || video.Length == 0) return;
string msgID = messageID;
AddNotification(new Notification()
{
VideoPath = video,
VideoVolume = Config.VolumeVideo,
VideoParams = RandomVideoParameters,
ExtraActionAtStartup = () =>
{
if (Config.Data[Config.Keys.ChannelRedemption_RandomVideo_MarkAsFulfilled].Equals("True"))
{
MarkRedemptionAsFulfilled(Config.Data[Config.Keys.ChannelRedemption_RandomVideo_ID], msgID);
}
}
});
}
/// <summary> Creates and adds to queue Chatter Timeout notification. </summary>
/// <param name="userName">Chatter name that got the timeout</param>
/// <param name="duration">Timeout duration</param>
/// <param name="reason">Timeout reason</param>
public static void CreateTimeoutNotification(string userName, TimeSpan duration, string reason)
{
if (StopFollowBotsActive) { return; } // While stop follow bots is active, don't play these notifications
var config = Configs["Timeout"];
if (!config.Enable) { return; }
if (duration < config.MinimumTime) { return; }
Array.Clear(NotificationData);
NotificationData[0] = userName;
NotificationData[7] = reason;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Chatter ban notification. </summary>
/// <param name="userName">Chatter name that got banned</param>
/// <param name="reason">Ban reason</param>
public static void CreateBanNotification(string userName, string reason)
{
if (StopFollowBotsActive) { return; } // While stop follow bots is active, don't play these notifications
var config = Configs["Ban"];
if (!config.Enable) { return; }
Array.Clear(NotificationData);
NotificationData[0] = userName;
NotificationData[7] = reason;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue Subscription notification. </summary>
public static void CreateMaybeSubscriptionNotification(string userName, string tier, string durationInAdvance, string streak, string cumulativeMonths, string message)
{
var config = Configs["SubscriptionExt"];
if (!config.Enable) return;
string chatter;
if (string.IsNullOrWhiteSpace(userName)) chatter = "Anonymous";
else chatter = userName?.Trim();
int temp;
Array.Clear(NotificationData);
NotificationData[0] = chatter;
NotificationData[1] = tier;
NotificationData[2] = durationInAdvance;
NotificationData[3] = streak;
NotificationData[5] = cumulativeMonths;
NotificationData[7] = message;
if (int.TryParse(durationInAdvance, out temp) && temp > 1) { NotificationData[10] = "s"; }
if (int.TryParse(streak, out temp) && temp > 1) { NotificationData[11] = "s"; }
if (int.TryParse(cumulativeMonths, out temp) && temp > 1) { NotificationData[13] = "s"; }
// Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddMaybeNotification(new Notification(config, NotificationData));
}
/// <summary> Creates and adds to queue on screen celebration notification. </summary>
/// <param name="userName">Chatter name that fired up a celebration</param>
/// <param name="msg">Attached message</param>
public static void CreateOnScreenCelebrationNotification(string userName, string msg)
{
var config = Configs["OnScreenCelebration"];
if (!config.Enable) return;
Array.Clear(NotificationData);
NotificationData[0] = userName;
NotificationData[7] = msg;
Chat.AddMessageToQueue(string.Format(config.ChatMessage, NotificationData));
AddNotification(new Notification(config, NotificationData));
}
private static void CreateVoicesPaste()
{
if (Chat.ResponseMessages.ContainsKey("!voices"))
{
Log.Warning("Couldn't add respoonse to \"{key}\" key - the key is already present in response messages.", "!voices");
return;
}
StringBuilder sb = new();
sb.AppendLine("StreamElements voices:");
StreamElements.AppendVoices(ref sb, 70);
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("TikTok voices:");
TikTok.AppendVoices(ref sb, 70);
GlotPaste paste = new() { Language = "assembly", Title = "TTS Voices" };
paste.Files.Add(new GlotFile() { Name = "TTS Voices", Content = sb.ToString() });
using HttpRequestMessage request = new(HttpMethod.Post, "https://glot.io/api/snippets");
request.Content = new StringContent(paste.ToJsonString(), Encoding.UTF8, "application/json");
string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { Log.Error("{key} response creation failed. {ex}", "!voices", ex); return; }
var response = GlotResponse.Deserialize(resp);
if (response?.Url?.Length > 0)
{
VoicesLink = response.Url.Replace("api/", ""); // Remove "api/" part
}
Chat.ResponseMessages.Add("!voices", ($"TTS Voices: {VoicesLink}", false, new DateTime()));
Log.Information("Added respoonse to \"{key}\" key.", "!voices");
}
private static void CreateVoicesResponse()
{
// Printing all available voices is too much for IRC chat (it would be few messages with word limit)
// For now we can response with links to StreamElements and TikTok classes on GitHub.
// Instead of links to GitHub CreateVoicesPaste() method can be used to generate paste link
if (!Chat.ResponseMessages.ContainsKey("!voices"))
{
Chat.ResponseMessages.Add("!voices", (string.Concat(
"TTS Voices: ",
"StreamElements: ", "https://github.com/Abev08/TwitchBot/blob/main/StreamElements.cs ",
"TikTok: ", "https://github.com/Abev08/TwitchBot/blob/main/TikTok.cs"
), false, new DateTime()));
Log.Information("Added respoonse to \"{key}\" key.", "!voices");
}
else
{
Log.Warning("Couldn't add respoonse to \"{key}\" key - the key is already present in response messages.", "!voices");
}
}
/// <summary> Removes all follow notifications from the queue. </summary>
public static void CleanFollowNotifications()
{
lock (NotificationQueue)
{
for (int i = NotificationQueue.Count - 1; i >= 0; i--)
{
if (NotificationQueue[i].Type == NotificationType.FOLLOW)
{
NotificationQueue[i].Stop(); // Stop the notification if it's currently playing
NotificationQueue.RemoveAt(i);
}
}
MainWindow.I.SetNotificationQueueCount(NotificationQueue.Count, MaybeNotificationQueue.Count);
}
}
public static Dictionary<string, FileInfo> GetSampleSounds()
{
// Get all sound files
Dictionary<string, FileInfo> sounds = new();
DirectoryInfo dir = new(SOUNDS_PATH);
if (dir.Exists)
{
foreach (var file in dir.GetFiles())
{
if (Array.IndexOf(SupportedAudioFormats, file.Extension) >= 0)
{
sounds.Add(file.Name.Replace(file.Extension, "").ToLower(), file);
}
}
}
lock (SampleSounds)
{
bool newSounds = false;
// Check if new sounds are available
// comparasion is needed to know when to update !sounds response paste link
if (sounds.Count != SampleSounds.Count) { newSounds = true; }
else
{
// Check file by file
foreach (var sound in sounds)
{
if (!SampleSounds.TryGetValue(sound.Key, out var val))
{
newSounds = true;
break;
}
else
{
if (sound.Value.Length != val.Length)
{
newSounds = true;
break;
}
}
}
}
if (newSounds)
{
SampleSounds.Clear();
foreach (var sound in sounds) { SampleSounds.Add(sound.Key, sound.Value); }
SoundsAvailable = SampleSounds.Count > 0;
s_soundsSamplePasteLink = null;
}
}
return SampleSounds;
}
public static string GetSampleSoundsResponse()
{
StringBuilder sb = new();
var samples = GetSampleSounds();
foreach (var sample in samples)
{
sb.Append(sample.Key).Append(", ");
}
string result = sb.ToString().Trim();
if (result.EndsWith(',')) return result[..^1];
return result;
}
/// <summary> Creates paste link with sound samples. </summary>
/// <returns>Link to the paste.</returns>
public static string GetSampleSoundsPaste()
{
var samples = GetSampleSounds();
if (s_soundsSamplePasteLink?.Length > 0) return s_soundsSamplePasteLink;
int lineLength = 0;
StringBuilder sb = new();
sb.AppendLine("Sound samples that can be used in TTS messages like: \"!tts -fart\".");
sb.AppendLine("Every sound sample can also be used with \"up\" or \"down\" prefix.");
foreach (var sample in samples)
{
sb.Append(sample.Key).Append(", ");
lineLength += sample.Key.Length + 2;
if (lineLength > 70)
{
sb.AppendLine();
lineLength = 0;
}
}
string result = sb.ToString().Trim();
if (result.EndsWith(',')) result = result[..^1];
GlotPaste paste = new() { Language = "assembly", Title = "Sound samples" };
paste.Files.Add(new GlotFile() { Name = "Sound samples", Content = result });
using HttpRequestMessage request = new(HttpMethod.Post, "https://glot.io/api/snippets");
request.Content = new StringContent(paste.ToJsonString(), Encoding.UTF8, "application/json");
string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { Log.Error("{key} response creation failed. {ex}", "!sounds", ex); return string.Empty; }
var response = GlotResponse.Deserialize(resp);
if (response?.Url?.Length > 0)
{
s_soundsSamplePasteLink = response.Url.Replace("api/", ""); // Remove "api/" part
Log.Information("Created respoonse link to \"{key}\" key.", "!sounds");
return s_soundsSamplePasteLink;
}
else
{
return "something went wrong peepoSad";
}
}
public static bool AreSoundsAvailable()
{
if (!SoundsAvailable) GetSampleSounds(); // try to load the sounds
return SoundsAvailable;
}
/// <summary> Gets all of the random videos in RANDOM_VIDEO_PATH directory and Discord channel. </summary>
/// <returns>List of paths to random videos</returns>
public static List<string> GetRandomVideosAll()
{
// Load random videos
var videos = new List<string>();
DirectoryInfo dir = new(RANDOM_VIDEO_PATH);
if (dir.Exists)
{
foreach (FileInfo file in dir.GetFiles())
{
if (Array.IndexOf(SupportedVideoFormats, file.Extension) >= 0)
{
videos.Add(string.Concat(RANDOM_VIDEO_PATH, "\\", file.Name));
}
}
}
// Get random videos in discord channel
var discordVideos = Discord.GetRandomVideos();
videos.AddRange(discordVideos);
return videos;
}
/// <summary> Gets random videos not played recently. </summary>
/// <returns>List of FileInfo objects describing random videos</returns>
public static List<string> GetRandomVideosToPlay()
{
var videosAll = GetRandomVideosAll();
var videosToPlay = new List<string>();
videosToPlay.AddRange(videosAll);
foreach (var v in RandomVideosRecentlyPlayed)
{
for (int i = videosToPlay.Count - 1; i >= 0; i--)
{
if (v == videosToPlay[i])
{
videosToPlay.RemoveAt(i);
break;
}
}
}
if (videosToPlay.Count == 0)
{
videosToPlay.AddRange(videosAll);
RandomVideosRecentlyPlayed.Clear();
}
return videosToPlay;
}
/// <summary> Gets random video from a pool of videos not played recently. </summary>
/// <returns>Path to random video</returns>
public static string GetRandomVideoNotPlayedRecently()
{
var videos = GetRandomVideosToPlay();
int index = Random.Shared.Next(videos.Count);
if (index < 0 || videos.Count == 0) return null;
var video = videos[index];
RandomVideosRecentlyPlayed.Add(video);
videos.RemoveAt(index);
return video;
}
/// <summary> Marks provided redemption from provided message ID as fulfilled. </summary>
/// <param name="redemptionID">Twitch redemption ID.</param>
/// <param name="messageID">Twitch message ID that created the redemption.</param>
public static void MarkRedemptionAsFulfilled(string redemptionID, string messageID)
{
string uri = string.Concat(
"https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions",
"?broadcaster_id=", Config.Data[Config.Keys.ChannelID],
"&reward_id=", redemptionID,
"&id=", messageID
);
using HttpRequestMessage request = new(HttpMethod.Patch, uri);
request.Content = new StringContent("""{ "status": "FULFILLED" }""", Encoding.UTF8, "application/json");
request.Headers.Add("Authorization", $"Bearer {Secret.Data[Secret.Keys.OAuthToken]}");
request.Headers.Add("Client-Id", Secret.Data[Secret.Keys.CustomerID]);
string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; } // Assume that it worked
catch (HttpRequestException ex) { Log.Error("Marking redemption as fulfilled failed. {ex}", ex); }
}
/// <summary> Activates shield mode. </summary>
public static void ActivateShieldMode()
{
string uri = string.Concat(
"https://api.twitch.tv/helix/moderation/shield_mode",
"?broadcaster_id=", Config.Data[Config.Keys.ChannelID],
"&moderator_id=", Config.Data[Config.Keys.ChannelID]
);
using HttpRequestMessage request = new(HttpMethod.Put, uri);
request.Content = new StringContent("""{"is_active":true}""", Encoding.UTF8, "application/json");
request.Headers.Add("Authorization", $"Bearer {Secret.Data[Secret.Keys.OAuthToken]}");
request.Headers.Add("Client-Id", Secret.Data[Secret.Keys.CustomerID]);
try
{
var resp = Client.Send(request);
if ((int)resp.StatusCode >= 200 && (int)resp.StatusCode < 300) { } // OK
else { Log.Error("Activating shield mode failed. Received error code: {code} ({status})", (int)resp.StatusCode, resp.StatusCode); }
}
catch (HttpRequestException ex) { Log.Error("Activating shield mode failed. {ex}", ex); }
}
}
public enum NotificationType { FOLLOW, SUBSCRIPTION, SUBSCRIPTIONGIFT, CHEER, CHEERRANGE, RAID, REDEMPTION, TIMEOUT, BAN, ONSCREENCELEBRATION }
public class NotificationsConfig
{
public bool Enable { get; set; } = false;
public string ChatMessage { get; set; } = string.Empty;
public string TextToDisplay { get; set; } = string.Empty;
public Notifications.TextPosition TextPosition { get; set; } = Notifications.TextPosition.TOP;
public double TextSize { get; set; }
public string TextToSpeech { get; set; } = string.Empty;
public string SoundToPlay { get; set; } = string.Empty;
public string VideoToPlay { get; set; } = string.Empty;
public VideoParameters VideoParams { get; set; }
public int MinimumRaiders { get; set; } = 10;
public int MinimumBits { get; set; } = 10;
public bool DoShoutout { get; set; }
public NotificationType Type { get; }
public TimeSpan MinimumTime { get; set; } = TimeSpan.MinValue;
public int[] BitsRange { get; set; } = new int[] { 0, 0 };
public NotificationsConfig(NotificationType type) { Type = type; }
public void Reset()
{
Enable = false;
ChatMessage = string.Empty;
TextToDisplay = string.Empty;
TextPosition = Notifications.TextPosition.TOP;
TextSize = 48;
TextToSpeech = string.Empty;
SoundToPlay = string.Empty;
VideoToPlay = string.Empty;
VideoParams?.Reset();
MinimumRaiders = 10;
MinimumBits = 10;
DoShoutout = false;
MinimumTime = TimeSpan.MinValue;
}
}
public class ChannelRedemption
{
public string ID { get; set; }
public NotificationsConfig Config { get; set; } = new(NotificationType.REDEMPTION);
public List<Key> KeysToPress { get; set; } = new();
public KeyActionType KeysToPressType { get; set; } = KeyActionType.PRESS;
public TimeSpan TimeToPressSecondAction { get; set; } = new TimeSpan();
public List<Key> KeysToPressAfterTime { get; set; } = new();
public KeyActionType KeysToPressAfterTimeType { get; set; } = KeyActionType.PRESS;
public bool MarkAsFulfilled { get; set; }
}
public enum KeyActionType { PRESS, TYPE }
}