stickerset.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. MIT License
  3. Copyright (c) 2023 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from typing import List
  21. from pyrogram import Client, errors, raw
  22. async def get_sticker_set_by_name(
  23. client: Client, name: str
  24. ) -> raw.base.messages.StickerSet:
  25. try:
  26. return await client.invoke(
  27. raw.functions.messages.GetStickerSet(
  28. stickerset=raw.types.InputStickerSetShortName(short_name=name),
  29. hash=0,
  30. )
  31. )
  32. except errors.exceptions.not_acceptable_406.StickersetInvalid:
  33. return None
  34. # Known errors: (I don't see a reason to catch them as we, for sure, won't face them right now):
  35. # errors.exceptions.bad_request_400.PackShortNameInvalid -> pack name needs to end with _by_botname
  36. # errors.exceptions.bad_request_400.ShortnameOccupyFailed -> pack's name is already in use
  37. async def create_sticker_set(
  38. client: Client,
  39. owner: int,
  40. title: str,
  41. short_name: str,
  42. stickers: List[raw.base.InputStickerSetItem],
  43. ) -> raw.base.messages.StickerSet:
  44. return await client.invoke(
  45. raw.functions.stickers.CreateStickerSet(
  46. user_id=await client.resolve_peer(owner),
  47. title=title,
  48. short_name=short_name,
  49. stickers=stickers,
  50. )
  51. )
  52. async def add_sticker_to_set(
  53. client: Client,
  54. stickerset: raw.base.messages.StickerSet,
  55. sticker: raw.base.InputStickerSetItem,
  56. ) -> raw.base.messages.StickerSet:
  57. return await client.invoke(
  58. raw.functions.stickers.AddStickerToSet(
  59. stickerset=raw.types.InputStickerSetShortName(
  60. short_name=stickerset.set.short_name
  61. ),
  62. sticker=sticker,
  63. )
  64. )
  65. async def create_sticker(
  66. sticker: raw.base.InputDocument, emoji: str
  67. ) -> raw.base.InputStickerSetItem:
  68. return raw.types.InputStickerSetItem(document=sticker, emoji=emoji)