|
@@ -1,10 +1,11 @@
|
|
|
from sys import executable
|
|
|
from asyncio import create_subprocess_shell
|
|
|
from asyncio.subprocess import PIPE
|
|
|
+from datetime import datetime, timedelta
|
|
|
|
|
|
from ujson import dumps
|
|
|
from tortoise.exceptions import IntegrityError
|
|
|
-from telethon.utils import get_display_name
|
|
|
+from telethon.utils import get_display_name, get_peer_id
|
|
|
from aiofiles.os import remove
|
|
|
from emoji import is_emoji
|
|
|
|
|
@@ -16,9 +17,11 @@ from actions import (
|
|
|
add_sticker,
|
|
|
assign_color,
|
|
|
add_admin,
|
|
|
- delete_admin
|
|
|
+ delete_admin,
|
|
|
+ add_or_update_birthday,
|
|
|
+ get_birthdays
|
|
|
)
|
|
|
-from utils import make_temporary_filename, parse_kind
|
|
|
+from utils import WORDS_TABLE, make_temporary_filename, parse_kind, get_link_to_user, get_word_kind
|
|
|
|
|
|
class Handler:
|
|
|
def __init__(self, handler, is_restricted=False):
|
|
@@ -203,7 +206,51 @@ async def save_handler(bot, event, command):
|
|
|
reply_to=message
|
|
|
)
|
|
|
finally:
|
|
|
- await remove(path)
|
|
|
+ await remove(path)
|
|
|
+
|
|
|
+async def bday_handler(bot, event, command):
|
|
|
+ if command.argc >= 1:
|
|
|
+ try:
|
|
|
+ date = datetime.strptime(' '.join(command.args), '%d.%m.%Y')
|
|
|
+ except ValueError:
|
|
|
+ await event.reply('Дата не может быть распознана. Пожалуйста, введите свой день рождения в следующем формате: 01.01.1970 (день, месяц, год).')
|
|
|
+
|
|
|
+ return
|
|
|
+
|
|
|
+ if await add_or_update_birthday(get_peer_id(event.peer_id), event.sender, date):
|
|
|
+ await event.reply('День рождения успешно добавлен!!!')
|
|
|
+ else:
|
|
|
+ await event.reply('День рождения успешно обновлён!!!')
|
|
|
+
|
|
|
+ return
|
|
|
+
|
|
|
+ birthdays = await get_birthdays(get_peer_id(event.peer_id))
|
|
|
+ if not birthdays:
|
|
|
+ await event.reply('Пока пусто…')
|
|
|
+
|
|
|
+ return
|
|
|
+
|
|
|
+ birthdays = sorted(birthdays, key=lambda birthday: birthday.date)
|
|
|
+
|
|
|
+ birthdays_list = ''
|
|
|
+ for birthday in birthdays:
|
|
|
+ now = datetime.now().date()
|
|
|
+ birthday_date = birthday.date.replace(year=now.year)
|
|
|
+
|
|
|
+ if now > birthday_date:
|
|
|
+ birthday_date = birthday.date.replace(year=now.year + 1)
|
|
|
+
|
|
|
+ delta = birthday_date - now
|
|
|
+ age = (birthday_date - birthday.date).days // 365
|
|
|
+ age_now = age - 1
|
|
|
+
|
|
|
+ birthdays_list += '- '
|
|
|
+ birthdays_list += get_link_to_user(await bot.get_entity(birthday.user_id))
|
|
|
+ birthdays_list += ': '
|
|
|
+ birthdays_list += birthday.date.strftime('%d.%m.%Y')
|
|
|
+ birthdays_list += f' (через {delta.days} {WORDS_TABLE["день"][get_word_kind(delta.days)]} исполнится {age} {WORDS_TABLE["год"][get_word_kind(age)]}; сейчас {age_now} {WORDS_TABLE["год"][get_word_kind(age_now)]})\n'
|
|
|
+
|
|
|
+ await event.reply(f'Дни рождения:\n{birthdays_list}')
|
|
|
|
|
|
COMMANDS = {
|
|
|
'newadmin': Handler(newadmin_handler, is_restricted=True),
|
|
@@ -212,5 +259,7 @@ COMMANDS = {
|
|
|
'delaction': Handler(delaction_handler, is_restricted=True),
|
|
|
'addgif': Handler(addgif_handler, is_restricted=True),
|
|
|
|
|
|
- 'save': Handler(save_handler)
|
|
|
+ 'save': Handler(save_handler),
|
|
|
+
|
|
|
+ 'bday': Handler(bday_handler)
|
|
|
}
|