notes.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. from wbb import app
  21. from wbb.utils.dbfunctions import (
  22. save_note, get_note_names, get_note, delete_note
  23. )
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.modules.admin import member_permissions
  26. from pyrogram import filters
  27. __MODULE__ = "Notes"
  28. __HELP__ = """/notes To Get All The Notes In The Chat.
  29. /save [NOTE_NAME] To Save A Note (Can be a sticker or text).
  30. /get [NOTE_NAME] To Get A Note.
  31. /delete [NOTE_NAME] To Delete A Note."""
  32. @app.on_message(filters.command("save") & ~filters.edited & ~filters.private)
  33. @capture_err
  34. async def save_notee(_, message):
  35. if len(message.command) < 2 or not message.reply_to_message:
  36. await message.reply_text("Usage:\nReply to a text or sticker with /save [NOTE_NAME] to save it.")
  37. elif not message.reply_to_message.text and not message.reply_to_message.sticker:
  38. await message.reply_text("__**You can only save text or stickers in notes.**__")
  39. elif len(await member_permissions(message.chat.id, message.from_user.id)) < 1:
  40. await message.reply_text("**You don't have enough permissions**")
  41. else:
  42. name = message.text.split(None, 1)[1].strip()
  43. if not name:
  44. await message.reply_text("**Usage**\n__/save [NOTE_NAME]__")
  45. return
  46. _type = "text" if message.reply_to_message.text else "sticker"
  47. note = {
  48. "type": _type,
  49. "data": message.reply_to_message.text.markdown if _type == "text" else message.reply_to_message.sticker.file_id
  50. }
  51. await save_note(message.chat.id, name, note)
  52. await message.reply_text(f"__**Saved note {name}.**__")
  53. @app.on_message(filters.command("notes") & ~filters.edited & ~filters.private)
  54. @capture_err
  55. async def get_notes(_, message):
  56. _notes = await get_note_names(message.chat.id)
  57. if not _notes:
  58. await message.reply_text("**No notes in this chat.**")
  59. else:
  60. msg = f"List of notes in {message.chat.title}\n"
  61. for note in _notes:
  62. msg += f"**-** `{note}`\n"
  63. msg += "Use **/get note_name** to get a note."
  64. await message.reply_text(msg)
  65. @app.on_message(filters.command("get") & ~filters.edited & ~filters.private)
  66. @capture_err
  67. async def get_one_note(_, message):
  68. if len(message.command) < 2:
  69. await message.reply_text("**Usage**\n__/get [NOTE_NAME]__")
  70. else:
  71. name = message.text.split(None, 1)[1].strip()
  72. if not name:
  73. await message.reply_text("**Usage**\n__/get [NOTE_NAME]__")
  74. return
  75. _note = await get_note(message.chat.id, name)
  76. if not _note:
  77. await message.reply_text("**No such note.**")
  78. else:
  79. if _note["type"] == "text":
  80. await message.reply_text(_note["data"], disable_web_page_preview=True)
  81. else:
  82. await message.reply_sticker(_note["data"])
  83. @app.on_message(filters.command("delete") & ~filters.edited & ~filters.private)
  84. @capture_err
  85. async def del_note(_, message):
  86. if len(message.command) < 2:
  87. await message.reply_text("**Usage**\n__/delete [NOTE_NAME]__")
  88. elif len(await member_permissions(message.chat.id, message.from_user.id)) < 1:
  89. await message.reply_text("**You don't have enough permissions**")
  90. else:
  91. name = message.text.split(None, 1)[1].strip()
  92. if not name:
  93. await message.reply_text("**Usage**\n__/delete [NOTE_NAME]__")
  94. return
  95. chat_id = message.chat.id
  96. deleted = await delete_note(chat_id, name)
  97. if deleted:
  98. await message.reply_text(f"**Deleted note {name} successfully.**")
  99. else:
  100. await message.reply_text("**No such note.**")