fix: text split error

This commit is contained in:
ikechan8370 2025-01-03 14:27:00 +08:00
parent 43faf96cfe
commit 25520ba9fe

View file

@ -136,7 +136,8 @@ export class bym extends plugin {
// console.log(JSON.stringify(opt))
let rsp = await client.sendMessage(e.msg, opt)
let text = rsp.text
let texts = text.split(/(?<!\?)[。?\n](?!\?)/, 3)
let texts = customSplitRegex(text, /(?<!\?)[。?\n](?!\?)/, 3)
// let texts = text.split(/(?<!\?)[。?\n](?!\?)/, 3)
for (let t of texts) {
if (!t) {
continue
@ -189,3 +190,23 @@ function filterResponseChunk (msg) {
}
return msg
}
function customSplitRegex (text, regex, limit) {
const result = []
let match
let lastIndex = 0
const globalRegex = new RegExp(regex, 'g')
while ((match = globalRegex.exec(text)) !== null) {
if (result.length < limit - 1) {
result.push(text.slice(lastIndex, match.index))
lastIndex = match.index + match[0].length
} else {
break
}
}
// 添加剩余部分
result.push(text.slice(lastIndex))
return result
}