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.

102 satır
3.8KB

  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. # loading config from global plugin config
  25. conf = super().load_config()
  26. if not conf:
  27. if not os.path.exists(config_path):
  28. conf = {"action": "ignore"}
  29. with open(config_path, "w") as f:
  30. json.dump(conf, f, indent=4)
  31. else:
  32. with open(config_path, "r") as f:
  33. conf = super().load_config() or json.load(f)
  34. self.searchr = WordsSearch()
  35. self.action = conf["action"]
  36. banwords_path = os.path.join(curdir, "banwords.txt")
  37. with open(banwords_path, "r", encoding="utf-8") as f:
  38. words = []
  39. for line in f:
  40. word = line.strip()
  41. if word:
  42. words.append(word)
  43. self.searchr.SetKeywords(words)
  44. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  45. if conf.get("reply_filter", True):
  46. self.handlers[Event.ON_DECORATE_REPLY] = self.on_decorate_reply
  47. self.reply_action = conf.get("reply_action", "ignore")
  48. logger.info("[Banwords] inited")
  49. except Exception as e:
  50. logger.warn("[Banwords] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/banwords .")
  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(ReplyType.INFO, "发言中包含敏感词,请重试: \n" + self.searchr.Replace(content))
  69. e_context["reply"] = reply
  70. e_context.action = EventAction.BREAK_PASS
  71. return
  72. def on_decorate_reply(self, e_context: EventContext):
  73. if e_context["reply"].type not in [ReplyType.TEXT]:
  74. return
  75. reply = e_context["reply"]
  76. content = reply.content
  77. if self.reply_action == "ignore":
  78. f = self.searchr.FindFirst(content)
  79. if f:
  80. logger.info("[Banwords] %s in reply" % f["Keyword"])
  81. e_context["reply"] = None
  82. e_context.action = EventAction.BREAK_PASS
  83. return
  84. elif self.reply_action == "replace":
  85. if self.searchr.ContainsAny(content):
  86. reply = Reply(ReplyType.INFO, "已替换回复中的敏感词: \n" + self.searchr.Replace(content))
  87. e_context["reply"] = reply
  88. e_context.action = EventAction.CONTINUE
  89. return
  90. def get_help_text(self, **kwargs):
  91. return "过滤消息中的敏感词。"