filters.py 12 KB

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