From 88b0c7a681fc4ff28510157ec8ea3067ccaf55c6 Mon Sep 17 00:00:00 2001 From: Yuuki Chan Date: Sun, 25 Feb 2024 22:17:30 +0900 Subject: [PATCH] Updated cookie.py --- cookie.py | 76 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/cookie.py b/cookie.py index eff530d..d038a30 100644 --- a/cookie.py +++ b/cookie.py @@ -21,15 +21,49 @@ c_db = Database(cookie_table, meta) df = '%Y/%m/%d %H:%M:%S' +def remaining_time(date: str, timeout: int): + diff = (int(time.mktime(datetime.strptime(datetime.now().strftime(df), df).timetuple()) * 1000) - + int(time.mktime(datetime.strptime(date, df).timetuple()) * 1000)) + h = int((timeout - diff) / (60 * 60 * 1000) % 24) + m = int((timeout - diff) / (60 * 1000) % 60) + s = int((timeout - diff) / 1000 % 60) + hms = '' + + if h == 0 and m == 0 and s == 0: + return 0 + + if h != 0: + hms += f'{h}h ' + if m != 0: + hms += f'{m}m ' + if s != 0: + hms += f'{s}s ' + + return f'{hms[:-1]}' + + +def insert_user(user: str): + with c_db.engine.connect() as conn: + stmt = select(cookie_table).where(cookie_table.c.name == user) + cnt = len(conn.execute(stmt).fetchall()) + + if cnt == 0: + conn.execute(( + insert(cookie_table). + values({'name': user}) + )) + conn.commit() + + class Plugin(PluginBase): async def handle_message(self, source_nick, channel, message): parts = message.split() - c_db.create_table('Cookie') + c_db.create_table(cookie_table.name) self.channel_manager = ChannelManager() if parts[0].lower() == '!cookie': if len(parts) == 1: - self.insert_user(source_nick) + insert_user(source_nick) cookies = c_db.get_user(source_nick, 2) rnd = random.randint(1, 10) @@ -53,48 +87,16 @@ class Plugin(PluginBase): 'last': current.strftime(df) }) else: - rem = self.remaining_time(last.strftime(df), 30 * 60000) + rem = remaining_time(last.strftime(df), 30 * 60000) await self.bot.ircsend(f'PRIVMSG {channel} :Remaining time: {rem}') elif len(parts) == 2: cookies = c_db.get_user(parts[1], 2) if cookies == -1: - await self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t ' - f'find them.') + await self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}\'s cookies, ' + f'but I couldn\'t find them.') else: c = 'no cookies' if cookies == 0 \ else f'{cookies} cookie' if cookies == 1 \ else f'{cookies} cookies' await self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}.') - - def insert_user(self, user: str): - with c_db.engine.connect() as conn: - stmt = select(cookie_table).where(cookie_table.c.name == user) - cnt = len(conn.execute(stmt).fetchall()) - - if cnt == 0: - conn.execute(( - insert(cookie_table). - values({'name': user}) - )) - conn.commit() - - def remaining_time(self, date: str, timeout: int): - diff = (int(time.mktime(datetime.strptime(datetime.now().strftime(df), df).timetuple()) * 1000) - - int(time.mktime(datetime.strptime(date, df).timetuple()) * 1000)) - h = int((timeout - diff) / (60 * 60 * 1000) % 24) - m = int((timeout - diff) / (60 * 1000) % 60) - s = int((timeout - diff) / 1000 % 60) - hms = '' - - if h == 0 and m == 0 and s == 0: - return 0 - - if h != 0: - hms += f'{h}h ' - if m != 0: - hms += f'{m}m ' - if s != 0: - hms += f'{s}s ' - - return f'{hms[:-1]}.'