notes.py 4.9 KB

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