from os import name import random import re import datetime from datetime import timedelta import time from time import sleep from typing import Optional 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() timepast = timenow.fromtimestamp if len(self.actionList) > self.coolDownActionLimit: actionCount = len(self.actionList) maxTmpIndex = actionCount - self.coolDownActionLimit - 1 maxRecentAction:Cooldown_Action = self.actionList[maxTmpIndex] minTmpIndex = actionCount - 1 mostRecentAction:Cooldown_Action = self.actionList[minTmpIndex] timeDiff = mostRecentAction.time - 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, 1) print("CD Test 1: ") for x in range(10): testCD.actionTrigger() sleep(0) print(testCD.isCooldownActive()) print("CD Test 2: ") for x in range(21): testCD.actionTrigger() sleep(0.08) print(testCD.isCooldownActive()) print("CD Test 3: ") for x in range(40): testCD.actionTrigger() sleep(0.02) print(testCD.isCooldownActive())