actions.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from random import randint
  2. from tortoise.contrib.postgres.functions import Random
  3. from telethon.utils import get_input_document
  4. from telethon.tl.functions.stickers import CreateStickerSetRequest, AddStickerToSetRequest
  5. from telethon.tl.functions.messages import UploadMediaRequest, GetStickerSetRequest
  6. from telethon.tl.types import InputStickerSetID, InputStickerSetShortName, InputStickerSetItem, InputMediaUploadedDocument, InputPeerSelf
  7. from tortoise.expressions import F
  8. from models import Action, Gif, UserColor, StickerPack, Admin
  9. from utils import is_valid_name
  10. from config import config
  11. async def is_admin(bot, user):
  12. admin = await bot.get_entity(config.ADMIN)
  13. if user.id == admin.id:
  14. return True
  15. admin = await Admin.filter(user_id=user.id)
  16. if admin:
  17. return True
  18. return False
  19. async def add_admin(user):
  20. await Admin(user_id=user.id).save()
  21. async def delete_admin(user):
  22. admin = await Admin.filter(user_id=user.id).first()
  23. if not admin:
  24. raise IndexError
  25. await admin.delete()
  26. async def create_action(name, template, can_apply_to_self):
  27. if not is_valid_name(name):
  28. raise SyntaxError
  29. await Action(name=name, template=template, can_apply_to_self=can_apply_to_self).save()
  30. async def find_action(name):
  31. if not is_valid_name(name):
  32. raise SyntaxError
  33. return await Action.filter(name=name).first()
  34. async def delete_action(name):
  35. action = await find_action(name)
  36. if not action:
  37. raise NameError
  38. gifs = await action.gifs.all()
  39. for gif in gifs:
  40. await gif.delete()
  41. await action.delete()
  42. async def add_gif(action, file_id):
  43. await Gif(action=action, file_id=file_id).save()
  44. async def get_random_gif(action):
  45. return await action.gifs.all().annotate(order=Random()).order_by('order').first()
  46. async def assign_color(username):
  47. user_color = await UserColor.filter(username=username).first()
  48. if not user_color:
  49. user_color = UserColor(username=username, color=randint(0, 7))
  50. await user_color.save()
  51. return user_color.color
  52. async def create_new_pack(bot, sticker):
  53. last_pack = await StickerPack.all().order_by('-id').first()
  54. set_id = last_pack.id + 1 if last_pack else 1
  55. user = await bot.get_entity(config.USER)
  56. pack = await bot(CreateStickerSetRequest(
  57. user_id=user.id,
  58. title=f'Messages #{set_id}.',
  59. short_name=f'messages{set_id}_by_openkriemybot',
  60. stickers=[sticker]
  61. ))
  62. sid = pack.set.id
  63. hash = pack.set.access_hash
  64. await StickerPack(
  65. short_name=pack.set.short_name,
  66. sid=sid,
  67. hash=hash
  68. ).save()
  69. return pack
  70. async def get_current_pack(bot):
  71. pack = await StickerPack.all().order_by('-id').first()
  72. if not pack or pack.stickers_count == 120:
  73. return None
  74. return pack
  75. async def add_sticker(bot, file, emoji):
  76. file = await bot.upload_file(file)
  77. file = InputMediaUploadedDocument(file, 'image/png', [])
  78. file = await bot(UploadMediaRequest(InputPeerSelf(), file))
  79. file = get_input_document(file)
  80. sticker = InputStickerSetItem(document=file, emoji=emoji)
  81. pack = await get_current_pack(bot)
  82. if not pack:
  83. pack = await create_new_pack(bot, sticker)
  84. else:
  85. await StickerPack.filter(id=pack.id).update(stickers_count=F('stickers_count') + 1)
  86. pack = await bot(AddStickerToSetRequest(
  87. stickerset=InputStickerSetID(
  88. id=pack.sid,
  89. access_hash=pack.hash
  90. ),
  91. sticker=sticker
  92. ))
  93. return get_input_document(pack.documents[-1])