notes.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.core.decorators.permissions import adminsOnly
  24. from wbb.utils.dbfunctions import (delete_note, get_note,
  25. get_note_names, 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. #NOTE_NAME To Get A Note.
  30. /delete [NOTE_NAME] To Delete A Note."""
  31. @app.on_message(
  32. filters.command("save") & ~filters.edited & ~filters.private
  33. )
  34. @adminsOnly("can_change_info")
  35. async def save_notee(_, message):
  36. if len(message.command) < 2 or not message.reply_to_message:
  37. await message.reply_text(
  38. "**Usage:**\nReply to a text or sticker with /save [NOTE_NAME] to save it."
  39. )
  40. elif (
  41. not message.reply_to_message.text
  42. and not message.reply_to_message.sticker
  43. ):
  44. await message.reply_text(
  45. "__**You can only save text or stickers in notes.**__"
  46. )
  47. else:
  48. name = message.text.split(None, 1)[1].strip()
  49. if not name:
  50. return await message.reply_text(
  51. "**Usage**\n__/save [NOTE_NAME]__"
  52. )
  53. _type = "text" if message.reply_to_message.text else "sticker"
  54. note = {
  55. "type": _type,
  56. "data": message.reply_to_message.text.markdown
  57. if _type == "text"
  58. else message.reply_to_message.sticker.file_id,
  59. }
  60. await save_note(message.chat.id, name, note)
  61. await message.reply_text(f"__**Saved note {name}.**__")
  62. @app.on_message(
  63. filters.command("notes") & ~filters.edited & ~filters.private
  64. )
  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. await message.reply_text(msg)
  75. @app.on_message(
  76. filters.regex(r"^#.+")
  77. & filters.text
  78. & ~filters.edited
  79. & ~filters.private
  80. )
  81. @capture_err
  82. async def get_one_note(_, message):
  83. name = message.text.replace("#", "", 1)
  84. if not name:
  85. return
  86. _note = await get_note(message.chat.id, name)
  87. if not _note:
  88. return
  89. if _note["type"] == "text":
  90. await message.reply_text(
  91. _note["data"], disable_web_page_preview=True
  92. )
  93. else:
  94. await message.reply_sticker(_note["data"])
  95. @app.on_message(
  96. filters.command("delete") & ~filters.edited & ~filters.private
  97. )
  98. @adminsOnly("can_change_info")
  99. async def del_note(_, message):
  100. if len(message.command) < 2:
  101. return await message.reply_text(
  102. "**Usage**\n__/delete [NOTE_NAME]__"
  103. )
  104. name = message.text.split(None, 1)[1].strip()
  105. if not name:
  106. return await message.reply_text(
  107. "**Usage**\n__/delete [NOTE_NAME]__"
  108. )
  109. chat_id = message.chat.id
  110. deleted = await delete_note(chat_id, name)
  111. if deleted:
  112. await message.reply_text(
  113. f"**Deleted note {name} successfully.**"
  114. )
  115. else:
  116. await message.reply_text("**No such note.**")