blacklist.py 5.1 KB

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