diff --git a/commands/hsinchu_wind/hsinchu_wind.py b/commands/hsinchu_wind/hsinchu_wind.py new file mode 100644 index 0000000..8ce4fd6 --- /dev/null +++ b/commands/hsinchu_wind/hsinchu_wind.py @@ -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) diff --git a/main.py b/main.py index 88a7c0d..fb27f53 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,7 @@ from commands import hgs from commands.reacttw import react_tw from commands.react_baltics import react_baltics +from commands.hsinchu_wind import hsinchu_wind from commands.shiba import random_shiba from commands.capoo import random_capoo import sys @@ -107,5 +108,8 @@ async def on_message(message: discord.Message): if react_baltics.is_baltic_message(message): await react_baltics.send_react_baltic(message) + if hsinchu_wind.is_hsinchu_message(message): + await hsinchu_wind.send_hsinchu_msg(message) + client.run(TOKEN)