From d46b8a9f30f7b89ca0b55f3e1fcd084d518dfd1a Mon Sep 17 00:00:00 2001 From: lz1998 <875543533@qq.com> Date: Mon, 8 Feb 2021 20:44:30 +0800 Subject: [PATCH] regex replace --- config/config.go | 12 +++++++----- service/bot/remote.go | 14 ++++---------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/config/config.go b/config/config.go index b657d5e..1a10427 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "encoding/json" + "github.com/pkg/errors" ) @@ -29,11 +30,12 @@ type GmcConfig struct { } type ServerGroup struct { - Name string `json:"name"` // 功能名称 - Disabled bool `json:"disabled"` // 不填false默认启用 - Urls []string `json:"urls"` // 服务器列表 - EventFilter []int32 `json:"event_filter"` // 事件过滤 - RegexFilter string `json:"regex_filter"` // 正则过滤 + Name string `json:"name"` // 功能名称 + Disabled bool `json:"disabled"` // 不填false默认启用 + Urls []string `json:"urls"` // 服务器列表 + EventFilter []int32 `json:"event_filter"` // 事件过滤 + RegexFilter string `json:"regex_filter"` // 正则过滤 + RegexReplace string `json:"regex_replace"` // 正则替换 // TODO event filter, msg filter, regex filter, prefix filter, suffix filter } diff --git a/service/bot/remote.go b/service/bot/remote.go index c6d4273..880e026 100644 --- a/service/bot/remote.go +++ b/service/bot/remote.go @@ -255,19 +255,13 @@ func HandleEventFrame(cli *client.QQClient, eventFrame *onebot.Frame) { if ws.regexp != nil { // 有prefix filter if e, ok := eventFrame.Data.(*onebot.Frame_PrivateMessageEvent); ok { - b := ws.regexp.Find([]byte(e.PrivateMessageEvent.RawMessage)) - if b == nil { - report = false - } else { - e.PrivateMessageEvent.RawMessage = string(b) + if report = ws.regexp.MatchString(e.PrivateMessageEvent.RawMessage); report && ws.RegexReplace != "" { + e.PrivateMessageEvent.RawMessage = ws.regexp.ReplaceAllString(e.PrivateMessageEvent.RawMessage, ws.RegexReplace) } } if e, ok := eventFrame.Data.(*onebot.Frame_GroupMessageEvent); ok { - b := ws.regexp.Find([]byte(e.GroupMessageEvent.RawMessage)) - if b == nil { - report = false - } else { - e.GroupMessageEvent.RawMessage = string(b) + if report = ws.regexp.MatchString(e.GroupMessageEvent.RawMessage); report && ws.RegexReplace != "" { + e.GroupMessageEvent.RawMessage = ws.regexp.ReplaceAllString(e.GroupMessageEvent.RawMessage, ws.RegexReplace) } } }