filters.py 12 KB

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