quotly.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. MIT License
  3. Copyright (c) 2024 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 io import BytesIO
  21. from traceback import format_exc
  22. from pyrogram import filters
  23. from pyrogram.types import Message
  24. from wbb import SUDOERS, USERBOT_PREFIX, app, app2, arq
  25. from wbb.core.decorators.errors import capture_err
  26. __MODULE__ = "Quotly"
  27. __HELP__ = """
  28. /q - To quote a message.
  29. /q [INTEGER] - To quote more than 1 messages.
  30. /q r - to quote a message with it's reply
  31. Use .q to quote using userbot
  32. """
  33. async def quotify(messages: list):
  34. response = await arq.quotly(messages)
  35. if not response.ok:
  36. return [False, response.result]
  37. sticker = response.result
  38. sticker = BytesIO(sticker)
  39. sticker.name = "sticker.webp"
  40. return [True, sticker]
  41. def getArg(message: Message) -> str:
  42. arg = message.text.strip().split(None, 1)[1].strip()
  43. return arg
  44. def isArgInt(message: Message) -> list:
  45. count = getArg(message)
  46. try:
  47. count = int(count)
  48. return [True, count]
  49. except ValueError:
  50. return [False, 0]
  51. @app2.on_message(
  52. filters.command("q", prefixes=USERBOT_PREFIX)
  53. & ~filters.forwarded
  54. & ~filters.via_bot
  55. & SUDOERS
  56. )
  57. @app.on_message(filters.command("q") & ~filters.private)
  58. @capture_err
  59. async def quotly_func(client, message: Message):
  60. if not message.reply_to_message:
  61. return await message.reply_text("Reply to a message to quote it.")
  62. if not message.reply_to_message.text:
  63. return await message.reply_text(
  64. "Replied message has no text, can't quote it."
  65. )
  66. m = await message.reply_text("Quoting Messages")
  67. if len(message.command) < 2:
  68. messages = [message.reply_to_message]
  69. elif len(message.command) == 2:
  70. arg = isArgInt(message)
  71. if arg[0]:
  72. if arg[1] < 2 or arg[1] > 10:
  73. return await m.edit("Argument must be between 2-10.")
  74. count = arg[1]
  75. # Fetching 5 extra messages so that we can ignore media
  76. # messages and still end up with correct offset
  77. messages = [
  78. i
  79. for i in await client.get_messages(
  80. message.chat.id,
  81. range(
  82. message.reply_to_message.id,
  83. message.reply_to_message.id + (count + 5),
  84. ),
  85. replies=0,
  86. )
  87. if not i.empty and not i.media
  88. ]
  89. messages = messages[:count]
  90. else:
  91. if getArg(message) != "r":
  92. return await m.edit(
  93. "Incorrect Argument, Pass **'r'** or **'INT'**, **EX:** __/q 2__"
  94. )
  95. reply_message = await client.get_messages(
  96. message.chat.id,
  97. message.reply_to_message.id,
  98. replies=1,
  99. )
  100. messages = [reply_message]
  101. else:
  102. return await m.edit(
  103. "Incorrect argument, check quotly module in help section."
  104. )
  105. try:
  106. #if not message:
  107. # return await m.edit("Something went wrong.")
  108. sticker = await quotify(messages)
  109. if not sticker[0]:
  110. await message.reply_text(sticker[1])
  111. return await m.delete()
  112. sticker = sticker[1]
  113. await message.reply_sticker(sticker)
  114. await m.delete()
  115. sticker.close()
  116. except Exception as e:
  117. await m.edit(
  118. "Something went wrong while quoting messages,"
  119. + " This error usually happens when there's a "
  120. + " message containing something other than text,"
  121. + " or one of the messages in-between are deleted."
  122. )
  123. e = format_exc()
  124. print(e)