-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from placeTW/hsinchu-kyoufuu
Hsinchu 強風 with cooldown
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import datetime | ||
import discord | ||
from re import compile, IGNORECASE, UNICODE | ||
|
||
KEYWORDS = ("HSINCHU", "新竹") | ||
|
||
HSINCHU_REGEX = compile( | ||
rf"(?:{'|'.join(KEYWORDS)})", flags=IGNORECASE | UNICODE | ||
) | ||
|
||
|
||
class HsinchuWind: | ||
COOLDOWN_TIME_IN_MINUTES = datetime.timedelta(minutes=5) | ||
HSINCHU_LINK = "https://www.youtube.com/watch?v=EtGDGCxq6m8" | ||
|
||
def __init__(self) -> None: | ||
self.latest_sent_time = None | ||
|
||
def set_latest_sent_time_to_now(self): | ||
self.latest_sent_time = datetime.datetime.now() | ||
|
||
def is_cooldown_over(self): | ||
if self.latest_sent_time is None: | ||
return True | ||
now_time = datetime.datetime.now() | ||
elapsed = now_time - self.latest_sent_time | ||
return elapsed > self.COOLDOWN_TIME_IN_MINUTES | ||
|
||
def get_response_or_ignore(self): | ||
if self.is_cooldown_over(): | ||
self.set_latest_sent_time_to_now() | ||
return self.HSINCHU_LINK | ||
else: | ||
# print("still on cooldown!") | ||
return None | ||
|
||
|
||
hsinchu_wind = HsinchuWind() | ||
|
||
|
||
def is_hsinchu_message(message: discord.Message): | ||
return HSINCHU_REGEX.search(message.content) | ||
|
||
|
||
async def send_hsinchu_msg(message: discord.Message): | ||
text = hsinchu_wind.get_response_or_ignore() | ||
if text is not None: | ||
await message.reply(text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters