openkriemy.py 3.4 KB

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