mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
try to support escape. #9
This commit is contained in:
parent
bc47267c3c
commit
4941f0c3f8
@ -31,7 +31,7 @@ func ToStringMessage(e []message.IMessageElement, code int64, raw ...bool) (r st
|
|||||||
for _, elem := range e {
|
for _, elem := range e {
|
||||||
switch o := elem.(type) {
|
switch o := elem.(type) {
|
||||||
case *message.TextElement:
|
case *message.TextElement:
|
||||||
r += o.Content
|
r += CQCodeEscapeText(o.Content)
|
||||||
case *message.AtElement:
|
case *message.AtElement:
|
||||||
if o.Target == 0 {
|
if o.Target == 0 {
|
||||||
r += "[CQ:at,qq=all]"
|
r += "[CQ:at,qq=all]"
|
||||||
@ -63,7 +63,7 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa
|
|||||||
for _, idx := range i {
|
for _, idx := range i {
|
||||||
if idx[0] > si {
|
if idx[0] > si {
|
||||||
text := m[si:idx[0]]
|
text := m[si:idx[0]]
|
||||||
r = append(r, message.NewText(text))
|
r = append(r, message.NewText(CQCodeUnescapeText(text)))
|
||||||
}
|
}
|
||||||
code := m[idx[0]:idx[1]]
|
code := m[idx[0]:idx[1]]
|
||||||
si = idx[1]
|
si = idx[1]
|
||||||
@ -105,7 +105,7 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa
|
|||||||
r = append(r, elem)
|
r = append(r, elem)
|
||||||
}
|
}
|
||||||
if si != len(m) {
|
if si != len(m) {
|
||||||
r = append(r, message.NewText(m[si:]))
|
r = append(r, message.NewText(CQCodeUnescapeText(m[si:])))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -309,3 +309,19 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
|
|||||||
return nil, errors.New("unsupported cq code: " + t)
|
return nil, errors.New("unsupported cq code: " + t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CQCodeEscapeText(raw string) string {
|
||||||
|
ret := raw
|
||||||
|
ret = strings.ReplaceAll(ret, "[", "[")
|
||||||
|
ret = strings.ReplaceAll(ret, "]", "]")
|
||||||
|
ret = strings.ReplaceAll(ret, "&", "&")
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func CQCodeUnescapeText(content string) string {
|
||||||
|
ret := content
|
||||||
|
ret = strings.ReplaceAll(ret, "[", "[")
|
||||||
|
ret = strings.ReplaceAll(ret, "]", "]")
|
||||||
|
ret = strings.ReplaceAll(ret, "&", "&")
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user