-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomBeer_StreamlabsSystem.py
More file actions
297 lines (243 loc) · 13.1 KB
/
RandomBeer_StreamlabsSystem.py
File metadata and controls
297 lines (243 loc) · 13.1 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#---------------------------
# Import Libraries
#---------------------------
import os
import sys
import json
import random
import time
from datetime import datetime
sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) #point at lib folder for classes / references
import clr
clr.AddReference("IronPython.SQLite.dll")
clr.AddReference("IronPython.Modules.dll")
# Import your Settings class
from Settings_Module import MySettings
#---------------------------
# [Required] Script Information
#---------------------------
ScriptName = "RandomBeer"
Website = "https://twitch.tv/rialDave/"
Description = "Gifts a beer 'currency' to any random user in the chat and counts how many beers one user has got yet."
Creator = "rialDave"
Version = "0.5.0-dev"
#---------------------------
# Define Global Variables
#---------------------------
SettingsPath = ""
ScriptSettings = MySettings()
beerFilename = os.path.join('data', 'beerdata.json')
beerFilepath = os.path.join(os.path.dirname(__file__), beerFilename)
# Configuration of keys in json file
JSONVariablesBeercountToday = 'beercounttoday'
JSONVariablesBeercountOverall = 'beercountoverall'
JSONVariablesLastbeer = 'lastbeer'
JSONVariablesDrunkleveltoday = 'drunkleveltoday'
# syntax: drunklevel-id: max. beercount for this drunklevel
# known issue: this is checked too late in the code atm: e.g. the user will be able to get 4 instead of 3 beer when having drunklevel 1
VariablesDrunklevel = {
"1": 3,
"2": 5,
"3": 10
}
#---------------------------
# [Required] Initialize Data (Only called on load)
#---------------------------
def Init():
# Create Settings Directory
directory = os.path.join(os.path.dirname(__file__), "Settings")
if not os.path.exists(directory):
os.makedirs(directory)
# Checks if beerfile exists, if it doesnt: creates it
if os.path.isfile(beerFilepath) == 0:
data = {}
with open(beerFilepath, 'w') as f:
json.dump(data, f, indent=4)
# Load settings
SettingsFile = os.path.join("Settings", "settings.json")
SettingsPath = os.path.join(os.path.dirname(__file__), SettingsFile)
ScriptSettings = MySettings(SettingsPath)
ScriptSettings.Response = "Overwritten file!"
return
#---------------------------
# [Required] Execute Data / Process messages
#---------------------------
def Execute(data):
if data.IsChatMessage() and data.GetParam(0).lower() == ScriptSettings.Command and Parent.IsOnUserCooldown(ScriptName,ScriptSettings.Command,data.User):
Parent.SendStreamMessage("Command on cooldown! Seconds remaining: " + str(Parent.GetUserCooldownDuration(ScriptName,ScriptSettings.Command,data.User)))
# Check if the propper command is used, the command is not on cooldown to use the command
if data.IsChatMessage() and data.GetParam(0).lower() == ScriptSettings.Command and not Parent.IsOnUserCooldown(ScriptName,ScriptSettings.Command,data.User):
Parent.BroadcastWsEvent("EVENT_MINE","{'show':false}")
ParsedResponse = Parse(ScriptSettings.Response) # Parse response first
Parent.SendStreamMessage(ParsedResponse) # Send your message to chat
Parent.AddUserCooldown(ScriptName,ScriptSettings.Command,data.User,ScriptSettings.Cooldown) # Put the command on cooldown
return
#---------------------------
# [Required] Tick method (Gets called during every iteration even when there is no incoming data)
# I don't really know what this does, but seems to be required to at least have an empty function defined.
#---------------------------
def Tick():
return
#---------------------------
# [Optional] Parse method (Allows you to create your own custom $parameters)
# Here's where the magic happens, all the strings are sent and processed through this function
#
# Parent.FUNCTION allows to use functions of the Chatbot and other outside APIs (see: https://github.com/AnkhHeart/Streamlabs-Chatbot-Python-Boilerplate/wiki/Parent)
#
# ORIGINAL DEF: def Parse(parseString, userid, username, targetid, targetname, message):
#---------------------------
def Parse(parseString):
# get a random active user from chat and afterwards their displayname
randUser = Parent.GetRandomActiveUser()
randUsername = Parent.GetDisplayName(randUser)
# just a fallback, luckily this didn't happen while being live too often yet
if randUsername == "":
return "DaveDebug: generated random username was an empty string again, what the heck?"
if IsDrunkToday(randUsername) == False:
# Randombeer variable called
if "$randomuser" in parseString:
# Adds a new Beer for given Username
AddBeerForUsername(randUsername)
# Replacing the variable "$randomuser" of the configured command text with the correct username which got the new beer
parseString = parseString.replace("$randomuser", str(randUsername))
# Beercountoverall variable for "overall" called, this sets the overall beercount of the randomuser which was called here
if "$beercountoverall" in parseString:
beerCount = GetBeerCountForUsernameAndType(randUsername, JSONVariablesBeercountOverall)
# if it's the very first beer for user overall, use a different return text (hardcoded). If not: Replaces the string with the correct localization text for "beerCount"
if beerCount == 1:
parseString = "Congratulations! That's " + randUsername + "'s very first beer ever!"
else:
parseString = parseString.replace("$beercountoverall", GetCountLocalization(beerCount))
# Beercounttoday variable for "today" called, this sets the todays beercount for the random user which was called here
if "$beercounttoday" in parseString:
beerCount = GetBeerCountForUsernameAndType(randUsername, JSONVariablesBeercountToday)
parseString = parseString.replace("$beercounttoday", GetCountLocalization(beerCount))
else:
if GetDrunkLevel(randUsername) == 1:
parseString = "Oh shit " + randUsername + ", you probably didn't eat a Schweinsbron before drinking - he's already drunk today!"
if GetDrunkLevel(randUsername) == 2:
parseString = randUsername + ", you should stop the fun now, you've got to work tomorrow!"
if GetDrunkLevel(randUsername) == 3:
parseString = "Already 5 Maß?! Are you crazy " + randUsername + "? Now I understand why you can't walk straight anymore."
# after every necessary variable was processed: return the whole parseString
return parseString
#---------------------------
# [Optional] Reload Settings (Called when a user clicks the Save Settings button in the Chatbot UI)
#---------------------------
def ReloadSettings(jsonData):
# Execute json reloading here
ScriptSettings.__dict__ = json.loads(jsonData)
ScriptSettings.Save(SettingsPath)
return
#---------------------------
# [Optional] Unload (Called when a user reloads their scripts or closes the bot / cleanup stuff)
#---------------------------
def Unload():
return
#---------------------------
# [Optional] ScriptToggled (Notifies you when a user disables your script or enables it)
#---------------------------
def ScriptToggled(state):
return
#---------------------------
# Own Functions: ModifyBeerFile: Function for Modfiying the file which contains the beer guys and according counters, see data/beerdata.json
#---------------------------
def AddBeerForUsername(username):
currenttimestamp = int(time.time())
currentday = datetime.fromtimestamp(currenttimestamp).strftime('%Y-%m-%d')
# this loads the data of the beer file beerdata.json into variable "data"
with open(beerFilepath, 'r') as f:
data = json.load(f)
# check if the given username exists in data. -> user doesnt exist yet, create array of the user data, which will be stored in beerdata.json
if str(username.lower()) not in data:
data[str(username.lower())] = {}
data[str(username.lower())][JSONVariablesBeercountToday] = 1
data[str(username.lower())][JSONVariablesBeercountOverall] = 1
data[str(username.lower())][JSONVariablesLastbeer] = currentday
data[str(username.lower())][JSONVariablesDrunkleveltoday] = GetRandomDrunkLevel()
# if the user already exists, update the user with added beercounts, but we need to check here if it's the first beer today or not to set the right values
else:
# new day since last beer? -> only count beercountoverall up, set beercounttoday to 1 again because it's a new day to start.
if currentday != data[str(username.lower())][JSONVariablesLastbeer]:
data[str(username.lower())][JSONVariablesBeercountToday] = 1
data[str(username.lower())][JSONVariablesBeercountOverall] += 1
data[str(username.lower())][JSONVariablesLastbeer] = currentday
data[str(username.lower())][JSONVariablesDrunkleveltoday] = GetRandomDrunkLevel()
# same day since last beer? -> count both up since we have still the same day since the last beer for the given user
else:
if IsDrunkToday(username) == False:
data[str(username.lower())][JSONVariablesBeercountToday] += 1
data[str(username.lower())][JSONVariablesBeercountOverall] += 1
data[str(username.lower())][JSONVariablesLastbeer] = currentday
# after everything was modified and updated, we need to write the stuff from our "data" variable to the beerdata.json file
os.remove(beerFilepath)
with open(beerFilepath, 'w') as f:
json.dump(data, f, indent=4)
return
#---------------------------
# Own Functions: GetBeerCountForUsernameAndType: Returns the current beer count of a specific type (today or overall) as int
# Params: username, beercounttype (JSONVariablesBeercountOverall or JSONVariablesBeercountToday)
#---------------------------
def GetBeerCountForUsernameAndType(username, beercounttype):
# reads the beerdata.json into "data" variable again
with open(beerFilepath, 'r') as f:
data = json.load(f)
# if the given username doesn't exist in data, return error. Else return the value of the beercount for this user.
# specialty: the submitted param "beercounttype" needs to be one of the examples in function description (also defined as global variables in the beginning of the script)
# so it matches with the array key and returns the correct value
# (this is just for saving some lines of code since it's more intelligent)
if str(username.lower()) not in data:
Parent.Log('Error', 'Oh shit, something went wrong when getting the beercount.')
else:
return data[str(username.lower())][beercounttype]
#---------------------------
# Own Functions: GetCountLocalization: Returns "first", "second" and "third" instead of 1., 2., 3. if none are mapping, then it just outputs the given number again
#
# returns: checked or translated number as a string
#---------------------------
def GetCountLocalization(beerCounter):
# prepend "th" string to the beercount number if its higher than 3
if beerCounter > 3:
return str(beerCounter) + "th"
# build mapping for the first three numbers to be readable.
beerCounterMapping = ["first", "second", "third"]
# since the previously created array matches with the keys 0, 1 and 2 it can be directly used when subtracting integer value 1 from the key.
return str(beerCounterMapping[int(beerCounter) - 1])
#---------------------------
# Own Functions: GetRandomDrunkLevel
#
# returns: int drunklevel
#---------------------------
def GetRandomDrunkLevel():
return random.randrange(1, 4)
#---------------------------
# Own Functions: GetDrunkLevel
#
# returns: int drunklevel of given username
#---------------------------
def GetDrunkLevel(username):
# reads the beerdata.json into "data" variable again
with open(beerFilepath, 'r') as f:
data = json.load(f)
return data[str(username.lower())][JSONVariablesDrunkleveltoday]
#---------------------------
# Own Functions: IsDrunkToday
#
# returns: Boolean if the user is drunk or not
#---------------------------
def IsDrunkToday(username):
# reads the beerdata.json into "data" variable again
with open(beerFilepath, 'r') as f:
data = json.load(f)
if str(username.lower()) in data:
if (JSONVariablesDrunkleveltoday in data[str(username.lower())]):
currenttimestamp = int(time.time())
currentday = datetime.fromtimestamp(currenttimestamp).strftime('%Y-%m-%d')
if (currentday == data[str(username.lower())][JSONVariablesLastbeer]):
beercountToday = GetBeerCountForUsernameAndType(username, JSONVariablesBeercountToday)
drunklevelToday = data[str(username.lower())][JSONVariablesDrunkleveltoday]
if (beercountToday > VariablesDrunklevel[str(drunklevelToday)]):
return True
return False