-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewChatter_StreamlabsSystem.py
More file actions
174 lines (148 loc) · 5.93 KB
/
NewChatter_StreamlabsSystem.py
File metadata and controls
174 lines (148 loc) · 5.93 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name
"""Play a sound if there is a new chatter in the stream."""
#---------------------------------------
# Libraries and references
#---------------------------------------
import codecs
import json
import os
import ctypes
import winsound
#---------------------------------------
# [Required] Script information
#---------------------------------------
ScriptName = "New Chatter Notification"
Website = ""
Creator = "MasterKriff"
Version = "1.1"
Description = "Play a sound if there is a new chatter in the stream."
#---------------------------------------
# Versions
#---------------------------------------
""" Releases (open README.txt for full release notes)
1.1 - Fix for non-UTF characters
1.0 - Initial Release
"""
#---------------------------------------
# Variables
#---------------------------------------
SettingsFile = os.path.join(os.path.dirname(__file__), "settings.json")
UserListFile = os.path.join(os.path.dirname(__file__), "userlist.txt")
MessageBox = ctypes.windll.user32.MessageBoxW
debugLogs = False
#---------------------------------------
# Classes
#---------------------------------------
class Settings:
"""" Loads settings from file if file is found if not uses default values"""
# The 'default' variable names need to match UI_Config
def __init__(self, settingsFile=None):
if settingsFile and os.path.isfile(settingsFile):
with codecs.open(settingsFile, encoding='utf-8-sig', mode='r') as f:
self.__dict__ = json.load(f, encoding='utf-8-sig')
else: #set variables if no custom settings file is found
self.NewChatterSoundLocation = os.path.join(os.path.dirname(__file__), "newchatter.mp3")
self.PlayNewChatterSound = False
self.Volume = 50
self.NewChatterMessage = "Welcome to the stream {0}! <3"
self.SendNewChatterMessage = False
# Reload settings on save through UI
def Reload(self, data):
"""Reload settings on save through UI"""
self.__dict__ = json.loads(data, encoding='utf-8-sig')
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 ValueError:
Parent.Log(ScriptName, "Failed to save settings to file.")
#---------------------------------------
# [OPTIONAL] Settings functions
#---------------------------------------
def BtnResetDefaults():
"""Set default settings function"""
winsound.MessageBeep()
MB_YES = 6
returnValue = MessageBox(0, u"You are about to reset the settings, "
"are you sure you want to contine?"
, u"Reset settings file?", 4)
if returnValue == MB_YES:
MySettings = Settings()
MySettings.Save(SettingsFile)
returnValue = MessageBox(0, u"Settings successfully restored to default values"
, u"Reset complete!", 0)
def BtnTestSound():
"""Test sound"""
PlayNewChatterSound()
def BtnResetUserList():
"""Resets the user list file"""
ResetUserListFile()
MessageBox(0, u"The user list for new chatters has been reset."
, u"User list reset", 0)
def ReloadSettings(jsondata):
"""Reload settings on Save"""
global MySettings
MySettings.Reload(jsondata)
#---------------------------------------
# Base Functions
#---------------------------------------
def DebugLog(msg):
"""Use this function to output debug messages to the chat"""
if debugLogs:
Parent.Log(ScriptName, msg)
def ResetUserListFile():
"""Resets the user list file"""
with open(UserListFile, "w") as f:
f.write("")
DebugLog("User list file reset")
def IsUsernameInList(username):
"""Returns true if user is in the user list file"""
with open(UserListFile, "r") as f:
for line in f.read().decode('utf-8').splitlines():
if line.strip() == username:
return True
DebugLog("User \"{0}\" NOT found in user list".format(username))
return False
def AddUserToList(username):
"""Add username to the user list file"""
encodedUsername = username.encode('utf-8')
with open(UserListFile, "a") as f:
DebugLog("Adding \"{0}\" to user list".format(encodedUsername))
f.write("{0}\n".format(encodedUsername))
def SendChatMessage(message):
"""Required SendChatMessage function"""
Parent.SendStreamMessage(message)
DebugLog("Sent message: {0}".format(message))
def PlayNewChatterSound():
"""Plays a sound when a new chatter joins the stream"""
if os.path.isfile(MySettings.NewChatterSoundLocation):
Parent.PlaySound(MySettings.NewChatterSoundLocation, MySettings.Volume*0.01)
else:
winsound.MessageBeep()
#---------------------------------------
# [Required] functions
#---------------------------------------
def Init():
"""data on Load, required function"""
global MySettings
MySettings = Settings(SettingsFile)
ResetUserListFile()
def Execute(data):
"""Required Execute data function"""
streamerName = Parent.GetChannelName()
userName = data.UserName
if userName == streamerName or IsUsernameInList(userName):
return
if MySettings.NewChatterSoundLocation and MySettings.PlayNewChatterSound:
PlayNewChatterSound()
if MySettings.NewChatterMessage and MySettings.SendNewChatterMessage:
msg = MySettings.NewChatterMessage.format(userName)
SendChatMessage(msg)
AddUserToList(userName)
def Tick():
"""Required tick function"""