blacklist.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. from wbb import app, SUDOERS
  21. from wbb.modules.admin import member_permissions, list_admins
  22. from wbb.utils.dbfunctions import (
  23. save_blacklist_filter, get_blacklisted_words, delete_blacklist_filter
  24. )
  25. from pyrogram import filters
  26. from pyrogram.types import ChatPermissions
  27. from wbb.core.decorators.errors import capture_err
  28. from wbb.utils.filter_groups import blacklist_filters_group
  29. import re
  30. import traceback
  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(filters.command("blacklist") & ~filters.edited & ~filters.private)
  38. @capture_err
  39. async def save_filters(_, message):
  40. if len(message.command) < 2:
  41. await message.reply_text("Usage:\n/blacklist [WORD|SENTENCE]")
  42. return
  43. chat_id = message.chat.id
  44. user_id = message.from_user.id
  45. permissions = await member_permissions(chat_id, user_id)
  46. if "can_restrict_members" not in permissions and user_id not in SUDOERS:
  47. await message.reply_text("**You don't have enough permissions**")
  48. return
  49. word = message.text.split(None, 1)[1].strip()
  50. if not word:
  51. await message.reply_text("**Usage**\n__/blacklist [WORD|SENTENCE]__")
  52. return
  53. await save_blacklist_filter(chat_id, word)
  54. await message.reply_text(f"__**Blacklisted {word}.**__")
  55. @app.on_message(filters.command("blacklisted") & ~filters.edited & ~filters.private)
  56. @capture_err
  57. async def get_filterss(_, message):
  58. data = await get_blacklisted_words(message.chat.id)
  59. if not data:
  60. await message.reply_text("**No blacklisted words in this chat.**")
  61. else:
  62. msg = f"List of blacklisted words in {message.chat.title}\n"
  63. for word in data:
  64. msg += f"**-** `{word}`\n"
  65. await message.reply_text(msg)
  66. @app.on_message(filters.command("whitelist") & ~filters.edited & ~filters.private)
  67. @capture_err
  68. async def del_filter(_, message):
  69. if len(message.command) < 2:
  70. await message.reply_text("Usage:\n/whitelist [WORD|SENTENCE]")
  71. return
  72. chat_id = message.chat.id
  73. user_id = message.from_user.id
  74. permissions = await member_permissions(chat_id, user_id)
  75. if "can_restrict_members" not in permissions and user_id not in SUDOERS:
  76. await message.reply_text("**You don't have enough permissions**")
  77. return
  78. word = message.text.split(None, 1)[1].strip()
  79. if not word:
  80. await message.reply_text("Usage:\n/whitelist [WORD|SENTENCE]")
  81. return
  82. deleted = await delete_blacklist_filter(chat_id, word)
  83. if deleted:
  84. await message.reply_text(f"**Whitelist {word}.**")
  85. else:
  86. await message.reply_text("**No such blacklist filter.**")
  87. @app.on_message(filters.text & ~filters.private, group=blacklist_filters_group)
  88. async def blacklist_filters_re(_, message):
  89. text = message.text.lower().strip()
  90. if not text:
  91. return
  92. chat_id = message.chat.id
  93. user = message.from_user
  94. if not user:
  95. return
  96. if user.id in SUDOERS:
  97. return
  98. try:
  99. list_of_filters = await get_blacklisted_words(chat_id)
  100. for word in list_of_filters:
  101. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  102. if re.search(pattern, text, flags=re.IGNORECASE):
  103. if user.id in await list_admins(chat_id):
  104. return
  105. try:
  106. await message.chat.restrict_member(user.id, ChatPermissions())
  107. except Exception:
  108. return
  109. await app.send_message(chat_id, f"Muted {user.mention} [`{user.id}`] due to a blacklist "
  110. + f"match on {word}.")
  111. return
  112. except Exception as e:
  113. e = traceback.format_exc()
  114. print(e)
  115. pass