chatServer/modules/spamprotection.py

29 lines
749 B
Python
Raw Normal View History

2021-10-28 06:17:07 +00:00
global time
import time
global spMult
spMult = 10
global spMinSleep
spMinSleep = 0.1
global spMaxSleep
spMaxSleep = 5
def f(env):
with connectionsLock:
connections[env["self"].cid]["spCmdStart"] = time.perf_counter()
handlers["preCommand"].append(f)
def f(env):
passedTime = 0
with connectionsLock:
if "spCmdStart" in connections[env["self"].cid]:
passedTime = time.perf_counter() - connections[env["self"].cid]["spCmdStart"]
if passedTime < 0: passedTime = 0
else:
return
sleepTime = passedTime*spMult
if sleepTime > spMaxSleep: sleepTime = spMaxSleep
if sleepTime < spMinSleep: sleepTime = spMinSleep
print("sleeping for: " +str(sleepTime))
time.sleep(sleepTime)
handlers["command"].append(f)