-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendSounds_StreamlabsSystem.py
327 lines (258 loc) · 9.54 KB
/
SendSounds_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
#!/usr/bin/env python
# encoding: utf-8
# ---------------------------
# Import Libraries
# ---------------------------
import sys, os, codecs, json, clr, re
clr.AddReference("IronPython.SQLite.dll")
clr.AddReference("IronPython.Modules.dll")
sys.path.append(os.path.join(os.path.dirname(__file__), './SendSounds-Classes'))
sys.path.append(os.path.join(os.path.dirname(__file__), './SendSounds-Database'))
from collections import deque
from random import randint
from SendSounds_LoggerClass import Logger as log
from SendSounds_DatabaseClass import Database as db
global scriptLogger, startMsg, streamer
scriptLogger = log('script')
streamer = ''
# ---------------------------
# [Required] Script Information
# ---------------------------
ScriptName = scriptLogger.scriptName
Website = 'http://rebrand.ly/vonWebsite'
Description = 'Sends the sound files added to Streamlabs Chatbot to an Overlay, simulating a virtual input'
Creator = 'von_Schappler'
Version = scriptLogger.version
# ---------------------------
# Define Global Variables
# ---------------------------
global scriptSettingsFile, scriptSettings, dbSettingsFile, soundsDB, guiFile
scriptSettingsFile = os.path.join(os.path.dirname(__file__), 'SendSounds_Settings.json')
dbSettingsFile = os.path.join(os.path.dirname(__file__), './SendSounds-Classes/SendSounds_dbSettings.json')
guiFile = os.path.realpath(os.path.join(os.path.dirname(__file__), './SendSounds-GUI/SendSounds_App.py'))
# ---------------------------
# [Required] Initialize Data (Only called on load)
# ---------------------------
def Init():
global startMsg, soundsDB, sndGlobalVolume, saveScriptLogs, sndFolderSFX, sndCommandTrigger
global triggerList, volumeList, pathList, msgList, sendAsList, commandList, sfxQ, streamer
try:
streamer = Parent.GetChannelName()
startMsg = scriptLogger.logInit()
sfxQ = deque()
with codecs.open(scriptSettingsFile, mode='r', encoding='utf-8-sig') as f:
scriptSettings = json.load(f)
sndGlobalVolume = scriptSettings['sndGlobalVolume']
saveScriptLogs = scriptSettings['saveScriptLogs']
sndFolderSFX = scriptSettings['sndFolderSFX']
sndCommandTrigger = scriptSettings['sndCommandTrigger']
soundsDB = db(dbSettingsFile, scriptSettingsFile)
startDBMsg = soundsDB.createTable()
if saveScriptLogs:
printLog(startDBMsg)
triggerList = soundsDB.getInfoDB('sndTrigger')
volumeList = soundsDB.getInfoDB('sndVolume')
pathList = soundsDB.getInfoDB('sndFilePath')
msgList = soundsDB.getInfoDB('sndSendMsg')
sendAsList = soundsDB.getInfoDB('sndSendAs')
commandList = ['play', 'opendb', 'reload', 'list']
except Exception as e:
msg = '({script} - {version}) Failed to start...'
msg += '({script} - {version}) - Error message: {err}'
msg = msg.format(script=ScriptName, version=Version, err=e)
Parent.SendStreamMessage(msg)
return
# ---------------------------
# [Required] Execute Data / Process messages
# ---------------------------
def Execute(data):
global sndCommandTrigger, triggerList, saveScriptLogs
if data.IsChatMessage() and data.IsFromTwitch():
chatMsg = data.Message
user = data.User
if chatMsg.startswith(sndCommandTrigger):
sndTrigger = chatMsg.replace(sndCommandTrigger, '')
logMsg = '[{time}] (INF) - "{trigger}" was triggered by {user}'
if sndTrigger in triggerList:
logMsg += '\n[{time}] (SUC) - "{trigger}" is being sent to the overlay'
RunCommand(user, 'play', sndTrigger)
else:
logMsg += '\n[{time}] (ERR) - "{trigger}" could not be sent to the overlay'
logMsg = logMsg.format(
time=scriptLogger.getTime(), trigger=sndTrigger, user=user)
if saveScriptLogs:
printLog(logMsg)
return
# ---------------------------
# [Required] Tick method (Gets called during every iteration even when there is no incoming data)
# ---------------------------
def Tick():
global sfxQ, sfxDuration
if len(sfxQ) > 0:
payload = sfxQ[0]
sfx = payload['sfx']
toPlay = payload['audio']
if Parent.PlaySound(toPlay, 0):
Parent.BroadcastWsEvent("EVENT_SENDSOUNDS", json.dumps(payload))
SendChat(sfx)
soundsDB.updateUseCount(sfx)
sfxQ.popleft()
return
# ---------------------------
# [Optional] Parse method (Allows you to create your own custom $parameters)
# ---------------------------
def Parse(parseString, userid, username, targetid, targetname, message):
if '$sendsounds' in parseString:
parseString = parseString.lower()
regex = re.search(r'\$sendsounds\((.*)\)', parseString)
if regex:
if len(regex.group(1).split(' ')) > 1:
command = regex.group(1).split(' ')[0]
sound = regex.group(1).split(' ')[1]
RunCommand(username, command, sound)
else:
command = regex.group(1).split(' ')[0]
RunCommand(username, command)
return parseString.replace(regex.group(0), "")
return parseString
# ---------------------------
# [Optional] Reload Settings (Called when a user clicks the Save Settings button in the Chatbot UI)
# ---------------------------
def ReloadSettings():
global scriptLogger
scriptLogger.logEnd()
Init()
return
# ---------------------------
# [Optional] Unload (Called when a user reloads their scripts or closes the bot / cleanup stuff)
# ---------------------------
def Unload():
global scriptLogger
scriptLogger.logEnd()
return
# ---------------------------
# [Optional] ScriptToggled (Notifies you when a user disables your script or enables it)
# ---------------------------
def ScriptToggled(state):
global streamer
if state:
status = 'enabled'
else:
status = 'disabled'
msg = '/me : @{streamer}, ({script} - {version}) is {status}...'
msg = msg.format(streamer=streamer, script=ScriptName, version=Version, status=status)
SendInfo(streamer, 'whisper', msg)
return
# ---------------------------
# Script Functions
# ---------------------------
def printLog(msg):
global scriptLogger
scriptLogger.logWrite(msg)
return
def RunCommand(user, command, *args):
global sfxQ, streamer
if command in commandList:
if command == 'play':
sfxQ.append(SendAudioToOverlay(args[0]))
if command == 'opendb':
OpenGUI()
if command == 'reload':
ReloadSettings()
if command == 'list':
PrintSoundList(user)
return
def SendInfo(user, mode, msg):
if mode == 'whisper':
Parent.SendStreamWhisper(user, msg)
elif mode == 'chat':
Parent.SendStreamMessage(msg)
elif mode == 'both':
Parent.SendStreamMessage(msg)
Parent.SendStreamWhisper(user, msg)
return
def SendAudioToOverlay(sfx):
global payLoad
index = triggerList.index(sfx)
payload = {
'sfx': sfx,
"audio": pathList[index],
"volume": (sndGlobalVolume * volumeList[index])/100
}
return payload
def SendChat(sfx):
index = triggerList.index(sfx)
toSend = msgList[index]
sendAs = sendAsList[index]
msg = ''
if sendAs == 'Chat':
msg = '{toSend}'
elif sendAs == 'Announcement (/announce)':
msg = '/announce {toSend}'
elif sendAs == 'Action (/me)':
msg = '/me : {toSend}'
elif sendAs == 'Announcement+Action (/announce /me)':
msg = '/announce /me : {toSend}'
msg = msg.format(toSend=toSend)
Parent.SendStreamMessage(msg)
return
def PrintSoundList(user):
msg = '/me : @{user} , those are the sounds you can pick from:'
msg = msg.format(user=user)
list = GetList()
while len(list) != 0:
if len(list) > 0:
msg += '\n/me : {list}'.format(list=list[0])
list.pop(0)
else:
list = []
msg += '\n/me : in order to play any of the sounds, use {trigger} followed by the sound name to be played. Try out for example {trigger}{sound} on chat and enjoy!'
msg = msg.format(trigger=sndCommandTrigger, sound=triggerList[randint(0, len(triggerList))])
Parent.SendStreamMessage(msg)
return
def GetList():
global triggerList
msg = ''
listToProcess = []
for trigger in triggerList:
msg += '{trigger}, '.format(trigger=trigger)
msg = msg[:-2]
while len(msg) != 0:
listToProcess.append(msg[0:500])
msg = msg[500:]
return listToProcess
# ---------------------------
# Streamlabs Chatbot GUI buttons functions
# ---------------------------
def OpenReadMe():
ReadMe = 'https://github.com/vonschappler/SLCB-SendSounds#readme'
os.startfile(ReadMe)
return
def OpenUserGuide():
Guide = 'https://github.com/vonschappler/SLCB-SendSounds/wiki/User-Guide'
os.startfile(Guide)
return
def OpenGUI():
global guiFile
command = 'python \"{file}"'.format(file=guiFile)
os.system(command)
return
def OpenDiscord():
Discord = "http://rebrand.ly/vonDiscord"
os.startfile(Discord)
return
def OpenReleases():
Release = "https://github.com/vonschappler/SLCB-SendSounds/releases"
os.startfile(Release)
return
def OpenDonation():
PayPal = 'https://rebrand.ly/vonPayPal'
os.startfile(PayPal)
return
def OpenTwitch():
Twitch = "http://rebrand.ly/vonTwitch"
os.startfile(Twitch)
return
def OpenSite():
os.startfile(Website)
return