|
| 1 | +package chatgpt |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/yqchilde/wxbot/engine/control" |
| 7 | + "github.com/yqchilde/wxbot/engine/pkg/log" |
| 8 | + "github.com/yqchilde/wxbot/engine/robot" |
| 9 | +) |
| 10 | + |
| 11 | +var sensitiveWords = []string{"中国", "党", "杀人", "放火", "炸弹", "枪", "毒品", "赌博", "1989", "天安门事件", "政治改革", "镇压", "死亡", "暴力", "民主运动", "军队", "游行"} |
| 12 | + |
| 13 | +func initSensitiveWords() { |
| 14 | + // insert system sensitive words |
| 15 | + for _, word := range sensitiveWords { |
| 16 | + db.Orm.Table("sensitive").FirstOrCreate(&SensitiveWords{Type: 1, Word: word}, "word = ?", word) |
| 17 | + } |
| 18 | + |
| 19 | + // all sensitive words |
| 20 | + var words []SensitiveWords |
| 21 | + if err := db.Orm.Table("sensitive").Where("deleted = 0").Find(&words).Error; err != nil { |
| 22 | + log.Errorf("[ChatGPT] 获取敏感词失败, error:%s", err.Error()) |
| 23 | + return |
| 24 | + } |
| 25 | + sensitiveWords = []string{} |
| 26 | + for _, word := range words { |
| 27 | + sensitiveWords = append(sensitiveWords, word.Word) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// 检查敏感词 |
| 32 | +func checkSensitiveWords(content string) bool { |
| 33 | + for _, word := range sensitiveWords { |
| 34 | + if strings.Contains(content, word) { |
| 35 | + return true |
| 36 | + } |
| 37 | + } |
| 38 | + return false |
| 39 | +} |
| 40 | + |
| 41 | +// 设置敏感词相关指令 |
| 42 | +func setSensitiveCommand(engine *control.Engine) { |
| 43 | + // 查看敏感词列表 |
| 44 | + engine.OnRegex("get chatgpt (sensitive|敏感词)", robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) { |
| 45 | + replyMsg := "当前敏感词列表: " + strings.Join(sensitiveWords, ",") |
| 46 | + log.Debugf("[ChatGPT] 敏感词: %s", replyMsg) |
| 47 | + ctx.ReplyTextAndAt("敏感词无法发出,请查阅日志输出") // 别尝试发出敏感词了,我试了会被吞消息 |
| 48 | + }) |
| 49 | + |
| 50 | + // 删除敏感词 |
| 51 | + engine.OnRegex("del chatgpt (sensitive|敏感词) (.+)", robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) { |
| 52 | + word := ctx.State["regex_matched"].([]string)[2] |
| 53 | + find := false |
| 54 | + for i := range sensitiveWords { |
| 55 | + if sensitiveWords[i] == word { |
| 56 | + sensitiveWords = append(sensitiveWords[:i], sensitiveWords[i+1:]...) |
| 57 | + find = true |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if !find { |
| 63 | + ctx.ReplyTextAndAt("敏感词不存在") |
| 64 | + return |
| 65 | + } |
| 66 | + if err := db.Orm.Table("sensitive").Where("word = ?", word).Update("deleted", 1).Error; err != nil { |
| 67 | + ctx.ReplyTextAndAt("删除敏感词失败") |
| 68 | + return |
| 69 | + } |
| 70 | + ctx.ReplyTextAndAt("删除敏感词成功") |
| 71 | + }) |
| 72 | + |
| 73 | + // 添加用户自定义敏感词 |
| 74 | + engine.OnRegex("set chatgpt (sensitive|敏感词) (.+)", robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) { |
| 75 | + word := ctx.State["regex_matched"].([]string)[2] |
| 76 | + words := strings.Split(word, ",") |
| 77 | + needs := words |
| 78 | + for i := range words { |
| 79 | + if words[i] == "" { |
| 80 | + needs = append(needs[:i], needs[i+1:]...) |
| 81 | + continue |
| 82 | + } |
| 83 | + for j := range sensitiveWords { |
| 84 | + if sensitiveWords[j] == words[i] { |
| 85 | + needs = append(needs[:i], needs[i+1:]...) |
| 86 | + break |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for i := range needs { |
| 92 | + sensitiveWords = append(sensitiveWords, needs[i]) |
| 93 | + db.Orm.Table("sensitive").Where("word = ?", needs[i]).Assign(map[string]interface{}{"deleted": 0}).FirstOrCreate(&SensitiveWords{Type: 2, Word: needs[i]}) |
| 94 | + } |
| 95 | + ctx.ReplyTextAndAt("添加敏感词成功") |
| 96 | + }) |
| 97 | +} |
0 commit comments