filters.py 10 KB

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