-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounterModule.py
More file actions
232 lines (211 loc) · 11.3 KB
/
CounterModule.py
File metadata and controls
232 lines (211 loc) · 11.3 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
# -*- coding: utf-8 -*-
#region importing custom modules ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import CBE
import json
import string
import os
#endregion
# necessary
Counter_List = {}
Counter_File = "CSWcounters.json"
Counter_Formatted_File = "CSWcounters.txt"
# load UI
CBE.LoadUI("ChatbotCounterModule_UI.json")
# backup command name
_cmdName = ""
def CounterCheckConfig():
tempDict = {}
tempDict["Counter_CMDName"] = "counters"
tempDict["Counter_CMDCooldown"] = 10
tempDict["Counter_CMDIsGlobal"] = True
tempDict["Counter_CMDUsage"] = "!counters [add <countername> [format]|delete <countername> confirm]"
tempDict["Counter_CMDDescription"] = "Shows all available commands"
tempDict["Counter_String_CounterJoin"] = ", "
tempDict["Counter_CMDModifyPermissions"] = "moderator"
tempDict["Counter_String_AvailableCounters"] = "Currently available counters: $list"
tempDict["Counter_CounterCooldown"] = 10
tempDict["Counter_String_AddedCounter"] = "Added counter !$counter"
tempDict["Counter_String_DeletedCounter"] = "Deleted counter !$counter"
tempDict["Counter_String_Help"] = "!$counter [+ [<n>]|- [<n>]|set <n>] (Cooldown $cooldown seconds)"
tempDict["Counter_String_WholeNumber"] = "You have to provide a whole number"
tempDict["Counter_String_AlreadyExists"] = "This counter already exists"
tempDict["Counter_String_CommandExists"] = "There is already a command with this name"
tempDict["Counter_String_NonASCII"] = "The counter name may only contain ascii-letters from a-z"
tempDict["Counter_String_ConfirmNeeded"] = "You have to confirm the deletion"
tempDict["Counter_String_DefaultFormat"] = "$counter today: $today ($total total)"
tempDict["Counter_String_AccessError"] = "Error accessing counters"
for key in tempDict.keys():
if key not in CBE.Config.keys():
CBE.Config[key] = tempDict[key]
return
CBE.CheckConfigCallbacks.append(CounterCheckConfig)
def GetCounterHelp(counter):
return CBE.stemp(CBE.Config["Counter_String_Help"]).safe_substitute(counter=counter, cooldown=CBE.Config["Counter_CounterCooldown"])
def InternalCounterHelp(data):
arg0 = data.GetParam(1)
if arg0.startswith("!"):
arg0 = arg0[1:]
if arg0 in Counter_List.keys():
CBE.SendTwitchMessage(GetCounterHelp(arg0))
return True
return False
CBE.HelpCallbacks.append(InternalCounterHelp)
def LoadCounters():
global Counter_List, _cmdName
try:
with open(os.path.join(CBE.Path, Counter_File), "r") as ctf:
Counter_List = json.load(ctf)
except IOError as e:
CBE.CBELog("Could not open " + Counter_File + ": " + str(e))
def WriteFormattedCounters():
try:
with open(os.path.join(CBE.Path, Counter_Formatted_File), "w") as ctf:
for cntr in sorted(Counter_List.keys()):
ctf.write(str.capitalize(cntr) + ": " + str(Counter_List[cntr]["today"]) + " (Ges. " + str(Counter_List[cntr]["today"] + Counter_List[cntr]["total"]) + ")\n")
#ctf.write(CBE.stemp(Counter_List[cntr]["format"]).safe_substitute(counter=cntr, today=str(Counter_List[cntr]["today"]), total=str(Counter_List[cntr]["total"] + Counter_List[cntr]["today"])) + "\n")
except IOError as e:
CBE.CBELog("Could not write to " + Counter_Formatted_File + ": " + str(e))
def InternalCounterInit():
global _cmdName
LoadCounters()
_cmdName = CBE.Config["Counter_CMDName"]
CBE.Commands[CBE.Config["Counter_CMDName"]] = {"cd": "Counter_CMDCooldown", "global": "Counter_CMDIsGlobal", "hasArgs": True, "func": ProcessCounters, "usage": "Counter_CMDUsage", "desc": "Counter_CMDDescription"}
WriteFormattedCounters()
return
CBE.InitCallbacks.append(InternalCounterInit)
def InternalCounterExecute(data):
if data.IsChatMessage() and data.Message.startswith("!"):
if Counter_List is None:
CBE.SendTwitchMessage(CBE.Config["Counter_String_AccessError"])
else:
for counter in Counter_List.keys():
if data.Message.startswith("!" + counter) and (data.GetParamCount() > 1 and CBE.ProcessCommandCooldown(data, counter + "_set") or data.GetParamCount() == 1):
if CBE.ProcessCommandCooldown(data, counter + "_show"):
ProcessCounter(data, counter)
return
return
CBE.ExecuteCallbacks.append(InternalCounterExecute)
def InternalCounterUnload():
SaveCounters()
return
CBE.UnloadCallbacks.append(InternalCounterUnload)
def InternalCounterReloadSettings(jsonData):
global _cmdName
if _cmdName != CBE.Config["Counter_CMDName"]:
CBE.Commands[CBE.Config["Counter_CMDName"]] = CBE.Commands[_cmdName]
del CBE.Commands[_cmdName]
_cmdName = CBE.Config["Counter_CMDName"]
CBE.ReloadSettingsCallbacks.append(InternalCounterReloadSettings)
#region counter processing ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def AddCounter(cntr, format):
global Counter_List
if Counter_List is None:
CBE.SendTwitchMessage(CBE.Config["Counter_String_AccessError"])
else:
Counter_List[cntr] = {"today": 0, "total": 0, "format": CBE.Config["Counter_String_DefaultFormat"] if format is None else format}
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["Counter_String_AddedCounter"]).safe_substitute(counter=cntr))
return
def DeleteCounter(cntr):
global Counter_List
if Counter_List is None:
CBE.SendTwitchMessage(CBE.Config["Counter_AccessError"])
else:
del Counter_List[cntr]
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["Counter_String_DeletedCounter"]).safe_substitute(counter=cntr))
return
def SaveCounters():
global Counter_List
if Counter_List is None:
CBE.SendTwitchMessage(CBE.Config["Counter_String_AccessError"])
else:
for counter in Counter_List:
Counter_List[counter]["total"] += Counter_List[counter]["today"]
Counter_List[counter]["today"] = 0
with open(os.path.join(CBE.Path, Counter_File), "w") as ctf:
json.dump(Counter_List, ctf)
return
def ProcessCounter(data, cntr):
global Counter_List
if Counter_List is None:
CBE.SendTwitchMessage(CBE.Config["Counter_String_AccessError"])
else:
if data.GetParamCount() > 1:
if data.GetParam(1) == "+":
val = 1
if data.GetParamCount() > 2:
if data.GetParam(2).isdigit():
val = int(data.GetParam(2))
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["Counter_String_WholeNumber"]).safe_substitute())
return
Counter_List[cntr]["today"] += val
CBE.CBEAddCooldown(data, cntr + "_set", CBE.Config["Counter_CounterCooldown"])
elif data.GetParam(1) == "-":
val = 1
if data.GetParamCount() > 2:
if data.GetParam(2).isdigit():
val = int(data.GetParam(2))
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["Counter_String_WholeNumber"]).safe_substitute())
return
Counter_List[cntr]["today"] = max(Counter_List[cntr]["today"] - val, 0)
CBE.CBEAddCooldown(data, cntr + "_set", CBE.Config["Counter_CounterCooldown"])
elif data.GetParam(1) == "set":
if data.GetParamCount() > 2 and data.GetParam(2).isdigit():
Counter_List[cntr]["today"] = int(data.GetParam(2))
CBE.CBEAddCooldown(data, cntr + "_set", CBE.Config["Counter_CounterCooldown"])
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["Counter_String_WholeNumber"]).safe_substitute())
return
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["CBE_String_UnknownParameter"]).safe_substitute(param=data.GetParam(1)))
return
else:
CBE.CBEAddCooldown(data, cntr + "_show", CBE.Config["Counter_CounterCooldown"])
WriteFormattedCounters()
CBE.SendTwitchMessage(CBE.stemp(Counter_List[cntr]["format"]).safe_substitute(counter=cntr, today=str(Counter_List[cntr]["today"]), total=str(Counter_List[cntr]["total"] + Counter_List[cntr]["today"])))
return
def ProcessCounters(data):
if Counter_List is None:
CBE.SendTwitchMessage(CBE.Config["Counter_String_AccessError"])
else:
if data.GetParamCount() > 1 and CBE.HasPermission(data.User, "moderator", data.User):
if data.GetParam(1) == "add":
if data.GetParamCount() > 2:
if data.GetParam(2) in Counter_List.keys():
CBE.SendTwitchMessage(CBE.Config["Counter_String_AlreadyExists"])
elif data.GetParam(2) in CBE.Commands.keys():
CBE.SendTwitchMessage(CBE.Config["Counter_String_CommandExists"])
else:
lower = data.GetParam(2).lower()
for c in lower:
if c not in string.ascii_lowercase:
CBE.SendTwitchMessage(CBE.Config["Counter_String_NonASCII"])
return
format = CBE.Config["Counter_String_DefaultFormat"]
if data.GetParamCount() > 3:
format = " ".join([data.GetParam(i) for i in range(3, data.GetParamCount())])
AddCounter(lower, format)
CBE.CBEAddCooldown(data, CBE.Config["Counter_CMDName"], CBE.Config["Counter_CMDCooldown"], CBE.Config["Counter_CMDIsGlobal"])
else:
CBE.SendTwitchMessage(CBE.Config["CBE_String_MissingParameter"])
elif data.GetParam(1) == "delete":
if data.GetParamCount() > 2:
if data.GetParam(2).lower() in Counter_List.keys():
if data.GetParamCount() == 4 and data.GetParam(3) == "confirm":
DeleteCounter(data.GetParam(2).lower())
CBE.CBEAddCooldown(data, CBE.Config["Counter_CMDName"], CBE.Config["Counter_CMDCooldown"], CBE.Config["Counter_CMDIsGlobal"])
else:
CBE.SendTwitchMessage(CBE.Config["Counter_String_ConfirmNeeded"])
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["CBE_String_UnknownParameter"]).safe_substitute(param=data.GetParam(2)))
else:
CBE.SendTwitchMessage(CBE.Config["CBE_String_MissingParameter"])
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["CBE_String_UnknownParameter"]).safe_substitute(param=data.GetParam(1)))
else:
CBE.SendTwitchMessage(CBE.stemp(CBE.Config["Counter_String_AvailableCounters"]).safe_substitute(list=CBE.Config["Counter_String_CounterJoin"].join(sorted(Counter_List.keys()))))
CBE.CBEAddCooldown(data, CBE.Config["Counter_CMDName"], CBE.Config["Counter_CMDCooldown"], CBE.Config["Counter_CMDIsGlobal"])
WriteFormattedCounters()
return
#endregion