From dd4a5beb03d4a578e6f2b66f01070d2eba5b6196 Mon Sep 17 00:00:00 2001 From: Last2014 Date: Wed, 27 May 2026 08:00:25 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=E7=B5=B5=E6=96=87=E5=AD=97=E3=81=8C?= =?UTF-8?q?=E6=AD=A3=E3=81=97=E3=81=8F=E3=82=AB=E3=82=A6=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=20/=20Fi?= =?UTF-8?q?x:=20=E6=8A=95=E7=A8=BF=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=20/=20Feat:=20=E6=8A=95=E7=A8=BF=E3=81=AE?= =?UTF-8?q?=E5=BE=85=E6=A9=9F=E6=99=82=E9=96=93=E3=82=92=E8=A1=A8=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/client.ts | 48 ++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/lib/client.ts b/src/lib/client.ts index 824dd95..24f07c2 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -17,7 +17,16 @@ type SuccessPosted = { }; } -const BASE_URL = "https://collapse.jp/api/v3/bot/"; +const getRealLength = (str: string): number => { + const segmenter = new Intl.Segmenter("ja", { granularity: "grapheme" }); + return [...segmenter.segment(str)].length; +}; + +const realSlice = (str: string, limit: number): string => { + const segmenter = new Intl.Segmenter("ja", { granularity: "grapheme" }); + const segments = [...segmenter.segment(str)]; + return segments.slice(0, limit).map(s => s.segment).join(""); +}; export const createPost = async (data: { text: string; @@ -27,6 +36,9 @@ export const createPost = async (data: { images?: [Blob, string][]; }, title: string) => { const excessedMessage = "**πŸ‘‰θΏ”δΏ‘γ«ηΆšγγŒγ‚γ‚ŠγΎγ™γ€‚**"; + + const excessedMessageLength = getRealLength(excessedMessage); + const eolLength = getRealLength(EOL); let lines = data.text.split(EOL); let firstUniqid = ""; @@ -40,23 +52,25 @@ export const createPost = async (data: { const maxLength = config.post.maxLength; - const limit = maxLength - (excessedMessage.length + EOL.length * 2); + const limit = maxLength - (excessedMessageLength + eolLength * 2); while (lines.length > 0) { const nextLine = lines[0]; if (nextLine === undefined) break; - if (nextLine.length > limit && currentText === "") { + const nextLineLength = getRealLength(nextLine); + if (nextLineLength > limit && currentText === "") { const targetLine = lines.shift() || ""; - currentText = targetLine.slice(0, limit); - - lines.unshift(targetLine.slice(limit)); + currentText = realSlice(targetLine, limit); + lines.unshift(realSlice(targetLine, limit * -1)); break; } - const potentialText = currentText ? currentText + EOL + nextLine : nextLine; - if (potentialText.length <= limit) { + const potentialText = currentText + ? currentText + EOL + nextLine + : nextLine; + if (getRealLength(potentialText) <= limit) { currentText = potentialText; lines.shift(); } else { @@ -79,25 +93,24 @@ export const createPost = async (data: { ? firstUniqid : data.replyid; - const body = new FormData(); - body.append("content", data.text); - body.append("zone", data.zone ?? "normal"); - body.append("reply_restriction", data.replyTarget ?? "all"); + const formData = new FormData(); + formData.append("content", currentText); + formData.append("zone", data.zone ?? "normal"); + formData.append("reply_restriction", data.replyTarget ?? "all"); if (replyid) { - body.append("reply_to_id", replyid); + formData.append("reply_to_id", replyid); } if (data.images) { - data.images.forEach(img => body.append("images", img[0], img[1])); + data.images.forEach(img => formData.append("images", img[0], img[1])); } - const req = await fetch(new URL("posts", BASE_URL), { + const req = await fetch(`https://collapse.jp/api/v3/bot/posts`, { method: "POST", - body, headers: { "Authorization": `Bearer ${config.mtweet.token}`, - "Content-Type": "application/json", "X-Idempotency-Key": String(process.hrtime.bigint()), }, + body: formData, }); const res = await req.json(); @@ -114,6 +127,7 @@ export const createPost = async (data: { const interval = res.error.details?.retry_after ? res.error.details.retry_after * 1000 : config.post.retryInterval; + console.log(`${interval}msεΎ…ζ©Ÿγ—γΎγ™...`); await new Promise(resolve => setTimeout(resolve, interval)); } } catch (err) {