filters.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 (replied_message.sticker or replied_message.video_note):
  70. data = None
  71. else:
  72. if replied_message and (replied_message.sticker or replied_message.video_note):
  73. data = None
  74. elif replied_message and not replied_message.text and not replied_message.caption:
  75. data = None
  76. else:
  77. data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
  78. match = "/filter " + name
  79. if not message.reply_to_message and message.text:
  80. if match == data:
  81. return await message.reply_text("**Usage:**\n__/filter [FILTER_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/filter [FILTER_NAME].")
  82. elif not message.reply_to_message and not message.text:
  83. if match == data:
  84. data = None
  85. if replied_message.text:
  86. _type = "text"
  87. file_id = None
  88. if replied_message.sticker:
  89. _type = "sticker"
  90. file_id = replied_message.sticker.file_id
  91. if replied_message.animation:
  92. _type = "animation"
  93. file_id = replied_message.animation.file_id
  94. if replied_message.photo:
  95. _type = "photo"
  96. file_id = replied_message.photo.file_id
  97. if replied_message.document:
  98. _type = "document"
  99. file_id = replied_message.document.file_id
  100. if replied_message.video:
  101. _type = "video"
  102. file_id = replied_message.video.file_id
  103. if replied_message.video_note:
  104. _type = "video_note"
  105. file_id = replied_message.video_note.file_id
  106. if replied_message.audio:
  107. _type = "audio"
  108. file_id = replied_message.audio.file_id
  109. if replied_message.voice:
  110. _type = "voice"
  111. file_id = replied_message.voice.file_id
  112. if replied_message.reply_markup and not "~" in data:
  113. urls = extract_urls(replied_message.reply_markup)
  114. if urls:
  115. response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
  116. data = data + response
  117. if data:
  118. data = await check_format(ikb, data)
  119. if not data:
  120. return await message.reply_text("**Wrong formatting, check the help section.**")
  121. name = name.replace("_", " ")
  122. _filter = {
  123. "type": _type,
  124. "data": data,
  125. "file_id": file_id,
  126. }
  127. await save_filter(chat_id, name, _filter)
  128. return await message.reply_text(f"__**Saved filter {name}.**__")
  129. except UnboundLocalError:
  130. return await message.reply_text("**Replied message is inaccessible.\n`Forward the message and try again`**")
  131. @app.on_message(filters.command("filters") & ~filters.private)
  132. @capture_err
  133. async def get_filterss(_, message):
  134. _filters = await get_filters_names(message.chat.id)
  135. if not _filters:
  136. return await message.reply_text("**No filters in this chat.**")
  137. _filters.sort()
  138. msg = f"List of filters in {message.chat.title} :\n"
  139. for _filter in _filters:
  140. msg += f"**-** `{_filter}`\n"
  141. await message.reply_text(msg)
  142. @app.on_message(filters.command("stop") & ~filters.private)
  143. @adminsOnly("can_change_info")
  144. async def del_filter(_, message):
  145. if len(message.command) < 2:
  146. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  147. name = message.text.split(None, 1)[1].strip()
  148. if not name:
  149. return await message.reply_text("**Usage:**\n__/stop [FILTER_NAME]__")
  150. chat_id = message.chat.id
  151. deleted = await delete_filter(chat_id, name)
  152. if deleted:
  153. await message.reply_text(f"**Deleted filter {name}.**")
  154. else:
  155. await message.reply_text("**No such filter.**")
  156. @app.on_message(
  157. filters.text & ~filters.private & ~filters.via_bot & ~filters.forwarded,
  158. group=chat_filters_group,
  159. )
  160. @capture_err
  161. async def filters_re(_, message):
  162. user_id = message.from_user.id
  163. chat_id = message.chat.id
  164. text = message.text.lower().strip()
  165. if not text:
  166. return
  167. chat_id = message.chat.id
  168. list_of_filters = await get_filters_names(chat_id)
  169. for word in list_of_filters:
  170. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  171. if re.search(pattern, text, flags=re.IGNORECASE):
  172. _filter = await get_filter(chat_id, word)
  173. data_type = _filter["type"]
  174. data = _filter["data"]
  175. file_id = _filter["file_id"]
  176. keyb = None
  177. if data:
  178. if "{chat}" in data:
  179. data = data.replace("{chat}", (await app.get_chat(chat_id)).title)
  180. if "{name}" in data:
  181. data = data.replace("{name}", (await app.get_users(user_id)).mention)
  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. if text.startswith("~"):
  189. await message.delete()
  190. if replied_message.from_user.id != message.from_user.id:
  191. message = replied_message
  192. if data_type == "text":
  193. await message.reply_text(
  194. text=data,
  195. reply_markup=keyb,
  196. disable_web_page_preview=True,
  197. )
  198. if data_type == "sticker":
  199. await message.reply_sticker(
  200. sticker=file_id,
  201. )
  202. if data_type == "animation":
  203. await message.reply_animation(
  204. animation=file_id,
  205. caption=data,
  206. reply_markup=keyb,
  207. )
  208. if data_type == "photo":
  209. await message.reply_photo(
  210. photo=file_id,
  211. caption=data,
  212. reply_markup=keyb,
  213. )
  214. if data_type == "document":
  215. await message.reply_document(
  216. document=file_id,
  217. caption=data,
  218. reply_markup=keyb,
  219. )
  220. if data_type == "video":
  221. await message.reply_video(
  222. video=file_id,
  223. caption=data,
  224. reply_markup=keyb,
  225. )
  226. if data_type == "video_note":
  227. await message.reply_video_note(
  228. video_note=file_id,
  229. )
  230. if data_type == "audio":
  231. await message.reply_audio(
  232. audio=file_id,
  233. caption=data,
  234. reply_markup=keyb,
  235. )
  236. if data_type == "voice":
  237. await message.reply_voice(
  238. voice=file_id,
  239. caption=data,
  240. reply_markup=keyb,
  241. )
  242. @app.on_message(filters.command("stopall") & ~filters.private)
  243. @adminsOnly("can_change_info")
  244. async def stop_all(_, message):
  245. _filters = await get_filters_names(message.chat.id)
  246. if not _filters:
  247. await message.reply_text("**No filters in this chat.**")
  248. else:
  249. keyboard = InlineKeyboardMarkup(
  250. [
  251. [InlineKeyboardButton("YES, DO IT", callback_data="stop_yes"),
  252. InlineKeyboardButton("Cancel", callback_data="stop_no")
  253. ]
  254. ]
  255. )
  256. await message.reply_text("**Are you sure you want to delete all the filters in this chat forever ?.**", reply_markup=keyboard)
  257. @app.on_callback_query(filters.regex("stop_(.*)"))
  258. async def stop_all_cb(_, cb):
  259. chat_id = cb.message.chat.id
  260. from_user = cb.from_user
  261. permissions = await member_permissions(chat_id, from_user.id)
  262. permission = "can_change_info"
  263. if permission not in permissions:
  264. return await cb.answer(f"You don't have the required permission.\n Permission: {permission}", show_alert=True)
  265. input = cb.data.split("_", 1)[1]
  266. if input == "yes":
  267. stoped_all = await deleteall_filters(chat_id)
  268. if stoped_all:
  269. return await cb.message.edit("**Successfully deleted all filters on this chat.**")
  270. if input == "no":
  271. await cb.message.reply_to_message.delete()
  272. await cb.message.delete()