paste.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import os
  21. import re
  22. import aiofiles
  23. from pyrogram import filters
  24. from pyrogram.types import Message
  25. from wbb import SUDOERS, USERBOT_PREFIX, app, app2, eor
  26. from wbb.core.decorators.errors import capture_err
  27. from wbb.core.keyboard import ikb
  28. from wbb.utils.pastebin import paste
  29. __MODULE__ = "Paste"
  30. __HELP__ = "/paste - To Paste Replied Text Or Document To A Pastebin"
  31. pattern = re.compile(r"^text/|json$|yaml$|xml$|toml$|x-sh$|x-shellscript$")
  32. @app2.on_message(filters.command("paste", prefixes=USERBOT_PREFIX) & SUDOERS)
  33. @app.on_message(filters.command("paste"))
  34. @capture_err
  35. async def paste_func(_, message: Message):
  36. if not message.reply_to_message:
  37. return await eor(message, text="Reply To A Message With /paste")
  38. r = message.reply_to_message
  39. if not r.text and not r.document:
  40. return await eor(message, text="Only text and documents are supported.")
  41. m = await eor(message, text="Pasting...")
  42. if r.text:
  43. content = str(r.text)
  44. elif r.document:
  45. if r.document.file_size > 40000:
  46. return await m.edit("You can only paste files smaller than 40KB.")
  47. if not pattern.search(r.document.mime_type):
  48. return await m.edit("Only text files can be pasted.")
  49. doc = await message.reply_to_message.download()
  50. async with aiofiles.open(doc, mode="r") as f:
  51. content = await f.read()
  52. os.remove(doc)
  53. link = await paste(content)
  54. kb = ikb({"Paste Link": link})
  55. try:
  56. if m.from_user.is_bot:
  57. await message.reply_photo(
  58. photo=link,
  59. quote=False,
  60. reply_markup=kb,
  61. )
  62. else:
  63. await message.reply_photo(
  64. photo=link,
  65. quote=False,
  66. caption=f"**Paste Link:** [Here]({link})",
  67. )
  68. await m.delete()
  69. except Exception:
  70. await m.edit("Here's your paste", reply_markup=kb)