Updated cookie.py

This commit is contained in:
Yuuki Chan 2024-02-25 22:17:30 +09:00
parent bc8424c1c8
commit 88b0c7a681

View file

@ -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]}.'