EliteBot-Plugins/pantsu.py

104 lines
3.8 KiB
Python

import random
import time
from datetime import datetime
from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select
from src.channel_manager import ChannelManager
from src.db import Database
from src.plugin_base import PluginBase
meta = MetaData()
pantsu_table = Table(
'Pantsu',
meta,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('name', String, unique=True, nullable=False),
Column('pantsu', Integer, default=0),
Column('last', String, default='1999/01/01 00:00:00'),
)
db = Database(pantsu_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 db.engine.connect() as conn:
stmt = select(pantsu_table).where(pantsu_table.c.name == user)
cnt = len(conn.execute(stmt).fetchall())
if cnt == 0:
conn.execute((
insert(pantsu_table).
values({'name': user})
))
conn.commit()
class Plugin(PluginBase):
async def handle_message(self, source_nick, channel, message):
parts = message.split()
db.create_table(pantsu_table.name)
self.channel_manager = ChannelManager()
if parts[0].lower() == '!pantsu':
if len(parts) == 1:
insert_user(source_nick)
pantsu = db.get_user_value(source_nick, 2)
rnd = random.randint(1, 8)
new_pantsu = pantsu + rnd
last = datetime.strptime(db.get_user_value(source_nick, 3), df)
current = datetime.strptime(datetime.now().strftime(df), df)
diff = round((current - last).total_seconds() / 60.0)
chance = 0.10
if diff >= 30:
if chance == 1:
await self.bot.action(channel, f'takes away {rnd} pantsu from {source_nick}.')
await self.bot.privmsg(channel, f'You now have a total of {new_pantsu} pantsu.')
new_pantsu = pantsu - rnd
if new_pantsu <= 0:
db.set_user_values(source_nick, {
'pantsu': (pantsu - rnd),
'last': current.strftime(df)
})
else:
await self.bot.action(channel, f'gives {rnd} pantsu to {source_nick}.')
await self.bot.privmsg(channel, f'You now have a total of {new_pantsu} pantsu.')
db.set_user_values(source_nick, {
'pantsu': new_pantsu,
'last': current.strftime(df)
})
else:
rem = remaining_time(last.strftime(df), 30 * 60000)
await self.bot.privmsg(channel, f'You have to wait {rem} before you can get more pantsu.')
elif len(parts) == 2:
pantsu = db.get_user_value(parts[1], 2)
if pantsu == -1:
await self.bot.privmsg(channel, f'I\'ve looked everywhere for {parts[1]}\'s pantsu, but I couldn\'t'
f' find them anywhere.')
else:
await self.bot.privmsg(channel, f'{parts[1]} currently has {pantsu} pantsu.')