openkriemy.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. from random import random, randint
  2. from asyncio import sleep
  3. from datetime import datetime, timedelta, date, time
  4. from telethon import TelegramClient
  5. from telethon.events import NewMessage, InlineQuery
  6. from telethon.utils import resolve_bot_file_id, get_peer_id
  7. from actions import get_all_birthdays
  8. from utils import parse_command, get_link_to_user, calculate_age, Kind
  9. from config import config
  10. from db import init_db
  11. from actions import (
  12. find_action,
  13. get_random_gif,
  14. is_admin,
  15. is_allowed,
  16. is_markov_enabled,
  17. get_markov_option,
  18. list_markov_chats,
  19. markov_say,
  20. run,
  21. )
  22. from commands import COMMANDS
  23. from markov import Markov
  24. bot = TelegramClient("openkriemy", config.API_ID, config.API_HASH).start(
  25. bot_token=config.API_TOKEN
  26. )
  27. markov = Markov()
  28. # Wait isn't that illegal??
  29. bot.markov = markov
  30. @bot.on(InlineQuery)
  31. async def on_inline_query(event):
  32. text = event.text.strip()
  33. if not text:
  34. return
  35. lang, input_text, result = await run(text)
  36. if not lang:
  37. await event.answer(
  38. [
  39. event.builder.article(result, text=result),
  40. ]
  41. )
  42. return
  43. result = f"```{lang}\n{input_text.replace('`', '')}```\n{result}"
  44. await event.answer(
  45. [
  46. event.builder.article("Результат.", text=result),
  47. ]
  48. )
  49. @bot.on(NewMessage)
  50. async def on_message(event):
  51. peer_id = get_peer_id(event.peer_id)
  52. text = event.text
  53. try:
  54. command = parse_command(text)
  55. except ValueError:
  56. if await is_markov_enabled(peer_id):
  57. markov.extend_corpus(text)
  58. for word in config.MARKOV_TRIGGER_WORDS:
  59. if word.lower() in text.lower() and random() > 0.5:
  60. await markov_say(bot, peer_id, reply_to=event)
  61. return
  62. reply_prob = await get_markov_option(peer_id, "opt_reply_prob")
  63. reply = await event.get_reply_message()
  64. if (
  65. reply
  66. and get_peer_id(reply.from_id) == await bot.get_peer_id("me")
  67. and random() > 0.5
  68. ) or random() > reply_prob:
  69. await markov_say(bot, peer_id, reply_to=event)
  70. return
  71. handler = COMMANDS.get(command.name, None)
  72. if handler and handler.is_public:
  73. await handler.handler(bot, event, command)
  74. return
  75. if not await is_allowed(peer_id):
  76. if not handler or not handler.is_restricted:
  77. return
  78. if handler:
  79. if handler.is_restricted and not await is_admin(bot, event.sender):
  80. await event.reply("К сожалению, данная команда Вам недоступна.")
  81. else:
  82. await handler.handler(bot, event, command)
  83. return
  84. try:
  85. action = await find_action(command.name)
  86. except SyntaxError:
  87. return
  88. if not action:
  89. return
  90. reply_to = None
  91. target = None
  92. if action.kind != Kind.NO_TARGET:
  93. target = await event.get_reply_message()
  94. if not target:
  95. try:
  96. target = await bot.get_entity(command.args[0])
  97. except (ValueError, IndexError):
  98. if action.kind != Kind.NO_TARGET_MAYBE:
  99. await event.reply("Это действие нужно применить на кого-то!")
  100. return
  101. else:
  102. reply_to = target
  103. target = target.sender
  104. if target is None:
  105. target = await bot.get_entity(event.peer_id.channel_id)
  106. if action.kind == Kind.CANNOT_APPLY_TO_SELF and target.id == event.sender.id:
  107. await event.reply("Данное действие нельзя применять к самому себе...")
  108. return
  109. try:
  110. await event.delete()
  111. except:
  112. pass
  113. if event.sender is None:
  114. initiator = await bot.get_entity(event.peer_id.channel_id)
  115. initiator = initiator.title
  116. else:
  117. initiator = get_link_to_user(event.sender)
  118. text = action.template.format(
  119. **{"initiator": initiator, "target": get_link_to_user(target) if target else ""}
  120. )
  121. gif = await get_random_gif(action)
  122. if gif:
  123. gif = resolve_bot_file_id(gif.file_id)
  124. await bot.send_message(event.peer_id, message=text, file=gif, reply_to=reply_to)
  125. async def notify_birthdays():
  126. birthdays = await get_all_birthdays()
  127. for birthday in birthdays:
  128. age = calculate_age(birthday.date)
  129. if age.days_until < 1:
  130. try:
  131. try:
  132. entity = await bot.get_entity(birthday.user_id)
  133. except ValueError:
  134. await bot.get_participants(birthday.peer_id)
  135. entity = await bot.get_entity(birthday.user_id)
  136. await bot.send_message(
  137. birthday.peer_id,
  138. f"{get_link_to_user(entity)}, поздравляю с днём рождения!!~~",
  139. )
  140. except:
  141. pass
  142. async def notify_birthdays_loop():
  143. interval = datetime.combine(date.today(), time(hour=0, minute=0))
  144. while True:
  145. await sleep(((interval - datetime.now()) % timedelta(days=1)).total_seconds())
  146. await notify_birthdays()
  147. async def markov_say_loop():
  148. while True:
  149. await sleep(randint(30, 60 * 5))
  150. for chat in await list_markov_chats():
  151. if random() > chat.opt_message_prob:
  152. await markov_say(bot, chat.peer_id)
  153. with bot:
  154. bot.loop.run_until_complete(init_db())
  155. bot.loop.create_task(notify_birthdays_loop())
  156. bot.loop.create_task(markov_say_loop())
  157. bot.start()
  158. bot.run_until_disconnected()