Compare commits

..

6 Commits

Author SHA1 Message Date
7205964c3c Merge pull request 'Added Cooldowns to limit messages and commands.' (#8) from cooldowns into master
Reviewed-on: #8
2020-10-15 10:09:05 +00:00
Alex Orid
202d8e5891 Updated Mains. 2020-10-15 06:00:49 -04:00
Alex Orid
d88871abef Fixed Import 2020-10-15 02:57:19 -04:00
Alex Orid
bbe77195ac Fixed Cooldowns Bug 2020-10-15 02:48:26 -04:00
Alex Orid
e7d9ecb273 Fixed Cooldowns Bug 2020-10-15 01:58:00 -04:00
Alex Orid
5f5496c918 added cooldowns class 2020-10-15 01:27:51 -04:00
3 changed files with 115 additions and 7 deletions

88
cooldowns.py Normal file
View File

@ -0,0 +1,88 @@
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//")

8
db.py
View File

@ -69,5 +69,9 @@ class db_module():
if __name__ == "__main__":
db_connection = db_module()
db_connection.setup_engine()
testModule = db_module()
credentials_manager = credentials.Credentials_Module()
credentials_manager.load_credentials()
testModule.dbCredential = credentials_manager.find_DB_Credential("praxis_bot")
testModule.setup_engine()

View File

@ -1,3 +1,4 @@
from typing import Sequence
import random
import re
@ -12,6 +13,7 @@ import commands.loader as command_loader
import credentials
from commands.command_base import AbstractCommand
from cooldowns import Cooldowns_Module
class Twitch_Module():
def __init__(self):
@ -30,6 +32,11 @@ class Twitch_Module():
self._urlMatcher = re.compile(
"(https?:(/{1,3}|[a-z0-9%])|[a-z0-9.-]+[.](com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw))")
# Default Twitch Chat limit is 20 per 30 seconds
# If Mod or Op, Twitch Chat limit is 100 per 30 seconds
self.twitchChat_cooldown:Cooldowns_Module = Cooldowns_Module()
self.twitchChat_cooldown.setupCooldown("twitchChat", 20, 32)
def join_channel(self, credential: credentials.Twitch_Credential, channel_name:str):
channel_name = "#" + channel_name
print("Connecting to Channel: " + channel_name)
@ -53,7 +60,9 @@ class Twitch_Module():
self.chat.irc.socket.close()
def send_message(self, message):
self.chat.send(message)
if self.twitchChat_cooldown.isCooldownActive() == False:
self.chat.send(message)
self.twitchChat_cooldown.actionTrigger()
def send_whisper(self, user, message):
pass
@ -64,8 +73,10 @@ class Twitch_Module():
if message.channel == "thecuriousnerd":
if not self.isSenderBot(message):
if message.sender.lower() == "thecuriousnerd":
if self.twitchChat_cooldown.isCooldownActive() == False:
self.eval_commands(message)
#elif message.channel == message.sender:
#self.eval_commands(message)
self.tts_message(message)
def eval_commands(self, message: twitch.chat.Message):
@ -74,7 +85,7 @@ class Twitch_Module():
#first_space_idx = message.text.index(' ')
# This fixes a error where if you send a command without arguments it fails because
# it cant find the substring.
# it cant find the substring.
if message.text.find(" ") != -1:
first_space_idx = message.text.index(' ')
else:
@ -152,5 +163,10 @@ def main_chat_commands_check(channel, sender, text):
if __name__ == "__main__":
testChat = Twitch_Module()
testChat.join_channel("thecuriousnerd")
testModule = Twitch_Module()
credentials_manager = credentials.Credentials_Module()
credentials_manager.load_credentials()
testModule.twitchCredential = credentials_manager.find_Twitch_Credential("praxis_bot")
testModule.dbCredential = credentials_manager.find_DB_Credential("praxis_bot")
testModule.join_channel(None ,"thecuriousnerd")