-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTTSAlertsAndChat_StreamlabsSystem.py
796 lines (671 loc) · 30.4 KB
/
TTSAlertsAndChat_StreamlabsSystem.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Text-To-Speech for Alerts and Chat Messages
1.1.4
Fixed bug adding unicode characters to banned words list
Added setting for ban messages
Added ability to ban users for a time
1.1.3
Fixed bug where banned words showed on overlay
1.1.2
Support ascii characters in overlay
1.1.1
Control overlay position/message, ban users, and set a character limit
1.1.0
Added $tts parameter, text-to-speech overlay, fixed twitch sub
1.0.0
Initial public release
"""
#---------------------------------------
# Script Import Libraries
#---------------------------------------
import os
import codecs
import json
from collections import OrderedDict
import time
import re
import threading
import clr
clr.AddReference("IronPython.Modules.dll")
clr.AddReference('System.Speech')
clr.AddReferenceToFileAndPath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "StreamlabsEventReceiver.dll"))
from System.Speech.Synthesis import SpeechSynthesizer
from StreamlabsEventReceiver import StreamlabsEventClient
#---------------------------------------
# Script Information
#---------------------------------------
ScriptName = "TTS Alerts and Chat"
Website = "https://www.twitch.tv/kruiser8"
Description = "Text-to-speech for streamlabs alerts and chat messages."
Creator = "Kruiser8"
Version = "1.1.4"
#---------------------------------------
# Script Variables
#---------------------------------------
# Socket Receiver
EventReceiver = None
# Settings file location
SettingsFile = os.path.join(os.path.dirname(__file__), "settings.json")
# UI Config file location
UIConfigFile = os.path.join(os.path.dirname(__file__), "UI_Config.json")
# Banned user file
BannedUserFile = os.path.join(os.path.dirname(__file__), "users.txt")
# Banned word file
BannedWordFile = os.path.join(os.path.dirname(__file__), "banned.txt")
# TTS Parser
RegTTS = re.compile(r"\$tts\((?P<message>.*?)\)")
SubPlanMap = {
"Prime": "Prime",
"1000": "Tier 1",
"2000": "Tier 2",
"3000": "Tier 3"
}
#---------------------------------------
# Script Classes
#---------------------------------------
class Settings(object):
""" Load in saved settings file if available else set default values. """
def __init__(self, settingsfile=None):
try:
with codecs.open(settingsfile, encoding="utf-8-sig", mode="r") as f:
self.__dict__ = json.load(f, encoding="utf-8")
except:
self.VoiceName = ""
self.Volume = 80
self.Rate = 0
self.MaxCharacters = 0
self.MaxCharacterMessage = "{user}, your message was too long for text-to-speech."
self.TTSCommand = "!tts"
self.TTSCommandPermission = "Caster"
self.TTSCommandPermissionInfo = ""
self.TTSCommandCost = 500
self.TTSCommandMessage = "{user} says, {message}"
self.TTSCommandUsage = "Stream Chat"
self.TTSCommandUsageReply = False
self.TTSCommandUsageReplyMessage = "{user} you can only use this command from {usage}!"
self.TTSUseCD = False
self.TTSCasterCD = True
self.TTSCooldown = 0
self.TTSOnCooldown = "{user} the command is still on cooldown for {cooldown} seconds!"
self.TTSUserCooldown = 10
self.TTSOnUserCooldown = "{user} the command is still on user cooldown for {cooldown} seconds!"
self.TTSAllChat = False
self.TTSAllChatExcludeCommands = True
self.TTSAllChatMessage = "{user} says, {message}"
self.TTSAllChatUsage = "Stream Chat"
self.TTSAllChatUsageReply = False
self.TTSAllChatUsageReplyMessage = "{user} you can only use this command from {usage}!"
self.TTSOverlayExcludeAlerts = True
self.TTSOverlayMessage = "{user} says, {message}"
self.TTSOverlayTime = 8
self.TTSOverlayFontColor = "rgba(255,255,255,1.0)"
self.TTSOverlayUseFontOutline = False
self.TTSOverlayFontOutline = "rgba(0,0,0,0)"
self.TTSOverlayUseFontShadow = True
self.TTSOverlayFontShadow = "rgba(0,0,0,1.0)"
self.TTSOverlayFontSize = 32
self.TTSOverlayFont = ""
self.TTSOverlayUseBackground = True
self.TTSOverlayBackgroundColor = "rgba(0,0,0,1.0)"
self.TTSOverlayUseBorder = True
self.TTSOverlayBorderColor = "rgba(255,255,255,1.0)"
self.TTSOverlayHorizontalAlign = "center"
self.TTSOverlayVerticalAlign = "center"
self.TTSOverlayAnimateIn = 'fadeIn'
self.TTSOverlayAnimateOut = 'fadeOut'
self.MixerOnFollow = False
self.MixerFollowDelay = 0
self.MixerFollowMessage = "{name} has followed."
self.MixerOnHost = False
self.MixerHostMinimum = 0
self.MixerHostDelay = 0
self.MixerHostMessage = "{name} has hosted you with {amount} viewer{isPlural}."
self.MixerOnSub = False
self.MixerIncludeSubMessage = True
self.MixerSubDelay = 0
self.MixerSubMessage = "{name} has subscribed ({tier})."
self.MixerResubMessage = "{name} has resubscribed ({tier}) for {months} months."
self.StreamlabsOnDonation = False
self.StreamlabsIncludeDonationMessage = True
self.StreamlabsDonationMinimum = 1
self.StreamlabsDonationDelay = 0
self.StreamlabsDonationMessage = "{name} donated {amount}."
self.TwitchOnCheer = False
self.TwitchIncludeCheerMessage = True
self.TwitchCheerMinimum = 100
self.TwitchCheerDelay = 0
self.TwitchCheerMessage = "{name} has used {amount} bit{isPlural}."
self.TwitchOnFollow = False
self.TwitchFollowDelay = 0
self.TwitchFollowMessage = "{name} has followed."
self.TwitchOnHost = False
self.TwitchHostMinimum = 0
self.TwitchHostDelay = 0
self.TwitchHostMessage = "{name} has hosted you with {amount} viewer{isPlural}."
self.TwitchOnRaid = False
self.TwitchRaidMinimum = 0
self.TwitchRaidDelay = 0
self.TwitchRaidMessage = "{name} has raided you with a party of {amount}."
self.TwitchOnSub = False
self.TwitchIncludeSubMessage = True
self.TwitchSubDelay = 0
self.TwitchSubMessage = "{name} has subscribed ({tier})."
self.TwitchResubMessage = "{name} has resubscribed ({tier}) for {months} months."
self.TwitchGiftMessage = "{gifter} has gifted a sub ({tier}) to {name} ({months} month{isPlural})."
self.TwitchGiftMassMessage = "{gifter} has gifted {amount} subs to the channel: {recipients}."
self.YoutubeOnFollow = False
self.YoutubeFollowDelay = 0
self.YoutubeFollowMessage = "{name} has followed."
self.YoutubeOnSub = False
self.YoutubeIncludeSubMessage = True
self.YoutubeSubDelay = 0
self.YoutubeSubMessage = "{name} has subscribed ({tier})."
self.YoutubeResubMessage = "{name} has resubscribed ({tier}) for {months} months."
self.YoutubeOnSuperchat = False
self.YoutubeIncludeSuperchatMessage = True
self.YoutubeSuperchatMinimum = 5
self.YoutubeSuperchatDelay = 0
self.YoutubeSuperchatMessage = "{name} donated {amount}."
self.BanUserCommand = "!banuser"
self.BanUserCommandPermission = "Caster"
self.BanUserCommandPermissionInfo = ""
self.BanUserAddResponse = "The user was banned from using TTS."
self.BanUserResponseResponse = "The user is now able to use TTS."
self.BanWordCommand = "!banword"
self.BanWordCommandPermission = "Caster"
self.BanWordCommandPermissionInfo = ""
self.BanWordAddResponse = "The word was added to the banned words list."
self.BanWordRemoveResponse = "The word was removed from the banned words list."
self.BannedAction = "Skip Messages with Banned Words"
self.BannedActionBoolean = True
self.BannedMatchWholeWord = True
self.BannedReplacement = ""
self.SocketToken = None
def Reload(self, jsondata):
""" Reload settings from Streamlabs user interface by given json data. """
self.__dict__ = json.loads(jsondata, encoding="utf-8")
def Save(self, settingsfile):
""" Save settings contained within to .json and .js settings files. """
try:
with codecs.open(settingsfile, encoding="utf-8-sig", mode="w+") as f:
json.dump(self.__dict__, f, encoding="utf-8", ensure_ascii=False)
with codecs.open(settingsfile.replace("json", "js"), encoding="utf-8-sig", mode="w+") as f:
f.write("var settings = {0};".format(json.dumps(self.__dict__, encoding='utf-8', ensure_ascii=False)))
except:
Parent.Log(ScriptName, "Failed to save settings to file.")
class UIConfig(object):
""" Load in saved settings file if available else set default values. """
def __init__(self, uiconfigfile=None):
try:
with codecs.open(uiconfigfile, encoding="utf-8-sig", mode="r") as f:
self.__dict__ = json.load(f, encoding="utf-8", object_pairs_hook=OrderedDict)
except:
Parent.SendStreamWhisper(Parent.GetChannelName(), "Failed to read UIConfig file: " + str(sys.exc_info()[1]))
def Save(self, uiconfigfile):
""" Save UI Config contained within to .json file. """
if len(self.__dict__) > 0:
try:
with codecs.open(uiconfigfile, encoding="utf-8-sig", mode="w+") as f:
json.dump(self.__dict__, f, encoding="utf-8", ensure_ascii=False)
except:
Parent.SendStreamWhisper(Parent.GetChannelName(), "Failed to save ui config to file.")
#---------------------------------------
# Event Receiver Functions
#---------------------------------------
def EventReceiverConnected(sender, args):
Parent.Log(ScriptName, "Connected")
return
def EventReceiverDisconnected(sender, args):
Parent.Log(ScriptName, "Disconnected")
def EventReceiverEvent(sender, args):
handleEvent(sender,args)
def handleEvent(sender, args):
# Just grab the all data in from the event
evntdata = args.Data
# Check if it contains data and for what streaming service it is
if evntdata and evntdata.For == "twitch_account":
if evntdata.Type == "follow" and ScriptSettings.TwitchOnFollow:
for message in evntdata.Message:
ttsMessage = ScriptSettings.TwitchFollowMessage.format(name=message.Name)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.TwitchFollowDelay)
elif evntdata.Type == "bits" and ScriptSettings.TwitchOnCheer:
s = ''
for message in evntdata.Message:
if message.Amount >= ScriptSettings.TwitchCheerMinimum:
if message.Amount > 1:
s = 's'
else:
s = ''
ttsMessage = ScriptSettings.TwitchCheerMessage.format(name=message.Name, amount=message.Amount, isPlural=s)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.TwitchCheerDelay, ScriptSettings.TwitchIncludeCheerMessage, message.Message, message.Name)
elif evntdata.Type == "host" and ScriptSettings.TwitchOnHost:
s = ''
for message in evntdata.Message:
if int(message.Viewers) >= ScriptSettings.TwitchHostMinimum:
if message.Viewers > 1:
s = 's'
else:
s = ''
ttsMessage = ScriptSettings.TwitchHostMessage.format(name=message.Name, amount=str(message.Viewers), isPlural=s)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.TwitchHostDelay)
elif evntdata.Type == "raid" and ScriptSettings.TwitchOnRaid:
for message in evntdata.Message:
if int(message.Raiders) >= ScriptSettings.TwitchRaidMinimum:
ttsMessage = ScriptSettings.TwitchRaidMessage.format(name=message.Name, amount=str(message.Raiders))
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.TwitchRaidDelay)
elif evntdata.Type == "subscription" and ScriptSettings.TwitchOnSub:
try:
s = ''
if len(evntdata.Message) > 1 and evntdata.Message[0].Gifter:
names = []
for message in evntdata.Message:
names.append(message.Name)
giftees = ', '.join(names)
ttsMessage = ScriptSettings.TwitchGiftMassMessage.format(recipients=giftees, gifter=message.Gifter, amount=len(names))
else:
for message in evntdata.Message:
tier = SubPlanMap[str(message.SubPlan)]
ttsMessage = ''
if message.Gifter:
if message.Months > 1:
s = 's'
else:
s = ''
ttsMessage = ScriptSettings.TwitchGiftMessage.format(name=message.Name, gifter=message.Gifter, tier=tier, months=message.Months, isPlural=s)
else:
if message.Months == 1:
ttsMessage = ScriptSettings.TwitchSubMessage.format(name=message.Name, tier=tier, months=message.Months)
else:
ttsMessage = ScriptSettings.TwitchResubMessage.format(name=message.Name, tier=tier, months=message.Months)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.TwitchSubDelay, ScriptSettings.TwitchIncludeSubMessage, message.Message, message.Name)
except Exception as e:
Parent.SendStreamWhisper(Parent.GetChannelName(), 'Failed to process subscription. Please see logs (i).')
Parent.Log(ScriptName, str(e.args))
elif evntdata and evntdata.For == "mixer_account":
if evntdata.Type == "follow" and ScriptSettings.MixerOnFollow:
for message in evntdata.Message:
ttsMessage = ScriptSettings.MixerFollowMessage.format(name=message.Name)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.MixerFollowDelay)
elif evntdata.Type == "subscription" and ScriptSettings.MixerOnSub:
for message in evntdata.Message:
ttsMessage = ''
if message.Months == 1:
ttsMessage = ScriptSettings.MixerSubMessage.format(name=message.Name, tier=tier, months=message.Months)
else:
ttsMessage = ScriptSettings.MixerResubMessage.format(name=message.Name, tier=tier, months=message.Months)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.MixerSubDelay, ScriptSettings.MixerIncludeSubMessage, message.Message, message.Name)
elif evntdata.Type == "host" and ScriptSettings.MixerOnHost:
s = ''
for message in evntdata.Message:
if int(message.Viewers) >= ScriptSettings.MixerHostMinimum:
if message.Viewers > 1:
s = 's'
else:
s = ''
ttsMessage = ScriptSettings.MixerHostMessage.format(name=message.Name, amount=str(message.Viewers), isPlural=s)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.MixerHostDelay)
elif evntdata and evntdata.For == "streamlabs":
if evntdata.Type == "donation" and ScriptSettings.StreamlabsOnDonation:
for message in evntdata.Message:
if float(message.Amount) >= ScriptSettings.StreamlabsDonationMinimum:
ttsMessage = ScriptSettings.StreamlabsDonationMessage.format(name=message.Name, amount=str(message.FormattedAmount))
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.StreamlabsDonationDelay, ScriptSettings.StreamlabsIncludeDonationMessage, message.Message, message.Name)
elif evntdata and evntdata.For == "youtube_account":
if evntdata.Type == "follow" and ScriptSettings.YoutubeOnFollow:
for message in evntdata.Message:
ttsMessage = ScriptSettings.YoutubeFollowMessage.format(name=message.Name)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.YoutubeFollowDelay)
elif evntdata.Type == "subscription" and ScriptSettings.YoutubeOnSub:
for message in evntdata.Message:
ttsMessage = ''
if message.Months == 1:
ttsMessage = ScriptSettings.YoutubeSubMessage.format(name=message.Name, tier=tier, months=message.Months)
else:
ttsMessage = ScriptSettings.YoutubeResubMessage.format(name=message.Name, tier=tier, months=message.Months)
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.YoutubeSubDelay)
elif evntdata.Type == "superchat" and ScriptSettings.YoutubeOnSuperchat:
for message in evntdata.Message:
if float(message.Amount) >= ScriptSettings.YoutubeSuperchatMinimum:
ttsMessage = ScriptSettings.YoutubeSuperchatMessage.format(name=message.Name, amount=str(message.FormattedAmount))
SendTTSMessagesWithDelay(ttsMessage, ScriptSettings.YoutubeSuperchatDelay, ScriptSettings.YoutubeIncludeSuperchatMessage, message.Message, message.Name)
#---------------------------------------
# Script Functions
#---------------------------------------
def updateUIConfig():
voices = []
for voice in spk.GetInstalledVoices():
info = voice.VoiceInfo
voices.append(info.Name)
UIConfigs = UIConfig(UIConfigFile)
UIConfigs.VoiceName['items'] = voices
if ScriptSettings.VoiceName not in voices:
ScriptSettings.VoiceName = ''
ScriptSettings.Save(SettingsFile)
UIConfigs.Save(UIConfigFile)
def updateBannedSettings():
global ScriptSettings, reBanned, bannedWords, bannedUsers
ScriptSettings.BannedActionBoolean = bool(ScriptSettings.BannedAction == 'Skip Messages with Banned Words')
if ScriptSettings.BannedMatchWholeWord:
reBanned = re.compile(r"\b({0})\b".format('|'.join(bannedWords)), re.IGNORECASE)
else:
reBanned = re.compile(r"({0})".format('|'.join(bannedWords)), re.IGNORECASE)
def SendOverlayUpdate(message):
""" Send updated information to the overlay. """
message = message.encode('utf8', 'replace')
payload = {
'message': message,
'time': ScriptSettings.TTSOverlayTime,
'fontColor': ScriptSettings.TTSOverlayFontColor,
'useOutline': ScriptSettings.TTSOverlayUseFontOutline,
'fontOutline': ScriptSettings.TTSOverlayFontOutline,
'useShadow': ScriptSettings.TTSOverlayUseFontShadow,
'fontShadow': ScriptSettings.TTSOverlayFontShadow,
'fontSize': ScriptSettings.TTSOverlayFontSize,
'font': ScriptSettings.TTSOverlayFont,
'useBackground': ScriptSettings.TTSOverlayUseBackground,
'background': ScriptSettings.TTSOverlayBackgroundColor,
'useBorder': ScriptSettings.TTSOverlayUseBorder,
'border': ScriptSettings.TTSOverlayBorderColor,
'horizontalAlign': ScriptSettings.TTSOverlayHorizontalAlign,
'verticalAlign': ScriptSettings.TTSOverlayVerticalAlign,
'animateIn': ScriptSettings.TTSOverlayAnimateIn,
'animateOut': ScriptSettings.TTSOverlayAnimateOut,
}
Parent.BroadcastWsEvent("EVENT_TTS_AC_OVERLAY", json.dumps(payload))
def SendTTSMessage(voice, message, isAlert, user = '', text = '', displayName = ''):
if user and user in bannedUsers:
return
if user and not text:
text = message
if not isAlert and user and ScriptSettings.MaxCharacters != 0 and len(message) > ScriptSettings.MaxCharacters:
Parent.SendStreamMessage(ScriptSettings.MaxCharacterMessage.format(user=displayName))
return
if ScriptSettings.BannedActionBoolean:
if bool(reBanned.search(message)):
return
else:
message = reBanned.sub(ScriptSettings.BannedReplacement, message)
text = reBanned.sub(ScriptSettings.BannedReplacement, text)
displayName = reBanned.sub(ScriptSettings.BannedReplacement, displayName)
try:
if (isAlert and not ScriptSettings.TTSOverlayExcludeAlerts) or (not isAlert and not user):
SendOverlayUpdate(message)
elif not isAlert:
SendOverlayUpdate(ScriptSettings.TTSOverlayMessage.format(user=displayName, message=text))
voice.Speak(message)
except Exception as e:
Parent.SendStreamWhisper(Parent.GetChannelName(), 'TTS Failed, please see the script logs (i).')
Parent.Log(ScriptName, str(e.args))
def SendTTSMessagesWithDelay(message, delay, includeExtra = False, extraMessage = '', user = ''):
if delay > 0:
time.sleep(delay)
global spk
SendTTSMessage(spk, message, True)
if includeExtra:
SendTTSMessage(spk, extraMessage, False, user)
def readFileArray(fileToRead):
lines = []
with open(fileToRead) as f:
lines = f.readlines()
lines = [x.strip().decode("utf-8", "replace") for x in lines]
return lines
def writeArrayToFile(arrayToWrite, fileToWrite):
with open(fileToWrite, 'w') as f:
f.write('\n'.join(arrayToWrite).encode('utf8', 'replace'))
def handleBanUser(data, user):
global bannedUsers
if user in bannedUsers:
bannedUsers.remove(user)
Parent.SendStreamMessage(ScriptSettings.BanUserRemoveResponse.format(user=data.UserName, banned=user))
else:
bannedUsers.append(user)
Parent.SendStreamMessage(ScriptSettings.BanUserAddResponse.format(user=data.UserName, banned=user))
writeArrayToFile(bannedUsers, BannedUserFile)
#---------------------------------------
# Chatbot Initialize Function
#---------------------------------------
def Init():
# Load settings from file and verify
global ScriptSettings
ScriptSettings = Settings(SettingsFile)
global spk
spk = SpeechSynthesizer()
spk.Rate = ScriptSettings.Rate
spk.Volume = ScriptSettings.Volume
updateUIConfig()
global bannedWords, bannedUsers
bannedUsers = readFileArray(BannedUserFile)
bannedWords = readFileArray(BannedWordFile)
updateBannedSettings()
if ScriptSettings.VoiceName != '':
spk.SelectVoice(ScriptSettings.VoiceName)
# Init the Streamlabs Event Receiver
global EventReceiver
EventReceiver = StreamlabsEventClient()
EventReceiver.StreamlabsSocketConnected += EventReceiverConnected
EventReceiver.StreamlabsSocketDisconnected += EventReceiverDisconnected
EventReceiver.StreamlabsSocketEvent += EventReceiverEvent
# Auto Connect if key is given in settings
if ScriptSettings.SocketToken:
EventReceiver.Connect(ScriptSettings.SocketToken)
# End of Init
return
#---------------------------------------
# Chatbot Save Settings Function
#---------------------------------------
def ReloadSettings(jsondata):
# Reload newly saved settings and verify
ScriptSettings.Reload(jsondata)
updateBannedSettings()
if ScriptSettings.VoiceName != '':
global spk
spk.SelectVoice(ScriptSettings.VoiceName)
spk.Rate = ScriptSettings.Rate
spk.Volume = ScriptSettings.Volume
global EventReceiver
if not EventReceiver.IsConnected and ScriptSettings.SocketToken:
EventReceiver.Connect(ScriptSettings.SocketToken)
elif EventReceiver.IsConnected and not ScriptSettings.SocketToken:
EventReceiver.Disconnect()
# End of ReloadSettings
return
#---------------------------------------
# Chatbot Script Unload Function
#---------------------------------------
def Unload():
global EventReceiver
if EventReceiver.IsConnected:
EventReceiver.Disconnect()
EventReceiver = None
#---------------------------
# [Optional] ScriptToggled (Notifies you when a user disables your script or enables it)
#---------------------------
def ScriptToggled(state):
global EventReceiver
if not state and EventReceiver.IsConnected:
EventReceiver.Disconnect()
elif state and not EventReceiver.IsConnected and ScriptSettings.SocketToken:
EventReceiver.Connect(ScriptSettings.SocketToken)
return
#---------------------------------------
# Chatbot Execute Function
#---------------------------------------
def Execute(data):
if data.IsChatMessage():
command = data.GetParam(0)
if command == ScriptSettings.TTSCommand and IsFromValidSource(data, ScriptSettings.TTSCommandUsage, ScriptSettings.TTSCommandUsageReply, ScriptSettings.TTSCommandUsageReplyMessage):
if HasPermission(data, ScriptSettings.TTSCommandPermission, ScriptSettings.TTSCommandPermissionInfo):
if not IsOnCooldown(data, ScriptSettings.TTSCommand, ScriptSettings.TTSCasterCD, ScriptSettings.TTSUseCD, ScriptSettings.TTSOnCooldown, ScriptSettings.TTSOnUserCooldown):
if HasCurrency(data, ScriptSettings.TTSCommandCost):
commandOffset = len(ScriptSettings.TTSCommand) + 1
text = data.Message[commandOffset:]
message = ScriptSettings.TTSCommandMessage.format(user=data.UserName, message=text)
messageThread = threading.Thread(target=SendTTSMessage, args=(spk, message, False, data.UserName.lower(), text, data.UserName))
messageThread.daemon = True
messageThread.start()
Parent.AddUserCooldown(ScriptName, ScriptSettings.TTSCommand, data.User, ScriptSettings.TTSUserCooldown)
Parent.AddCooldown(ScriptName, ScriptSettings.TTSCommand, ScriptSettings.TTSCooldown)
elif command == ScriptSettings.BanWordCommand and HasPermission(data, ScriptSettings.BanWordCommandPermission, ScriptSettings.BanWordCommandPermissionInfo) and data.GetParamCount() > 1:
message = data.GetParam(1)
i = 2
while i < data.GetParamCount():
message = message + ' ' + data.GetParam(i)
i = i + 1
if message:
global bannedWords
isPhrase = (' ' in message)
if message in bannedWords:
bannedWords.remove(message)
if isPhrase:
Parent.SendStreamMessage(ScriptSettings.BanWordRemoveResponse.format(user=data.UserName, word=message))
else:
Parent.SendStreamMessage(ScriptSettings.BanWordRemoveResponse.format(user=data.UserName, word=message))
else:
bannedWords.append(message)
if isPhrase:
Parent.SendStreamMessage(ScriptSettings.BanWordAddResponse.format(user=data.UserName, word=message))
else:
Parent.SendStreamMessage(ScriptSettings.BanWordAddResponse.format(user=data.UserName, word=message))
writeArrayToFile(bannedWords, BannedWordFile)
updateBannedSettings()
elif command == ScriptSettings.BanUserCommand and HasPermission(data, ScriptSettings.BanUserCommandPermission, ScriptSettings.BanUserCommandPermissionInfo) and data.GetParamCount() > 1:
user = data.GetParam(1).lower()
if user:
handleBanUser(data, user)
if data.GetParamCount() > 2:
time = data.GetParam(2)
if time.isdigit():
banThread = threading.Timer(int(time), handleBanUser, args=(data, user))
banThread.daemon = True
banThread.start()
if ScriptSettings.TTSAllChat and IsFromValidSource(data, ScriptSettings.TTSAllChatUsage, ScriptSettings.TTSAllChatUsageReply, ScriptSettings.TTSAllChatUsageReplyMessage):
if not ScriptSettings.TTSAllChatExcludeCommands or command[0] != '!':
message = ScriptSettings.TTSAllChatMessage.format(user=data.UserName, message=data.Message)
messageThread = threading.Thread(target=SendTTSMessage, args=(spk, message, False, data.UserName.lower(), data.Message, data.UserName))
messageThread.daemon = True
messageThread.start()
# End of execute
return
#---------------------------------------
# Chatbot Execute Helper Functions
#---------------------------------------
def SendResp(data, Message):
"""Sends message to Stream or discord chat depending on settings"""
if not data.IsFromDiscord() and not data.IsWhisper():
Parent.SendStreamMessage(Message)
if not data.IsFromDiscord() and data.IsWhisper():
Parent.SendStreamWhisper(data.User, Message)
if data.IsFromDiscord() and not data.IsWhisper():
Parent.SendDiscordMessage(Message)
if data.IsFromDiscord() and data.IsWhisper():
Parent.SendDiscordDM(data.User, Message)
def IsFromValidSource(data, Usage, SendResponse, UsageResponse):
"""Return true or false depending on the message is sent from
a source that's in the usage setting or not"""
usedDiscord = data.IsFromDiscord()
usedWhisper = data.IsWhisper()
if not usedDiscord:
l = ["Stream Chat", "Chat Both", "All", "Stream Both"]
if not usedWhisper and (Usage in l):
return True
l = ["Stream Whisper", "Whisper Both", "All", "Stream Both"]
if usedWhisper and (Usage in l):
return True
if usedDiscord:
l = ["Discord Chat", "Chat Both", "All", "Discord Both"]
if not usedWhisper and (Usage in l):
return True
l = ["Discord Whisper", "Whisper Both", "All", "Discord Both"]
if usedWhisper and (Usage in l):
return True
if SendResponse:
message = UsageResponse.format(user=data.UserName, usage=Usage)
SendResp(data, message)
return False
def HasPermission(data, permission, permissionInfo):
"""Returns true if user has permission and false if user doesn't"""
if not Parent.HasPermission(data.User, permission, permissionInfo):
return False
return True
def IsOnCooldown(data, command, casterCD, useCD, cooldownMessage, userCooldownMessage):
"""Return true if command is on cooldown and send cooldown message if enabled"""
#introduce globals for cooldown management
cooldown = Parent.IsOnCooldown(ScriptName, command)
userCooldown = Parent.IsOnUserCooldown(ScriptName, command, data.User)
caster = (Parent.HasPermission(data.User, "Caster", "") and casterCD)
#check if command is on cooldown
if (cooldown or userCooldown) and caster is False:
#check if cooldown message is enabled
if useCD:
#set variables for cooldown
cooldownDuration = Parent.GetCooldownDuration(ScriptName, command)
userCDD = Parent.GetUserCooldownDuration(ScriptName, command, data.User)
#check for the longest CD!
if cooldownDuration > userCDD:
#set cd remaining
m_CooldownRemaining = cooldownDuration
#send cooldown message
message = cooldownMessage.format(user=data.UserName, cooldown=m_CooldownRemaining)
SendResp(data, message)
else: #set cd remaining
m_CooldownRemaining = userCDD
#send usercooldown message
message = userCooldownMessage.format(user=data.UserName, cooldown=m_CooldownRemaining)
SendResp(data, message)
return True
return False
def HasCurrency(data, cost):
if (cost == 0) or (Parent.RemovePoints(data.User, data.UserName, cost)):
return True
return False
#---------------------------------------
# Chatbot Tick Function
#---------------------------------------
def Tick():
# End of Tick
return
#---------------------------------------
# Chatbot Parameter Parser
#---------------------------------------
def Parse(parseString, user, target, message):
result = RegTTS.search(parseString)
if result:
paramMessage = result.group(0)
ttsMessage = result.group("message")
parseString = parseString.replace(paramMessage, "")
messageThread = threading.Thread(target=SendTTSMessage, args=(spk, ttsMessage, False))
messageThread.daemon = True
messageThread.start()
# Return unaltered parseString
return parseString
#---------------------------------------
# Chatbot Button Function
#---------------------------------------
def OpenOverlayFolder():
"""Open the overlay folder in the scripts folder"""
os.startfile(os.path.join(os.path.dirname(__file__), "overlay"))
def OpenReadMe():
"""Open the README.txt in the scripts folder"""
os.startfile(os.path.join(os.path.dirname(__file__), "README.txt"))
def OpenBannedWordFile():
"""Open the banned.txt in the scripts folder"""
os.startfile(BannedWordFile)
def OpenBannedUserFile():
"""Open the users.txt in the scripts folder"""
os.startfile(BannedUserFile)
def OpenAnimateDemo():
"""Open Animation Demo Website"""
OpenLink("https://daneden.github.io/animate.css/")
def OpenSocketToken():
"""Open Streamlabs API Settings"""
OpenLink("https://streamlabs.com/dashboard#/settings/api-settings")
def OpenGithubRepository():
"""Open the GitHub Repository for this script"""
OpenLink("https://github.com/kruiser8/TTS-Alerts-And-Chat")
def OpenTwitter():
"""Open the Twitter of the author"""
OpenLink("https://twitter.com/kruiser8")
def OpenLink(link):
"""Open links through buttons in UI"""
os.system("explorer " + link)