actions.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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
  9. from utils import is_valid_name
  10. from config import config
  11. async def create_action(name, template):
  12. if not is_valid_name(name):
  13. raise SyntaxError
  14. await Action(name=name, template=template).save()
  15. async def find_action(name):
  16. if not is_valid_name(name):
  17. raise SyntaxError
  18. return await Action.filter(name=name).first()
  19. async def delete_action(name):
  20. action = await find_action(name)
  21. if not action:
  22. raise NameError
  23. gifs = await action.gifs.all()
  24. for gif in gifs:
  25. await gif.delete()
  26. await action.delete()
  27. async def add_gif(action, file_id):
  28. await Gif(action=action, file_id=file_id).save()
  29. async def get_random_gif(action):
  30. return await action.gifs.all().annotate(order=Random()).order_by('order').first()
  31. async def assign_color(username):
  32. user_color = await UserColor.filter(username=username).first()
  33. if not user_color:
  34. user_color = UserColor(username=username, color=randint(0, 7))
  35. await user_color.save()
  36. return user_color.color
  37. async def create_new_pack(bot, sticker):
  38. last_pack = await StickerPack.all().order_by('-id').first()
  39. set_id = last_pack.id + 1 if last_pack else 1
  40. user = await bot.get_entity(config.USER)
  41. pack = await bot(CreateStickerSetRequest(
  42. user_id=user.id,
  43. title=f'Messages #{set_id}.',
  44. short_name=f'messages{set_id}_by_openkriemybot',
  45. stickers=[sticker]
  46. ))
  47. sid = pack.set.id
  48. hash = pack.set.access_hash
  49. await StickerPack(
  50. short_name=pack.set.short_name,
  51. sid=sid,
  52. hash=hash
  53. ).save()
  54. return pack
  55. async def get_current_pack(bot):
  56. pack = await StickerPack.all().order_by('-id').first()
  57. if not pack or pack.stickers_count == 120:
  58. return None
  59. return pack
  60. async def add_sticker(bot, file, emoji):
  61. file = await bot.upload_file(file)
  62. file = InputMediaUploadedDocument(file, 'image/png', [])
  63. file = await bot(UploadMediaRequest(InputPeerSelf(), file))
  64. file = get_input_document(file)
  65. sticker = InputStickerSetItem(document=file, emoji=emoji)
  66. pack = await get_current_pack(bot)
  67. if not pack:
  68. pack = await create_new_pack(bot, sticker)
  69. else:
  70. await StickerPack.filter(id=pack.id).update(stickers_count=F('stickers_count') + 1)
  71. pack = await bot(AddStickerToSetRequest(
  72. stickerset=InputStickerSetID(
  73. id=pack.sid,
  74. access_hash=pack.hash
  75. ),
  76. sticker=sticker
  77. ))
  78. return get_input_document(pack.documents[-1])