Skip to content

Commit

Permalink
fix: text split error
Browse files Browse the repository at this point in the history
  • Loading branch information
ikechan8370 committed Jan 3, 2025
1 parent 43faf96 commit 25520ba
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion apps/bym.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 25520ba

Please sign in to comment.