Skip to content

Commit

Permalink
optimize(bot): combine two preprocess loop
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 9, 2024
1 parent 3f1e32b commit 42d748f
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,25 @@ loop:
func preprocessMessageEvent(e *Event) {
msgs := message.ParseMessage(e.NativeMessage)

for i := 0; i < len(msgs)-1; i++ {
if msgs[i].Type == "at" && msgs[i+1].Type == "text" {
msgs[i+1].Data["text"] = strings.TrimSpace(msgs[i+1].Data["text"])
}
}
var filtered = make([]message.MessageSegment, 0, len(msgs))
// remove empty text segment
for i := 0; i < len(msgs); {
if msgs[i].Type != "text" || msgs[i].Data["text"] != "" {
filtered = append(filtered, msgs[i])
if len(msgs) > 0 {
filtered := make([]message.MessageSegment, 0, len(msgs))
// trim space after at and remove empty text segment
for i := range msgs {
if i < len(msgs)-1 && msgs[i].Type == "at" && msgs[i+1].Type == "text" {
msgs[i+1].Data["text"] = strings.TrimLeft(msgs[i+1].Data["text"], " ")
}
if msgs[i].Type != "text" || msgs[i].Data["text"] != "" {
filtered = append(filtered, msgs[i])
}
}
e.Message = filtered
}
e.Message = filtered

processAt := func() { // 处理是否at机器人
e.IsToMe = false
if len(e.Message) == 0 {
return
}
for i, m := range e.Message {
if m.Type == "at" {
qq, _ := strconv.ParseInt(m.Data["qq"], 10, 64)
Expand Down

0 comments on commit 42d748f

Please sign in to comment.