blacklist.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 ChatPermissions
  23. from wbb import SUDOERS, app
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.core.decorators.permissions import adminsOnly
  26. from wbb.modules.admin import list_admins
  27. from wbb.utils.dbfunctions import (delete_blacklist_filter,
  28. get_blacklisted_words,
  29. save_blacklist_filter)
  30. from wbb.utils.filter_groups import blacklist_filters_group
  31. __MODULE__ = "Blacklist"
  32. __HELP__ = """
  33. /blacklisted - Get All The Blacklisted Words In The Chat.
  34. /blacklist [WORD|SENTENCE] - Blacklist A Word Or A Sentence.
  35. /whitelist [WORD|SENTENCE] - Whitelist A Word Or A Sentence.
  36. """
  37. @app.on_message(
  38. filters.command("blacklist") & ~filters.edited & ~filters.private
  39. )
  40. @adminsOnly("can_restrict_members")
  41. async def save_filters(_, message):
  42. if len(message.command) < 2:
  43. return await message.reply_text(
  44. "Usage:\n/blacklist [WORD|SENTENCE]"
  45. )
  46. word = message.text.split(None, 1)[1].strip()
  47. if not word:
  48. return await message.reply_text(
  49. "**Usage**\n__/blacklist [WORD|SENTENCE]__"
  50. )
  51. chat_id = message.chat.id
  52. await save_blacklist_filter(chat_id, word)
  53. await message.reply_text(f"__**Blacklisted {word}.**__")
  54. @app.on_message(
  55. filters.command("blacklisted")
  56. & ~filters.edited
  57. & ~filters.private
  58. )
  59. @capture_err
  60. async def get_filterss(_, message):
  61. data = await get_blacklisted_words(message.chat.id)
  62. if not data:
  63. await message.reply_text(
  64. "**No blacklisted words in this chat.**"
  65. )
  66. else:
  67. msg = f"List of blacklisted words in {message.chat.title}\n"
  68. for word in data:
  69. msg += f"**-** `{word}`\n"
  70. await message.reply_text(msg)
  71. @app.on_message(
  72. filters.command("whitelist") & ~filters.edited & ~filters.private
  73. )
  74. @adminsOnly("can_restrict_members")
  75. async def del_filter(_, message):
  76. if len(message.command) < 2:
  77. return await message.reply_text(
  78. "Usage:\n/whitelist [WORD|SENTENCE]"
  79. )
  80. word = message.text.split(None, 1)[1].strip()
  81. if not word:
  82. return await message.reply_text(
  83. "Usage:\n/whitelist [WORD|SENTENCE]"
  84. )
  85. chat_id = message.chat.id
  86. deleted = await delete_blacklist_filter(chat_id, word)
  87. if deleted:
  88. return await message.reply_text(f"**Whitelisted {word}.**")
  89. await message.reply_text("**No such blacklist filter.**")
  90. @app.on_message(
  91. filters.text & ~filters.private, group=blacklist_filters_group
  92. )
  93. @capture_err
  94. async def blacklist_filters_re(_, message):
  95. text = message.text.lower().strip()
  96. if not text:
  97. return
  98. chat_id = message.chat.id
  99. user = message.from_user
  100. if not user:
  101. return
  102. if user.id in SUDOERS:
  103. return
  104. list_of_filters = await get_blacklisted_words(chat_id)
  105. for word in list_of_filters:
  106. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  107. if re.search(pattern, text, flags=re.IGNORECASE):
  108. if user.id in await list_admins(chat_id):
  109. return
  110. try:
  111. await message.chat.restrict_member(
  112. user.id, ChatPermissions()
  113. )
  114. except Exception:
  115. return
  116. await app.send_message(
  117. chat_id,
  118. f"Muted {user.mention} [`{user.id}`] due to a blacklist "
  119. + f"match on {word}.",
  120. )
  121. return