paste.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 asyncio
  21. import os
  22. import re
  23. import aiofiles
  24. from pyrogram import filters
  25. from wbb import app
  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(
  32. r"^text/|json$|yaml$|xml$|toml$|x-sh$|x-shellscript$"
  33. )
  34. @app.on_message(filters.command("paste") & ~filters.edited)
  35. @capture_err
  36. async def paste_func(_, message):
  37. if not message.reply_to_message:
  38. return await message.reply("Reply To A Message With /paste")
  39. r = message.reply_to_message
  40. if not r.text and not r.document:
  41. return await message.reply(
  42. "Only text and documents are supported."
  43. )
  44. m = await message.reply("Pasting...")
  45. if r.text:
  46. content = str(r.text)
  47. elif r.document:
  48. if r.document.file_size > 40000:
  49. return await m.edit(
  50. "You can only paste files smaller than 40KB."
  51. )
  52. if not pattern.search(r.document.mime_type):
  53. return await m.edit("Only text files can be pasted.")
  54. doc = await message.reply_to_message.download()
  55. async with aiofiles.open(doc, mode="r") as f:
  56. content = await f.read()
  57. os.remove(doc)
  58. link = await paste(content)
  59. kb = ikb({"Paste Link": link})
  60. try:
  61. await message.reply_photo(
  62. photo=link, quote=False, reply_markup=kb
  63. )
  64. except Exception:
  65. await message.reply("Here's your paste", reply_markup=kb)