actions.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. from random import uniform
  2. from asyncio import sleep
  3. from tortoise.contrib.postgres.functions import Random
  4. from telethon.utils import get_input_document
  5. from telethon.tl.functions.stickers import (
  6. CreateStickerSetRequest,
  7. AddStickerToSetRequest,
  8. )
  9. from telethon.tl.functions.messages import UploadMediaRequest, GetStickerSetRequest
  10. from telethon.tl.types import (
  11. InputStickerSetID,
  12. InputStickerSetShortName,
  13. InputStickerSetItem,
  14. InputMediaUploadedDocument,
  15. InputPeerSelf,
  16. )
  17. from tortoise.expressions import F
  18. from models import (
  19. Action,
  20. Gif,
  21. StickerPack,
  22. Admin,
  23. BirthDay,
  24. VPNServer,
  25. AllowedChat,
  26. MarkovChat,
  27. )
  28. from utils import is_valid_name, is_valid_ip
  29. from config import config
  30. async def is_admin(bot, user):
  31. admin = await bot.get_entity(config.ADMIN)
  32. if user.id == admin.id:
  33. return True
  34. admin = await Admin.filter(user_id=user.id)
  35. if admin:
  36. return True
  37. return False
  38. async def add_admin(user):
  39. await Admin(user_id=user.id).save()
  40. async def delete_admin(user):
  41. admin = await Admin.filter(user_id=user.id).first()
  42. if not admin:
  43. raise IndexError
  44. await admin.delete()
  45. async def create_action(name, template, kind):
  46. if not is_valid_name(name):
  47. raise SyntaxError
  48. await Action(name=name, template=template, kind=kind).save()
  49. async def find_action(name):
  50. if not is_valid_name(name):
  51. raise SyntaxError
  52. return await Action.filter(name=name).first()
  53. async def delete_action(name):
  54. action = await find_action(name)
  55. if not action:
  56. raise NameError
  57. gifs = await action.gifs.all()
  58. for gif in gifs:
  59. await gif.delete()
  60. await action.delete()
  61. async def add_gif(action, file_id):
  62. await Gif(action=action, file_id=file_id).save()
  63. async def get_random_gif(action):
  64. return await action.gifs.all().annotate(order=Random()).order_by("order").first()
  65. async def create_new_pack(bot, sticker):
  66. last_pack = await StickerPack.all().order_by("-id").first()
  67. set_id = last_pack.id + 1 if last_pack else 1
  68. user = await bot.get_entity(config.USER)
  69. me = await bot.get_me()
  70. bot_username = me.username
  71. pack = await bot(
  72. CreateStickerSetRequest(
  73. user_id=user.id,
  74. title=f"Messages #{set_id}.",
  75. short_name=f"messages{set_id}_by_{bot_username}",
  76. stickers=[sticker],
  77. )
  78. )
  79. sid = pack.set.id
  80. hash = pack.set.access_hash
  81. await StickerPack(short_name=pack.set.short_name, sid=sid, hash=hash).save()
  82. return pack
  83. async def get_current_pack(bot):
  84. pack = await StickerPack.all().order_by("-id").first()
  85. if not pack or pack.stickers_count >= 119:
  86. return None
  87. return pack
  88. async def add_sticker(bot, file, emoji):
  89. file = await bot.upload_file(file)
  90. file = InputMediaUploadedDocument(file, "image/png", [])
  91. file = await bot(UploadMediaRequest(InputPeerSelf(), file))
  92. file = get_input_document(file)
  93. sticker = InputStickerSetItem(document=file, emoji=emoji)
  94. pack = await get_current_pack(bot)
  95. if not pack:
  96. pack = await create_new_pack(bot, sticker)
  97. else:
  98. await StickerPack.filter(id=pack.id).update(
  99. stickers_count=F("stickers_count") + 1
  100. )
  101. pack = await bot(
  102. AddStickerToSetRequest(
  103. stickerset=InputStickerSetID(id=pack.sid, access_hash=pack.hash),
  104. sticker=sticker,
  105. )
  106. )
  107. return get_input_document(pack.documents[-1])
  108. async def get_birthdays(peer_id):
  109. return await BirthDay.filter(peer_id=peer_id).all()
  110. async def add_or_update_birthday(peer_id, user, date):
  111. birthday = await BirthDay.filter(peer_id=peer_id, user_id=user.id).first()
  112. if birthday:
  113. await BirthDay.filter(id=birthday.id).update(date=date)
  114. return False
  115. await BirthDay(peer_id=peer_id, user_id=user.id, date=date).save()
  116. return True
  117. async def get_all_birthdays():
  118. return await BirthDay.all()
  119. async def add_server(name, ip):
  120. if not is_valid_ip(ip):
  121. raise ValueError
  122. await VPNServer(name=name, ip=ip).save()
  123. async def add_server(name, ip):
  124. if not is_valid_name(name):
  125. raise SyntaxError
  126. if not is_valid_ip(ip):
  127. raise ValueError
  128. await VPNServer(name=name, ip=ip).save()
  129. async def delete_server(name):
  130. if not is_valid_name(name):
  131. raise SyntaxError
  132. server = await VPNServer.filter(name=name).first()
  133. if not server:
  134. raise IndexError
  135. await server.delete()
  136. async def list_servers():
  137. servers = await VPNServer.all()
  138. if not servers:
  139. return "*пусто*"
  140. return ", ".join(map(lambda server: server.name, servers))
  141. async def get_server_ip(name):
  142. if not is_valid_name(name):
  143. raise SyntaxError
  144. server = await VPNServer.filter(name=name).first()
  145. if not server:
  146. raise IndexError
  147. return server.ip
  148. async def add_allowed(peer_id):
  149. await AllowedChat(peer_id=peer_id).save()
  150. async def delete_allowed(peer_id):
  151. chat = await AllowedChat.filter(peer_id=peer_id).first()
  152. if not chat:
  153. raise IndexError
  154. await chat.delete()
  155. async def is_allowed(peer_id):
  156. return await AllowedChat.filter(peer_id=peer_id).exists()
  157. async def is_markov_enabled(peer_id):
  158. return await MarkovChat.filter(peer_id=peer_id).exists()
  159. async def enable_markov(peer_id):
  160. await MarkovChat(peer_id=peer_id).save()
  161. async def set_markov_options(peer_id, **options):
  162. chat = await MarkovChat.filter(peer_id=peer_id).first()
  163. if not chat:
  164. raise IndexError
  165. await MarkovChat.filter(id=chat.id).update(**options)
  166. async def get_markov_option(peer_id, option):
  167. chat = await MarkovChat.filter(peer_id=peer_id).first()
  168. if not chat:
  169. raise IndexError
  170. return getattr(chat, option)
  171. async def disable_markov(peer_id):
  172. chat = await MarkovChat.filter(peer_id=peer_id).first()
  173. if not chat:
  174. raise IndexError
  175. await chat.delete()
  176. async def list_markov_chats():
  177. return await MarkovChat.all()
  178. async def markov_say(bot, peer_id, reply_to=None, init_state=None):
  179. if not bot.markov.is_ready:
  180. return
  181. text = bot.markov.generate(init_state)
  182. async with bot.action(peer_id, "typing"):
  183. amount = 0
  184. for _ in range(len(text)):
  185. amount += round(uniform(0.05, 0.2), 2)
  186. await sleep(min(amount, 8))
  187. await bot.send_message(peer_id, message=text, reply_to=reply_to)