Updated cookie.py

This commit is contained in:
Yuuki Chan 2024-02-19 22:57:22 +09:00
parent 3ca7e9cf36
commit 8c32e5c3a4

View file

@ -1,3 +1,7 @@
import random
import time
from datetime import datetime
from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select
from src.channel_manager import ChannelManager from src.channel_manager import ChannelManager
@ -23,27 +27,44 @@ class Plugin(PluginBase):
self.channel_manager = ChannelManager() self.channel_manager = ChannelManager()
if parts[0].lower() == '!cookie': if parts[0].lower() == '!cookie':
if len(parts) == 1: # !cookie if len(parts) == 1:
self.insert_user(source_nick) 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'}) cookies = c_db.get(source_nick, 2)
elif len(parts) == 2: # !cookie USER 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) cookies = c_db.get(parts[1], 2)
if cookies == -1: if cookies == -1:
self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t ' self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t '
f'find them.') f'find them.')
else: else:
c = 'cookie' c = 'no cookies' if cookies == 0 \
if cookies == 0: else f'{cookies} cookie' if cookies == 1 \
c = 'no cookies.' else f'{cookies} cookies'
elif cookies == 1: self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}.')
c = f'{cookies} cookie.'
else:
c = f'{cookies} cookies.'
self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}')
def insert_user(self, user: str): def insert_user(self, user: str):
with c_db.engine.connect() as conn: with c_db.engine.connect() as conn:
@ -56,3 +77,24 @@ class Plugin(PluginBase):
values({'name': user}) values({'name': user})
)) ))
conn.commit() 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]}.'