openkriemy.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from asyncio import run, sleep
  2. from datetime import datetime, timedelta, date, time
  3. from telethon import TelegramClient
  4. from telethon.events import NewMessage
  5. from telethon.utils import resolve_bot_file_id, get_peer_id
  6. from actions import get_all_birthdays
  7. from utils import (
  8. parse_command,
  9. get_link_to_user,
  10. calculate_age,
  11. Kind
  12. )
  13. from config import config
  14. from db import init_db
  15. from actions import (
  16. find_action,
  17. get_random_gif,
  18. is_admin,
  19. is_allowed
  20. )
  21. from commands import COMMANDS
  22. bot = TelegramClient(
  23. 'openkriemy',
  24. config.API_ID,
  25. config.API_HASH
  26. ).start(bot_token=config.API_TOKEN)
  27. @bot.on(NewMessage)
  28. async def on_message(event):
  29. try:
  30. command = parse_command(event.text)
  31. except ValueError:
  32. return
  33. handler = COMMANDS.get(command.name, None)
  34. if not await is_allowed(get_peer_id(event.peer_id)):
  35. if not handler or not handler.is_restricted:
  36. return
  37. if handler:
  38. if handler.is_restricted\
  39. and not await is_admin(bot, event.sender):
  40. await event.reply('К сожалению, данная команда Вам недоступна.')
  41. else:
  42. await handler.handler(bot, event, command)
  43. return
  44. try:
  45. action = await find_action(command.name)
  46. except SyntaxError:
  47. return
  48. if not action:
  49. return
  50. reply_to = None
  51. target = None
  52. if action.kind != Kind.NO_TARGET:
  53. target = await event.get_reply_message()
  54. if not target:
  55. try:
  56. target = await bot.get_entity(command.args[0])
  57. except (ValueError, IndexError):
  58. if action.kind != Kind.NO_TARGET_MAYBE:
  59. await event.reply('Это действие нужно применить на кого-то!')
  60. return
  61. else:
  62. reply_to = target
  63. target = target.sender
  64. if target is None:
  65. target = await bot.get_entity(event.peer_id.channel_id)
  66. if action.kind == Kind.CANNOT_APPLY_TO_SELF\
  67. and target.id == event.sender.id:
  68. await event.reply('Данное действие нельзя применять к самому себе...')
  69. return
  70. try:
  71. await event.delete()
  72. except:
  73. pass
  74. if event.sender is None:
  75. initiator = await bot.get_entity(event.peer_id.channel_id)
  76. initiator = initiator.title
  77. else:
  78. initiator = get_link_to_user(event.sender)
  79. text = action.template.format(**{
  80. 'initiator': initiator,
  81. 'target': get_link_to_user(target) if target else ''
  82. })
  83. gif = await get_random_gif(action)
  84. if gif:
  85. gif = resolve_bot_file_id(gif.file_id)
  86. await bot.send_message(
  87. event.peer_id,
  88. message=text,
  89. file=gif,
  90. reply_to=reply_to
  91. )
  92. async def notify_birthdays():
  93. birthdays = await get_all_birthdays()
  94. for birthday in birthdays:
  95. age = calculate_age(birthday.date)
  96. if age.days_until < 1:
  97. try:
  98. try:
  99. entity = await bot.get_entity(birthday.user_id)
  100. except ValueError:
  101. await bot.get_participants(birthday.peer_id)
  102. entity = await bot.get_entity(birthday.user_id)
  103. await bot.send_message(
  104. birthday.peer_id,
  105. f'{get_link_to_user(entity)}, поздравляю с днём рождения!!~~'
  106. )
  107. except:
  108. pass
  109. async def schedule(task, when):
  110. await sleep(
  111. ((when - datetime.now()) % timedelta(days=1)).total_seconds()
  112. )
  113. await task
  114. async def notify_birthdays_loop():
  115. interval = datetime.combine(date.today(), time(hour=0, minute=0))
  116. while True:
  117. await sleep(
  118. ((interval - datetime.now()) % timedelta(days=1)).total_seconds()
  119. )
  120. await notify_birthdays()
  121. with bot:
  122. bot.loop.run_until_complete(init_db())
  123. bot.loop.create_task(notify_birthdays_loop())
  124. bot.start()
  125. bot.run_until_disconnected()