filters.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """
  2. MIT License
  3. Copyright (c) 2024 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 pyrogram.types import (
  23. CallbackQuery,
  24. InlineKeyboardButton,
  25. InlineKeyboardMarkup,
  26. )
  27. from wbb import app
  28. from wbb.core.decorators.errors import capture_err
  29. from wbb.core.decorators.permissions import adminsOnly
  30. from wbb.core.keyboard import ikb
  31. from wbb.modules.admin import member_permissions
  32. from wbb.modules.notes import extract_urls
  33. from wbb.utils.dbfunctions import (
  34. delete_filter,
  35. deleteall_filters,
  36. get_filter,
  37. get_filters_names,
  38. save_filter,
  39. )
  40. from wbb.utils.filter_groups import chat_filters_group
  41. from wbb.utils.functions import (
  42. check_format,
  43. extract_text_and_keyb,
  44. get_data_and_name,
  45. )
  46. __MODULE__ = "Filters"
  47. __HELP__ = """/filters To Get All The Filters In The Chat.
  48. /filter [FILTER_NAME] To Save A Filter(reply to a message).
  49. Supported filter types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice.
  50. To use more words in a filter use.
  51. `/filter Hey_there` To filter "Hey there".
  52. /stop [FILTER_NAME] To Stop A Filter.
  53. /stopall To delete all the filters in a chat (permanently).
  54. You can use markdown or html to save text too.
  55. Checkout /markdownhelp to know more about formattings and other syntax.
  56. """
  57. @app.on_message(filters.command("filter") & ~filters.private)
  58. @adminsOnly("can_change_info")
  59. async def save_filters(_, message):
  60. try:
  61. if len(message.command) < 2:
  62. return await message.reply_text(
  63. "**Usage:**\nReply to a message with /filter [FILTER_NAME] [CONTENT] To set a new filter."
  64. )
  65. replied_message = message.reply_to_message
  66. if not replied_message:
  67. replied_message = message
  68. data, name = await get_data_and_name(replied_message, message)
  69. if data == "error":
  70. return await message.reply_text(
  71. "**Usage:**\n__/filter [FILTER_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/filter [FILTER_NAME]."
  72. )
  73. if replied_message.text:
  74. _type = "text"
  75. file_id = None
  76. if replied_message.sticker:
  77. _type = "sticker"
  78. file_id = replied_message.sticker.file_id
  79. if replied_message.animation:
  80. _type = "animation"
  81. file_id = replied_message.animation.file_id
  82. if replied_message.photo:
  83. _type = "photo"
  84. file_id = replied_message.photo.file_id
  85. if replied_message.document:
  86. _type = "document"
  87. file_id = replied_message.document.file_id
  88. if replied_message.video:
  89. _type = "video"
  90. file_id = replied_message.video.file_id
  91. if replied_message.video_note:
  92. _type = "video_note"
  93. file_id = replied_message.video_note.file_id
  94. if replied_message.audio:
  95. _type = "audio"
  96. file_id = replied_message.audio.file_id
  97. if replied_message.voice:
  98. _type = "voice"
  99. file_id = replied_message.voice.file_id
  100. if replied_message.reply_markup and not re.findall(r"\[.+\,.+\]", data):
  101. urls = extract_urls(replied_message.reply_markup)
  102. if urls:
  103. response = "\n".join(
  104. [f"{name}=[{text}, {url}]" for name, text, url in urls]
  105. )
  106. data = data + response
  107. if data:
  108. data = await check_format(ikb, data)
  109. if not data:
  110. return await message.reply_text(
  111. "**Wrong formatting, check the help section.**"
  112. )
  113. name = name.replace("_", " ")
  114. _filter = {
  115. "type": _type,
  116. "data": data,
  117. "file_id": file_id,
  118. }
  119. chat_id = message.chat.id
  120. await save_filter(chat_id, name, _filter)
  121. return await message.reply_text(f"__**Saved filter {name}.**__")
  122. except UnboundLocalError:
  123. return await message.reply_text(
  124. "**Replied message is inaccessible.\n`Forward the message and try again`**"
  125. )
  126. @app.on_message(filters.command("filters") & ~filters.private)
  127. @capture_err
  128. async def get_filterss(_, message):
  129. _filters = await get_filters_names(message.chat.id)
  130. if not _filters:
  131. return await message.reply_text("**No filters in this chat.**")
  132. _filters.sort()
  133. msg = f"List of filters in {message.chat.title} :\n"
  134. for _filter in _filters:
  135. msg += f"**-** `{_filter}`\n"
  136. await message.reply_text(msg)
  137. @app.on_message(filters.command("stop") & ~filters.private)
  138. @adminsOnly("can_change_info")
  139. async def del_filter(_, message):
  140. if len(message.command) < 2:
  141. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  142. name = message.text.split(None, 1)[1].strip()
  143. if not name:
  144. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  145. chat_id = message.chat.id
  146. deleted = await delete_filter(chat_id, name)
  147. if deleted:
  148. await message.reply_text(f"**Deleted filter {name}.**")
  149. else:
  150. await message.reply_text("**No such filter.**")
  151. @app.on_message(
  152. filters.text & ~filters.private & ~filters.channel & ~filters.via_bot & ~filters.forwarded,
  153. group=chat_filters_group,
  154. )
  155. @capture_err
  156. async def filters_re(_, message):
  157. from_user = message.from_user if message.from_user else message.sender_chat
  158. user_id = from_user.id
  159. chat_id = message.chat.id
  160. text = message.text.lower().strip()
  161. if not text:
  162. return
  163. chat_id = message.chat.id
  164. list_of_filters = await get_filters_names(chat_id)
  165. for word in list_of_filters:
  166. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  167. if re.search(pattern, text, flags=re.IGNORECASE):
  168. _filter = await get_filter(chat_id, word)
  169. data_type = _filter["type"]
  170. data = _filter["data"]
  171. file_id = _filter.get("file_id")
  172. keyb = None
  173. if data:
  174. if "{chat}" in data:
  175. data = data.replace(
  176. "{chat}", message.chat.title
  177. )
  178. if "{name}" in data:
  179. data = data.replace(
  180. "{name}", (from_user.mention if message.from_user else from_user.title)
  181. )
  182. if re.findall(r"\[.+\,.+\]", data):
  183. keyboard = extract_text_and_keyb(ikb, data)
  184. if keyboard:
  185. data, keyb = keyboard
  186. replied_message = message.reply_to_message
  187. if replied_message:
  188. replied_user = replied_message.from_user if replied_message.from_user else replied_message.sender_chat
  189. if text.startswith("~"):
  190. await message.delete()
  191. if replied_user.id != from_user.id:
  192. message = replied_message
  193. if data_type == "text":
  194. await message.reply_text(
  195. text=data,
  196. reply_markup=keyb,
  197. disable_web_page_preview=True,
  198. )
  199. else:
  200. if not file_id:
  201. continue
  202. if data_type == "sticker":
  203. await message.reply_sticker(
  204. sticker=file_id,
  205. )
  206. if data_type == "animation":
  207. await message.reply_animation(
  208. animation=file_id,
  209. caption=data,
  210. reply_markup=keyb,
  211. )
  212. if data_type == "photo":
  213. await message.reply_photo(
  214. photo=file_id,
  215. caption=data,
  216. reply_markup=keyb,
  217. )
  218. if data_type == "document":
  219. await message.reply_document(
  220. document=file_id,
  221. caption=data,
  222. reply_markup=keyb,
  223. )
  224. if data_type == "video":
  225. await message.reply_video(
  226. video=file_id,
  227. caption=data,
  228. reply_markup=keyb,
  229. )
  230. if data_type == "video_note":
  231. await message.reply_video_note(
  232. video_note=file_id,
  233. )
  234. if data_type == "audio":
  235. await message.reply_audio(
  236. audio=file_id,
  237. caption=data,
  238. reply_markup=keyb,
  239. )
  240. if data_type == "voice":
  241. await message.reply_voice(
  242. voice=file_id,
  243. caption=data,
  244. reply_markup=keyb,
  245. )
  246. return # NOTE: Avoid filter spam
  247. @app.on_message(filters.command("stopall") & ~filters.private)
  248. @adminsOnly("can_change_info")
  249. async def stop_all(_, message):
  250. _filters = await get_filters_names(message.chat.id)
  251. if not _filters:
  252. await message.reply_text("**No filters in this chat.**")
  253. else:
  254. keyboard = InlineKeyboardMarkup(
  255. [
  256. [
  257. InlineKeyboardButton(
  258. "YES, DO IT", callback_data="stop_yes"
  259. ),
  260. InlineKeyboardButton("Cancel", callback_data="stop_no"),
  261. ]
  262. ]
  263. )
  264. await message.reply_text(
  265. "**Are you sure you want to delete all the filters in this chat forever ?.**",
  266. reply_markup=keyboard,
  267. )
  268. @app.on_callback_query(filters.regex("stop_(.*)"))
  269. async def stop_all_cb(_, cb):
  270. chat_id = cb.message.chat.id
  271. from_user = cb.from_user
  272. permissions = await member_permissions(chat_id, from_user.id)
  273. permission = "can_change_info"
  274. if permission not in permissions:
  275. return await cb.answer(
  276. f"You don't have the required permission.\n Permission: {permission}",
  277. show_alert=True,
  278. )
  279. input = cb.data.split("_", 1)[1]
  280. if input == "yes":
  281. stoped_all = await deleteall_filters(chat_id)
  282. if stoped_all:
  283. return await cb.message.edit(
  284. "**Successfully deleted all filters on this chat.**"
  285. )
  286. if input == "no":
  287. await cb.message.reply_to_message.delete()
  288. await cb.message.delete()