blacklist.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. from pyrogram import filters
  21. from wbb import app
  22. from wbb.core.decorators.errors import capture_err
  23. from wbb.core.decorators.permissions import adminsOnly
  24. from wbb.services.blacklist_enforcement import (
  25. add_risk_keyword,
  26. enforce_risk_message,
  27. get_effective_risk_rules,
  28. remove_risk_keyword,
  29. risk_rule_keywords,
  30. )
  31. from wbb.services.external_risk_rules import enforce_external
  32. from wbb.utils.filter_groups import blacklist_filters_group
  33. __MODULE__ = "黑名单"
  34. __HELP__ = """
  35. /blacklisted - 查看本群全部黑名单关键词。
  36. /blacklist [词语或句子] - 添加黑名单关键词。
  37. /whitelist [词语或句子] - 移除黑名单关键词。
  38. """
  39. @app.on_message(filters.command("blacklist") & ~filters.private)
  40. @adminsOnly("can_restrict_members")
  41. async def save_filters(_, message):
  42. if len(message.command) < 2:
  43. return await message.reply_text("用法:\n/blacklist [词语或句子]")
  44. word = message.text.split(None, 1)[1].strip()
  45. if not word:
  46. return await message.reply_text(
  47. "**用法**\n__/blacklist [词语或句子]__"
  48. )
  49. chat_id = message.chat.id
  50. changed, _ = await add_risk_keyword(chat_id, word)
  51. if changed:
  52. return await message.reply_text(f"__**已将“{word}”加入风控关键词。**__")
  53. await message.reply_text("**该关键词已存在。**")
  54. @app.on_message(filters.command("blacklisted") & ~filters.private)
  55. @capture_err
  56. async def get_filterss(_, message):
  57. data = risk_rule_keywords(
  58. await get_effective_risk_rules(message.chat.id), enabled_only=True
  59. )
  60. if not data:
  61. await message.reply_text("**本群没有黑名单关键词。**")
  62. else:
  63. msg = f"{message.chat.title} 的风控关键词:\n"
  64. for word in data:
  65. msg += f"**-** `{word}`\n"
  66. await message.reply_text(msg)
  67. @app.on_message(filters.command("whitelist") & ~filters.private)
  68. @adminsOnly("can_restrict_members")
  69. async def del_filter(_, message):
  70. if len(message.command) < 2:
  71. return await message.reply_text("用法:\n/whitelist [词语或句子]")
  72. word = message.text.split(None, 1)[1].strip()
  73. if not word:
  74. return await message.reply_text("用法:\n/whitelist [词语或句子]")
  75. chat_id = message.chat.id
  76. deleted, _ = await remove_risk_keyword(chat_id, word)
  77. if deleted:
  78. return await message.reply_text(f"**已将“{word}”移出黑名单。**")
  79. await message.reply_text("**未找到该黑名单关键词。**")
  80. @app.on_message(
  81. (
  82. filters.text
  83. | filters.caption
  84. | filters.photo
  85. | filters.animation
  86. | filters.document
  87. )
  88. & ~filters.private,
  89. group=blacklist_filters_group,
  90. )
  91. @capture_err
  92. async def blacklist_filters_re(_, message):
  93. result = await enforce_risk_message(message)
  94. await enforce_external(message, already_deleted=bool(result and result.deleted))