From 8c32e5c3a400cdc19e66006a464d523475b0b694 Mon Sep 17 00:00:00 2001 From: Yuuki Chan Date: Mon, 19 Feb 2024 22:57:22 +0900 Subject: [PATCH] Updated cookie.py --- plugins/cookie.py | 68 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/plugins/cookie.py b/plugins/cookie.py index f8170af..8eedb97 100644 --- a/plugins/cookie.py +++ b/plugins/cookie.py @@ -1,3 +1,7 @@ +import random +import time +from datetime import datetime + from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select from src.channel_manager import ChannelManager @@ -23,27 +27,44 @@ class Plugin(PluginBase): self.channel_manager = ChannelManager() if parts[0].lower() == '!cookie': - if len(parts) == 1: # !cookie + if len(parts) == 1: self.insert_user(source_nick) - self.bot.ircsend(f'PRIVMSG {channel} :Nooooo~~') - c_db.set(source_nick, {'cookies': 1, 'last': '1999/01/01 00:00:01'}) - elif len(parts) == 2: # !cookie USER + cookies = c_db.get(source_nick, 2) + rnd = random.randint(1, 10) + last = datetime.strptime(c_db.get(source_nick, 3), '%Y/%m/%d %H:%M:%S') + current = datetime.strptime(datetime.now().strftime('%Y/%m/%d %H:%M:%S'), '%Y/%m/%d %H:%M:%S') + diff = round((current - last).total_seconds() / 60.0) + + if diff >= 30: + c1 = 'no cookies' if rnd == 0 \ + else f'{rnd} cookie' if rnd == 1 \ + else f'{rnd} cookies' + c2 = 'no cookies' if (cookies + rnd) == 0 \ + else f'{(cookies + rnd)} cookie' if (cookies + rnd) == 1 \ + else f'{(cookies + rnd)} cookies' + + self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION gives {c1} to {source_nick}.\x01') + self.bot.ircsend(f'PRIVMSG {channel} :You now have a total of {c2}.') + + c_db.set(source_nick, { + 'cookies': (cookies + rnd), + 'last': current.strftime('%Y/%m/%d %H:%M:%S') + }) + else: + rem = self.remaining_time(last.strftime('%Y/%m/%d %H:%M:%S'), 30 * 60000) + self.bot.ircsend(f'PRIVMSG {channel} :Remaining time: {rem}') + elif len(parts) == 2: cookies = c_db.get(parts[1], 2) if cookies == -1: self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t ' f'find them.') else: - c = 'cookie' - if cookies == 0: - c = 'no cookies.' - elif cookies == 1: - c = f'{cookies} cookie.' - else: - c = f'{cookies} cookies.' - - self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}') + c = 'no cookies' if cookies == 0 \ + else f'{cookies} cookie' if cookies == 1 \ + else f'{cookies} cookies' + self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}.') def insert_user(self, user: str): with c_db.engine.connect() as conn: @@ -56,3 +77,24 @@ class Plugin(PluginBase): values({'name': user}) )) conn.commit() + + def remaining_time(self, date: str, timeout: int): + diff = (int(time.mktime( + datetime.strptime(datetime.now().strftime('%Y/%m/%d %H:%M:%S'), '%Y/%m/%d %H:%M:%S').timetuple()) * 1000) - + int(time.mktime(datetime.strptime(date, '%Y/%m/%d %H:%M:%S').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]}.'