openkriemy.py 3.3 KB

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