You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7KB

  1. # encoding:utf-8
  2. import json
  3. import os
  4. import plugins
  5. from bridge.context import ContextType
  6. from bridge.reply import Reply, ReplyType
  7. from common.log import logger
  8. from plugins import *
  9. from .lib.WordsSearch import WordsSearch
  10. @plugins.register(
  11. name="Banwords",
  12. desire_priority=100,
  13. hidden=True,
  14. desc="判断消息中是否有敏感词、决定是否回复。",
  15. version="1.0",
  16. author="lanvent",
  17. )
  18. class Banwords(Plugin):
  19. def __init__(self):
  20. super().__init__()
  21. try:
  22. curdir = os.path.dirname(__file__)
  23. config_path = os.path.join(curdir, "config.json")
  24. conf = None
  25. if not os.path.exists(config_path):
  26. conf = {"action": "ignore"}
  27. with open(config_path, "w") as f:
  28. json.dump(conf, f, indent=4)
  29. else:
  30. with open(config_path, "r") as f:
  31. conf = json.load(f)
  32. self.searchr = WordsSearch()
  33. self.action = conf["action"]
  34. banwords_path = os.path.join(curdir, "banwords.txt")
  35. with open(banwords_path, "r", encoding="utf-8") as f:
  36. words = []
  37. for line in f:
  38. word = line.strip()
  39. if word:
  40. words.append(word)
  41. self.searchr.SetKeywords(words)
  42. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  43. if conf.get("reply_filter", True):
  44. self.handlers[Event.ON_DECORATE_REPLY] = self.on_decorate_reply
  45. self.reply_action = conf.get("reply_action", "ignore")
  46. logger.info("[Banwords] inited")
  47. except Exception as e:
  48. logger.warn(
  49. "[Banwords] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/banwords ."
  50. )
  51. raise e
  52. def on_handle_context(self, e_context: EventContext):
  53. if e_context["context"].type not in [
  54. ContextType.TEXT,
  55. ContextType.IMAGE_CREATE,
  56. ]:
  57. return
  58. content = e_context["context"].content
  59. logger.debug("[Banwords] on_handle_context. content: %s" % content)
  60. if self.action == "ignore":
  61. f = self.searchr.FindFirst(content)
  62. if f:
  63. logger.info("[Banwords] %s in message" % f["Keyword"])
  64. e_context.action = EventAction.BREAK_PASS
  65. return
  66. elif self.action == "replace":
  67. if self.searchr.ContainsAny(content):
  68. reply = Reply(
  69. ReplyType.INFO, "发言中包含敏感词,请重试: \n" + self.searchr.Replace(content)
  70. )
  71. e_context["reply"] = reply
  72. e_context.action = EventAction.BREAK_PASS
  73. return
  74. def on_decorate_reply(self, e_context: EventContext):
  75. if e_context["reply"].type not in [ReplyType.TEXT]:
  76. return
  77. reply = e_context["reply"]
  78. content = reply.content
  79. if self.reply_action == "ignore":
  80. f = self.searchr.FindFirst(content)
  81. if f:
  82. logger.info("[Banwords] %s in reply" % f["Keyword"])
  83. e_context["reply"] = None
  84. e_context.action = EventAction.BREAK_PASS
  85. return
  86. elif self.reply_action == "replace":
  87. if self.searchr.ContainsAny(content):
  88. reply = Reply(
  89. ReplyType.INFO, "已替换回复中的敏感词: \n" + self.searchr.Replace(content)
  90. )
  91. e_context["reply"] = reply
  92. e_context.action = EventAction.CONTINUE
  93. return
  94. def get_help_text(self, **kwargs):
  95. return "过滤消息中的敏感词。"