openkriemy.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. await event.answer(
  44. [
  45. event.builder.article(
  46. "Результат.",
  47. text=result,
  48. ),
  49. ]
  50. )
  51. @bot.on(NewMessage)
  52. async def on_message(event):
  53. peer_id = get_peer_id(event.peer_id)
  54. text = event.text
  55. try:
  56. command = parse_command(text)
  57. except ValueError:
  58. if await is_markov_enabled(peer_id):
  59. markov.extend_corpus(text)
  60. for word in config.MARKOV_TRIGGER_WORDS:
  61. if word.lower() in text.lower() and random() > 0.5:
  62. await markov_say(bot, peer_id, reply_to=event)
  63. return
  64. reply_prob = await get_markov_option(peer_id, "opt_reply_prob")
  65. reply = await event.get_reply_message()
  66. if (
  67. reply
  68. and get_peer_id(reply.from_id) == await bot.get_peer_id("me")
  69. and random() > 0.5
  70. ) or random() > reply_prob:
  71. await markov_say(bot, peer_id, reply_to=event)
  72. return
  73. handler = COMMANDS.get(command.name, None)
  74. if handler and handler.is_public:
  75. await handler.handler(bot, event, command)
  76. return
  77. if not await is_allowed(peer_id):
  78. if not handler or not handler.is_restricted:
  79. return
  80. if handler:
  81. if handler.is_restricted and not await is_admin(bot, event.sender):
  82. await event.reply("К сожалению, данная команда Вам недоступна.")
  83. else:
  84. await handler.handler(bot, event, command)
  85. return
  86. try:
  87. action = await find_action(command.name)
  88. except SyntaxError:
  89. return
  90. if not action:
  91. return
  92. reply_to = None
  93. target = None
  94. if action.kind != Kind.NO_TARGET:
  95. target = await event.get_reply_message()
  96. if not target:
  97. try:
  98. target = await bot.get_entity(command.args[0])
  99. except (ValueError, IndexError):
  100. if action.kind != Kind.NO_TARGET_MAYBE:
  101. await event.reply("Это действие нужно применить на кого-то!")
  102. return
  103. else:
  104. reply_to = target
  105. target = target.sender
  106. if target is None:
  107. target = await bot.get_entity(event.peer_id.channel_id)
  108. if action.kind == Kind.CANNOT_APPLY_TO_SELF and target.id == event.sender.id:
  109. await event.reply("Данное действие нельзя применять к самому себе...")
  110. return
  111. try:
  112. await event.delete()
  113. except:
  114. pass
  115. if event.sender is None:
  116. initiator = await bot.get_entity(event.peer_id.channel_id)
  117. initiator = initiator.title
  118. else:
  119. initiator = get_link_to_user(event.sender)
  120. text = action.template.format(
  121. **{"initiator": initiator, "target": get_link_to_user(target) if target else ""}
  122. )
  123. gif = await get_random_gif(action)
  124. if gif:
  125. gif = resolve_bot_file_id(gif.file_id)
  126. await bot.send_message(event.peer_id, message=text, file=gif, reply_to=reply_to)
  127. async def notify_birthdays():
  128. birthdays = await get_all_birthdays()
  129. for birthday in birthdays:
  130. age = calculate_age(birthday.date)
  131. if age.days_until < 1:
  132. try:
  133. try:
  134. entity = await bot.get_entity(birthday.user_id)
  135. except ValueError:
  136. await bot.get_participants(birthday.peer_id)
  137. entity = await bot.get_entity(birthday.user_id)
  138. await bot.send_message(
  139. birthday.peer_id,
  140. f"{get_link_to_user(entity)}, поздравляю с днём рождения!!~~",
  141. )
  142. except:
  143. pass
  144. async def notify_birthdays_loop():
  145. interval = datetime.combine(date.today(), time(hour=0, minute=0))
  146. while True:
  147. await sleep(((interval - datetime.now()) % timedelta(days=1)).total_seconds())
  148. await notify_birthdays()
  149. async def markov_say_loop():
  150. while True:
  151. await sleep(randint(30, 60 * 5))
  152. for chat in await list_markov_chats():
  153. if random() > chat.opt_message_prob:
  154. await markov_say(bot, chat.peer_id)
  155. with bot:
  156. bot.loop.run_until_complete(init_db())
  157. bot.loop.create_task(notify_birthdays_loop())
  158. bot.loop.create_task(markov_say_loop())
  159. bot.start()
  160. bot.run_until_disconnected()