Praxis_Bot/cooldowns.py
2020-10-15 02:57:19 -04:00

88 lines
2.1 KiB
Python

import random
import re
import datetime
from datetime import timedelta
import time
from time import sleep
class Cooldown_Action:
def __init__(self):
self.name:str = ""
self.time = datetime.datetime.now()
class Cooldowns_Module:
def __init__(self):
super().__init__()
self.coolDownName:str = ""
self.coolDownActionLimit:int = 0
self.coolDownDuration:int = 0 #Seconds
self.actionList:list = []
def setupCooldown(self, name, limit, duration):
self.coolDownName = name
self.coolDownActionLimit = limit
self.coolDownDuration = duration
def isCooldownActive(self):
isCoolDownActivated:bool = False
timenow = datetime.datetime.now()
if len(self.actionList) >= self.coolDownActionLimit:
actionCount = len(self.actionList)
maxTmpIndex = actionCount - self.coolDownActionLimit
maxRecentAction:Cooldown_Action = self.actionList[maxTmpIndex]
timeDiff = timenow - maxRecentAction.time
maxTimeAllowed = timedelta(seconds = self.coolDownDuration)
if timeDiff < maxTimeAllowed:
isCoolDownActivated = True
return isCoolDownActivated
def actionTrigger(self, name:str = ""):
newAction = Cooldown_Action()
newAction.name = name
self.actionList.append(newAction)
if __name__ == "__main__":
testCD = Cooldowns_Module()
testCD.setupCooldown("test", 20, 2)
print("CD Test 1: ")
for x in range(20):
testCD.actionTrigger()
sleep(0)
print(testCD.isCooldownActive())
print("//Test Done//")
sleep(2)
print("CD Test 2: ")
for x in range(10):
testCD.actionTrigger()
sleep(0)
print(testCD.isCooldownActive())
print("//Test Done//")
sleep(2)
print("CD Test 3: ")
for x in range(20):
testCD.actionTrigger()
sleep(0.05)
print(testCD.isCooldownActive())
print("//Test Done//")
sleep(2)
print("CD Test 4: ")
for x in range(20):
testCD.actionTrigger()
sleep(0.6)
print(testCD.isCooldownActive())
print("//Test Done//")