filters.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 re
  21. from pyrogram import filters
  22. from wbb import app
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.core.decorators.permissions import adminsOnly
  25. from wbb.core.keyboard import ikb
  26. from wbb.utils.dbfunctions import (
  27. delete_filter,
  28. get_filter,
  29. get_filters_names,
  30. save_filter,
  31. )
  32. from wbb.utils.filter_groups import chat_filters_group
  33. from wbb.utils.functions import extract_text_and_keyb
  34. __MODULE__ = "Filters"
  35. __HELP__ = """/filters To Get All The Filters In The Chat.
  36. /filter [FILTER_NAME] To Save A Filter (Can be a sticker or text).
  37. /stop [FILTER_NAME] To Stop A Filter.
  38. You can use markdown or html to save text too.
  39. Checkout /markdownhelp to know more about formattings and other syntax.
  40. """
  41. @app.on_message(filters.command("filter") & ~filters.private)
  42. @adminsOnly("can_change_info")
  43. async def save_filters(_, message):
  44. if len(message.command) < 2 or not message.reply_to_message:
  45. return await message.reply_text(
  46. "**Usage:**\nReply to a text or sticker with /filter [FILTER_NAME] to save it."
  47. )
  48. if not message.reply_to_message.text and not message.reply_to_message.sticker:
  49. return await message.reply_text(
  50. "__**You can only save text or stickers in filters.**__"
  51. )
  52. name = message.text.split(None, 1)[1].strip()
  53. if not name:
  54. return await message.reply_text("**Usage:**\n__/filter [FILTER_NAME]__")
  55. chat_id = message.chat.id
  56. _type = "text" if message.reply_to_message.text else "sticker"
  57. _filter = {
  58. "type": _type,
  59. "data": message.reply_to_message.text.markdown
  60. if _type == "text"
  61. else message.reply_to_message.sticker.file_id,
  62. }
  63. await save_filter(chat_id, name, _filter)
  64. await message.reply_text(f"__**Saved filter {name}.**__")
  65. @app.on_message(filters.command("filters") & ~filters.private)
  66. @capture_err
  67. async def get_filterss(_, message):
  68. _filters = await get_filters_names(message.chat.id)
  69. if not _filters:
  70. return await message.reply_text("**No filters in this chat.**")
  71. _filters.sort()
  72. msg = f"List of filters in {message.chat.title} :\n"
  73. for _filter in _filters:
  74. msg += f"**-** `{_filter}`\n"
  75. await message.reply_text(msg)
  76. @app.on_message(filters.command("stop") & ~filters.private)
  77. @adminsOnly("can_change_info")
  78. async def del_filter(_, message):
  79. if len(message.command) < 2:
  80. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  81. name = message.text.split(None, 1)[1].strip()
  82. if not name:
  83. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  84. chat_id = message.chat.id
  85. deleted = await delete_filter(chat_id, name)
  86. if deleted:
  87. await message.reply_text(f"**Deleted filter {name}.**")
  88. else:
  89. await message.reply_text("**No such filter.**")
  90. @app.on_message(
  91. filters.text & ~filters.private & ~filters.via_bot & ~filters.forwarded,
  92. group=chat_filters_group,
  93. )
  94. @capture_err
  95. async def filters_re(_, message):
  96. text = message.text.lower().strip()
  97. if not text:
  98. return
  99. chat_id = message.chat.id
  100. list_of_filters = await get_filters_names(chat_id)
  101. for word in list_of_filters:
  102. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  103. if re.search(pattern, text, flags=re.IGNORECASE):
  104. _filter = await get_filter(chat_id, word)
  105. data_type = _filter["type"]
  106. data = _filter["data"]
  107. if data_type == "text":
  108. keyb = None
  109. if re.findall(r"\[.+\,.+\]", data):
  110. keyboard = extract_text_and_keyb(ikb, data)
  111. if keyboard:
  112. data, keyb = keyboard
  113. if message.reply_to_message:
  114. await message.reply_to_message.reply_text(
  115. data,
  116. reply_markup=keyb,
  117. disable_web_page_preview=True,
  118. )
  119. if text.startswith("~"):
  120. await message.delete()
  121. return
  122. return await message.reply_text(
  123. data,
  124. reply_markup=keyb,
  125. disable_web_page_preview=True,
  126. )
  127. if message.reply_to_message:
  128. await message.reply_to_message.reply_sticker(data)
  129. if text.startswith("~"):
  130. await message.delete()
  131. return
  132. return await message.reply_sticker(data)