filters.py 5.6 KB

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