forked from illogicalNetwork/LogicEFTBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcooldown.py
40 lines (35 loc) · 1.36 KB
/
cooldown.py
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
#!/usr/bin/python3
from typing import Dict
from bot.database import Database
import datetime
from datetime import timedelta
from bot.config import settings
from bot.log import log
# A map of Channel Name => Cooldown counter.
# The cooldown itself is the last-use timestamp from the
# channel.
cooldowns: Dict[str, datetime.datetime] = {}
def check_cooldown(db: Database, channel_name: str) -> bool:
"""
Returns true if the given channel is under cooldown.
(API requests should be blocked to avoid overloading backend.)
"""
if channel_name[0] == "#":
channel_name = channel_name[1:]
log.error("Someplace in the code is using channels with #.")
cooldown_time = cooldowns[channel_name] if channel_name in cooldowns else None
if cooldown_time is None:
return False # no cooldown found.
cooldown = db.get_cd(channel_name)
if cooldown is None:
cooldown = int(settings["default_cooldown"])
return not datetime.datetime.utcnow() - cooldown_time > timedelta(seconds=cooldown)
def reset_cooldown(channel_name: str) -> None:
"""
Updates the cooldown for the given channel, setting the last accessed
time to now.
"""
if channel_name[0] == "#":
channel_name = channel_name[1:]
log.error("Someplace in the code is using channels with #.")
cooldowns[channel_name] = datetime.datetime.utcnow()