Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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", desc="判断消息中是否有敏感词、决定是否回复。", version="1.0", author="lanvent", desire_priority= 100)
  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: %s" % e)
  39. def on_handle_context(self, e_context: EventContext):
  40. if e_context['context'].type not in [ContextType.TEXT,ContextType.IMAGE_CREATE]:
  41. return
  42. content = e_context['context'].content
  43. logger.debug("[Banwords] on_handle_context. content: %s" % content)
  44. if self.action == "ignore":
  45. f = self.searchr.FindFirst(content)
  46. if f:
  47. logger.info("Banwords: %s" % f["Keyword"])
  48. e_context.action = EventAction.BREAK_PASS
  49. return
  50. elif self.action == "replace":
  51. if self.searchr.ContainsAny(content):
  52. reply = Reply(ReplyType.INFO, "发言中包含敏感词,请重试: \n"+self.searchr.Replace(content))
  53. e_context['reply'] = reply
  54. e_context.action = EventAction.BREAK_PASS
  55. return