blacklist.py 4.5 KB

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