Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

91 rinda
3.6KB

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