txlyre пре 2 година
родитељ
комит
dc4a06facc
2 измењених фајлова са 38 додато и 4 уклоњено
  1. 7 1
      actions.py
  2. 31 3
      openkriemy.py

+ 7 - 1
actions.py

@@ -8,7 +8,10 @@ from telethon.tl.types import InputStickerSetID, InputStickerSetShortName, Input
 from tortoise.expressions import F
 
 from models import Action, Gif, StickerPack, Admin, BirthDay
-from utils import is_valid_name
+from utils import (
+  is_valid_name,
+  calculate_age
+)
 from config import config
 
 async def is_admin(bot, user):
@@ -137,3 +140,6 @@ async def add_or_update_birthday(peer_id, user, date):
   ).save()
 
   return True
+
+async def get_all_birthdays():
+  return await BirthDay.all()

+ 31 - 3
openkriemy.py

@@ -1,9 +1,10 @@
-from asyncio import run
+from asyncio import run, sleep
 
 from telethon import TelegramClient
 from telethon.events import NewMessage
 from telethon.utils import resolve_bot_file_id
 
+from actions import get_all_birthdays
 from utils import (
   parse_command,
   get_link_to_user,
@@ -23,7 +24,7 @@ bot = TelegramClient(
   config.API_ID, 
   config.API_HASH
 ).start(bot_token=config.API_TOKEN)
- 
+
 @bot.on(NewMessage)
 async def on_message(event):
   try:
@@ -104,8 +105,35 @@ async def on_message(event):
     file=gif,
     reply_to=reply_to
   )
-  
+
+# cursed
+async def notify_birthdays():
+  while True:
+    birthdays = await get_all_birthdays()
+
+    for birthday in birthdays:
+      age = calculate_age(birthday.date)
+
+      if age.days_until < 1:
+        try:
+          try:
+            entity = await bot.get_entity(birthday.user_id)
+          except ValueError:
+            await bot.get_participants(birthday.peer_id)
+
+            entity = await bot.get_entity(birthday.user_id)
+
+          await bot.send_message(
+            birthday.peer_id,
+            f'{get_link_to_user(entity)}, поздравляю с днём рождения!!~~'
+          )
+        except:
+          pass
+
+    await sleep(60 * 60 * 24) # 24 hours.
+
 with bot:
   bot.loop.run_until_complete(init_db())
+  bot.loop.create_task(notify_birthdays())
   bot.start()
   bot.run_until_disconnected()