openkriemy.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 handler and handler.is_public:
  35. await handler.handler(bot, event, command)
  36. return
  37. if not await is_allowed(get_peer_id(event.peer_id)):
  38. if not handler or not handler.is_restricted:
  39. return
  40. if handler:
  41. if handler.is_restricted\
  42. and not await is_admin(bot, event.sender):
  43. await event.reply('К сожалению, данная команда Вам недоступна.')
  44. else:
  45. await handler.handler(bot, event, command)
  46. return
  47. try:
  48. action = await find_action(command.name)
  49. except SyntaxError:
  50. return
  51. if not action:
  52. return
  53. reply_to = None
  54. target = None
  55. if action.kind != Kind.NO_TARGET:
  56. target = await event.get_reply_message()
  57. if not target:
  58. try:
  59. target = await bot.get_entity(command.args[0])
  60. except (ValueError, IndexError):
  61. if action.kind != Kind.NO_TARGET_MAYBE:
  62. await event.reply('Это действие нужно применить на кого-то!')
  63. return
  64. else:
  65. reply_to = target
  66. target = target.sender
  67. if target is None:
  68. target = await bot.get_entity(event.peer_id.channel_id)
  69. if action.kind == Kind.CANNOT_APPLY_TO_SELF\
  70. and target.id == event.sender.id:
  71. await event.reply('Данное действие нельзя применять к самому себе...')
  72. return
  73. try:
  74. await event.delete()
  75. except:
  76. pass
  77. if event.sender is None:
  78. initiator = await bot.get_entity(event.peer_id.channel_id)
  79. initiator = initiator.title
  80. else:
  81. initiator = get_link_to_user(event.sender)
  82. text = action.template.format(**{
  83. 'initiator': initiator,
  84. 'target': get_link_to_user(target) if target else ''
  85. })
  86. gif = await get_random_gif(action)
  87. if gif:
  88. gif = resolve_bot_file_id(gif.file_id)
  89. await bot.send_message(
  90. event.peer_id,
  91. message=text,
  92. file=gif,
  93. reply_to=reply_to
  94. )
  95. async def notify_birthdays():
  96. birthdays = await get_all_birthdays()
  97. for birthday in birthdays:
  98. age = calculate_age(birthday.date)
  99. if age.days_until < 1:
  100. try:
  101. try:
  102. entity = await bot.get_entity(birthday.user_id)
  103. except ValueError:
  104. await bot.get_participants(birthday.peer_id)
  105. entity = await bot.get_entity(birthday.user_id)
  106. await bot.send_message(
  107. birthday.peer_id,
  108. f'{get_link_to_user(entity)}, поздравляю с днём рождения!!~~'
  109. )
  110. except:
  111. pass
  112. async def notify_birthdays_loop():
  113. interval = datetime.combine(date.today(), time(hour=0, minute=0))
  114. while True:
  115. await sleep(
  116. ((interval - datetime.now()) % timedelta(days=1)).total_seconds()
  117. )
  118. await notify_birthdays()
  119. with bot:
  120. bot.loop.run_until_complete(init_db())
  121. bot.loop.create_task(notify_birthdays_loop())
  122. bot.start()
  123. bot.run_until_disconnected()