filters.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. from wbb.modules.notes import extract_urls
  35. __MODULE__ = "Filters"
  36. __HELP__ = """/filters To Get All The Filters In The Chat.
  37. /filter [FILTER_NAME] To Save A Filter(reply to a message).
  38. Supported filter types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice
  39. To use more words in a filter use.
  40. `/filter Hey_there` To filter "Hey there"
  41. /stop [FILTER_NAME] To Stop A Filter.
  42. You can use markdown or html to save text too.
  43. Checkout /markdownhelp to know more about formattings and other syntax.
  44. """
  45. @app.on_message(filters.command("filter") & ~filters.private)
  46. @adminsOnly("can_change_info")
  47. async def save_filters(_, message):
  48. if len(message.command) < 2 or not message.reply_to_message:
  49. return await message.reply_text(
  50. "**Usage:**\nReply to a message with /filter [FILTER_NAME] To set a new filter."
  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. replied_message = message.reply_to_message
  57. text = name.split(" ", 1)
  58. if len(text) > 1:
  59. name = text[0]
  60. data = text[1].strip()
  61. if replied_message.sticker or replied_message.video_note:
  62. data = None
  63. else:
  64. if replied_message.sticker or replied_message.video_note:
  65. data = None
  66. elif not replied_message.text and not replied_message.caption:
  67. data = None
  68. else:
  69. data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
  70. if replied_message.text:
  71. _type = "text"
  72. file_id = None
  73. if replied_message.sticker:
  74. _type = "sticker"
  75. file_id = replied_message.sticker.file_id
  76. if replied_message.animation:
  77. _type = "animation"
  78. file_id = replied_message.animation.file_id
  79. if replied_message.photo:
  80. _type = "photo"
  81. file_id = replied_message.photo.file_id
  82. if replied_message.document:
  83. _type = "document"
  84. file_id = replied_message.document.file_id
  85. if replied_message.video:
  86. _type = "video"
  87. file_id = replied_message.video.file_id
  88. if replied_message.video_note:
  89. _type = "video_note"
  90. file_id = replied_message.video_note.file_id
  91. if replied_message.audio:
  92. _type = "audio"
  93. file_id = replied_message.audio.file_id
  94. if replied_message.voice:
  95. _type = "voice"
  96. file_id = replied_message.voice.file_id
  97. if replied_message.reply_markup and not "~" in data:
  98. urls = extract_urls(replied_message.reply_markup)
  99. if urls:
  100. response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
  101. data = data + response
  102. name = name.replace("_", " ")
  103. _filter = {
  104. "type": _type,
  105. "data": data,
  106. "file_id": file_id,
  107. }
  108. await save_filter(chat_id, name, _filter)
  109. return await message.reply_text(f"__**Saved filter {name}.**__")
  110. @app.on_message(filters.command("filters") & ~filters.private)
  111. @capture_err
  112. async def get_filterss(_, message):
  113. _filters = await get_filters_names(message.chat.id)
  114. if not _filters:
  115. return await message.reply_text("**No filters in this chat.**")
  116. _filters.sort()
  117. msg = f"List of filters in {message.chat.title} :\n"
  118. for _filter in _filters:
  119. msg += f"**-** `{_filter}`\n"
  120. await message.reply_text(msg)
  121. @app.on_message(filters.command("stop") & ~filters.private)
  122. @adminsOnly("can_change_info")
  123. async def del_filter(_, message):
  124. if len(message.command) < 2:
  125. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  126. name = message.text.split(None, 1)[1].strip()
  127. if not name:
  128. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  129. chat_id = message.chat.id
  130. deleted = await delete_filter(chat_id, name)
  131. if deleted:
  132. await message.reply_text(f"**Deleted filter {name}.**")
  133. else:
  134. await message.reply_text("**No such filter.**")
  135. @app.on_message(
  136. filters.text & ~filters.private & ~filters.via_bot & ~filters.forwarded,
  137. group=chat_filters_group,
  138. )
  139. @capture_err
  140. async def filters_re(_, message):
  141. text = message.text.lower().strip()
  142. if not text:
  143. return
  144. chat_id = message.chat.id
  145. list_of_filters = await get_filters_names(chat_id)
  146. for word in list_of_filters:
  147. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  148. if re.search(pattern, text, flags=re.IGNORECASE):
  149. _filter = await get_filter(chat_id, word)
  150. data_type = _filter["type"]
  151. data = _filter["data"]
  152. file_id = _filter["file_id"]
  153. keyb = None
  154. if data:
  155. if re.findall(r"\[.+\,.+\]", data):
  156. keyboard = extract_text_and_keyb(ikb, data)
  157. if keyboard:
  158. data, keyb = keyboard
  159. replied_message = message.reply_to_message
  160. if replied_message:
  161. if text.startswith("~"):
  162. await message.delete()
  163. if replied_message.from_user.id != message.from_user.id:
  164. message = replied_message
  165. if data_type == "text":
  166. await message.reply_text(
  167. text=data,
  168. reply_markup=keyb,
  169. disable_web_page_preview=True,
  170. )
  171. if data_type == "sticker":
  172. await message.reply_sticker(
  173. sticker=file_id,
  174. )
  175. if data_type == "animation":
  176. await message.reply_animation(
  177. animation=file_id,
  178. caption=data,
  179. reply_markup=keyb,
  180. )
  181. if data_type == "photo":
  182. await message.reply_photo(
  183. photo=file_id,
  184. caption=data,
  185. reply_markup=keyb,
  186. )
  187. if data_type == "document":
  188. await message.reply_document(
  189. document=file_id,
  190. caption=data,
  191. reply_markup=keyb,
  192. )
  193. if data_type == "video":
  194. await message.reply_video(
  195. video=file_id,
  196. caption=data,
  197. reply_markup=keyb,
  198. )
  199. if data_type == "video_note":
  200. await message.reply_video_note(
  201. video_note=file_id,
  202. )
  203. if data_type == "audio":
  204. await message.reply_audio(
  205. audio=file_id,
  206. caption=data,
  207. reply_markup=keyb,
  208. )
  209. if data_type == "voice":
  210. await message.reply_voice(
  211. voice=file_id,
  212. caption=data,
  213. reply_markup=keyb,
  214. )