No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

67 líneas
2.5KB

  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. logger.info("[Banwords] inited")
  37. except Exception as e:
  38. logger.warn("[Banwords] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/banwords .")
  39. raise e
  40. def on_handle_context(self, e_context: EventContext):
  41. if e_context['context'].type not in [ContextType.TEXT,ContextType.IMAGE_CREATE]:
  42. return
  43. content = e_context['context'].content
  44. logger.debug("[Banwords] on_handle_context. content: %s" % content)
  45. if self.action == "ignore":
  46. f = self.searchr.FindFirst(content)
  47. if f:
  48. logger.info("Banwords: %s" % f["Keyword"])
  49. e_context.action = EventAction.BREAK_PASS
  50. return
  51. elif self.action == "replace":
  52. if self.searchr.ContainsAny(content):
  53. reply = Reply(ReplyType.INFO, "发言中包含敏感词,请重试: \n"+self.searchr.Replace(content))
  54. e_context['reply'] = reply
  55. e_context.action = EventAction.BREAK_PASS
  56. return
  57. def get_help_text(self, **kwargs):
  58. return Banwords.desc