Feat: ユーズの再試行 / Feat: ユーズの文字数制限回避 / Feat: ユーズ送信関数 / Chg: weatherNotice.tsのマジックナンバーに命名
This commit is contained in:
+61
-1
@@ -2,6 +2,8 @@ import uwuzu from "better-uwuzu-sdk";
|
||||
import config from "@/lib/config";
|
||||
import Parser from "better-uwuzu-sdk/1.6.8/parser";
|
||||
import ApiMap from "better-uwuzu-sdk/types/1.6.8/map";
|
||||
import Memory from "@/lib/memory";
|
||||
import { EOL } from "node:os";
|
||||
|
||||
const client = new uwuzu<ApiMap>({
|
||||
origin: config.uwuzu.origin,
|
||||
@@ -9,5 +11,63 @@ const client = new uwuzu<ApiMap>({
|
||||
});
|
||||
|
||||
client.token = config.uwuzu.token;
|
||||
export default client;
|
||||
|
||||
export default client;
|
||||
export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: string) => {
|
||||
const mem = Memory.memory;
|
||||
const maxLength = mem.max_length;
|
||||
const excessedMessage = "👉返信に続きがあります。";
|
||||
|
||||
let remainingText = data.text;
|
||||
let firstUniqid = "";
|
||||
|
||||
while (remainingText.length > 0) {
|
||||
let currentText = "";
|
||||
let bodyTextLength = 0;
|
||||
|
||||
if (remainingText.length <= maxLength) {
|
||||
currentText = remainingText;
|
||||
bodyTextLength = remainingText.length;
|
||||
remainingText = "";
|
||||
} else {
|
||||
bodyTextLength = maxLength - excessedMessage.length - 2;
|
||||
currentText = remainingText.slice(0, bodyTextLength) + EOL.repeat(2) + excessedMessage;
|
||||
remainingText = remainingText.slice(bodyTextLength);
|
||||
}
|
||||
|
||||
let postedUniqid = "";
|
||||
let success = false;
|
||||
|
||||
for (let attempt = 1; attempt <= config.ueuse.maxRetries; attempt++) {
|
||||
const response = await client.request("ueuse/create", {
|
||||
...data,
|
||||
text: currentText,
|
||||
replyid: data.replyid === undefined && firstUniqid !== ""
|
||||
? firstUniqid
|
||||
: data.replyid,
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
success = true;
|
||||
postedUniqid = response.uniqid;
|
||||
break;
|
||||
}
|
||||
|
||||
console.warn(`${title}の投稿に失敗しました (試行 ${attempt}/${config.ueuse.maxRetries}):`, response.error_code);
|
||||
if (attempt < config.ueuse.maxRetries) {
|
||||
await new Promise(resolve => setTimeout(resolve, config.ueuse.retryInterval));
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
console.error(`${title}の全試行が失敗したため、処理を中断します。`);
|
||||
break;
|
||||
}
|
||||
|
||||
if (firstUniqid === "") {
|
||||
firstUniqid = postedUniqid;
|
||||
}
|
||||
|
||||
console.log(`${title}を投稿:`, postedUniqid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user