Skip to content

Commit

Permalink
Merge pull request #27 from placeTW/hsinchu-kyoufuu
Browse files Browse the repository at this point in the history
Hsinchu 強風 with cooldown
  • Loading branch information
chanomkaimuk authored Aug 11, 2023
2 parents 03ed870 + 82e2a12 commit 8f80311
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
48 changes: 48 additions & 0 deletions commands/hsinchu_wind/hsinchu_wind.py
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)
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 8f80311

Please sign in to comment.