Del: noticeUwuzuのAA / Del: worker_threadsを廃止 / FIx: client.tsでパスが誤っている問題 / Chg: Collapseのアップデートに反映
This commit is contained in:
@@ -1,5 +0,0 @@
|
|||||||
# # ###### ##### ### ###### ####### # # # # # # ###### # #
|
|
||||||
## # # # # # # # # # # # # # # # # #
|
|
||||||
# ## # # # # # # ####### # # # # # # # # ## # #
|
|
||||||
# ## # # # # # # # # # # # # # # #
|
|
||||||
# # ###### # ### ###### ####### ###### # # ###### ###### ######
|
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "earthquake-bot-for-mtweet",
|
"name": "earthquake-bot-for-mtweet",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+46
-44
@@ -6,59 +6,61 @@ import { EOL } from "node:os";
|
|||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
import generateImage from "@/earthquake/generateImage";
|
import generateImage from "@/earthquake/generateImage";
|
||||||
|
|
||||||
if (config.earthquake?.useHistoryData) {
|
export default function earthquake() {
|
||||||
console.log("過去の地震情報を配信します");
|
if (config.earthquake?.useHistoryData) {
|
||||||
const history = JSON.parse(readFileSync(`${import.meta.dirname}/../../260420.json`, "utf-8"));
|
console.log("過去の地震情報を配信します");
|
||||||
history.reverse();
|
const history = JSON.parse(readFileSync(`${import.meta.dirname}/../../260420.json`, "utf-8"));
|
||||||
|
history.reverse();
|
||||||
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
processMessage(history[i]);
|
processMessage(history[i]);
|
||||||
i++;
|
i++;
|
||||||
}, 10 * 1000);
|
}, 10 * 1000);
|
||||||
} else {
|
} else {
|
||||||
const connect = () => {
|
const connect = () => {
|
||||||
const WEBSOCKET_URL = config.debug
|
const WEBSOCKET_URL = config.debug
|
||||||
? "wss://api-realtime-sandbox.p2pquake.net/v2/ws"
|
? "wss://api-realtime-sandbox.p2pquake.net/v2/ws"
|
||||||
: "wss://api.p2pquake.net/v2/ws";
|
: "wss://api.p2pquake.net/v2/ws";
|
||||||
|
|
||||||
console.log("P2P地震情報のWebSocketに接続します");
|
console.log("P2P地震情報のWebSocketに接続します");
|
||||||
const socket = new WebSocket(WEBSOCKET_URL);
|
const socket = new WebSocket(WEBSOCKET_URL);
|
||||||
|
|
||||||
socket.addEventListener("open", () => {
|
socket.addEventListener("open", () => {
|
||||||
console.log("P2P地震情報のWebSocketに接続しました");
|
console.log("P2P地震情報のWebSocketに接続しました");
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.addEventListener("close", (err) => {
|
socket.addEventListener("close", (err) => {
|
||||||
const interval = config.earthquake.reconnectInterval;
|
const interval = config.earthquake.reconnectInterval;
|
||||||
console.log(`WebSocketが切断されました。${interval / 1000}秒後に再接続します:`, err.reason);
|
console.log(`WebSocketが切断されました。${interval / 1000}秒後に再接続します:`, err.reason);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
connect();
|
connect();
|
||||||
}, interval);
|
}, interval);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.addEventListener("error", (err) => {
|
socket.addEventListener("error", (err) => {
|
||||||
console.error("WebSocketでエラーが発生しました:", err);
|
console.error("WebSocketでエラーが発生しました:", err);
|
||||||
socket.close();
|
socket.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.addEventListener("message", async (event) => {
|
socket.addEventListener("message", async (event) => {
|
||||||
let message;
|
let message;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
message = typeof event.data === "string"
|
message = typeof event.data === "string"
|
||||||
? JSON.parse(event.data)
|
? JSON.parse(event.data)
|
||||||
: event.data;
|
: event.data;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("地震情報メッセージのパースでエラーが発生:", err);
|
console.error("地震情報メッセージのパースでエラーが発生:", err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
processMessage(message);
|
processMessage(message);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const processMessage = async (message: any) => {
|
const processMessage = async (message: any) => {
|
||||||
|
|||||||
+2
-3
@@ -1,10 +1,9 @@
|
|||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import config from "@/lib/config";
|
import config from "@/lib/config";
|
||||||
import { styleText } from "node:util";
|
import { styleText } from "node:util";
|
||||||
import { Worker } from "node:worker_threads";
|
import earthquake from "@/earthquake";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(readFileSync(`${import.meta.dirname}/../asciiart.txt`, "utf-8"));
|
|
||||||
console.log(JSON.parse(readFileSync(`${import.meta.dirname}/../package.json`, "utf-8")).version);
|
console.log(JSON.parse(readFileSync(`${import.meta.dirname}/../package.json`, "utf-8")).version);
|
||||||
if (config.debug) {
|
if (config.debug) {
|
||||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||||
@@ -16,7 +15,7 @@ try {
|
|||||||
}
|
}
|
||||||
console.log();
|
console.log();
|
||||||
|
|
||||||
new Worker(`${import.meta.dirname}/earthquake/index.js`);
|
earthquake();
|
||||||
|
|
||||||
console.log("Botが起動しました");
|
console.log("Botが起動しました");
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|||||||
+13
-18
@@ -11,18 +11,18 @@ type SuccessPosted<T = string> = {
|
|||||||
post: {
|
post: {
|
||||||
id: T;
|
id: T;
|
||||||
content: string;
|
content: string;
|
||||||
zone: "normal" | string;
|
zone: "normal" | "free";
|
||||||
created_at: string;
|
created_at: string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const BASE_URL = "https://collapse.jp/api/v3/bot";
|
const BASE_URL = "https://collapse.jp/api/v3/bot/";
|
||||||
|
|
||||||
export const createPost = async (data: {
|
export const createPost = async (data: {
|
||||||
text: string;
|
text: string;
|
||||||
zone?: "normal" | string;
|
zone?: "normal" | "free";
|
||||||
replyTarget?: "all" | string;
|
replyTarget?: "all" | "followers" | "mutual" | "mentioned" | "none";
|
||||||
replyid?: string;
|
replyid?: string;
|
||||||
images?: [Blob, string][];
|
images?: [Blob, string][];
|
||||||
}, title: string) => {
|
}, title: string) => {
|
||||||
@@ -78,30 +78,25 @@ export const createPost = async (data: {
|
|||||||
const replyid = data.replyid === undefined && firstUniqid !== ""
|
const replyid = data.replyid === undefined && firstUniqid !== ""
|
||||||
? firstUniqid
|
? firstUniqid
|
||||||
: data.replyid;
|
: data.replyid;
|
||||||
const reply = replyid !== undefined
|
|
||||||
? `/${replyid}/reply`
|
|
||||||
: "";
|
|
||||||
const url = new URL(`/posts${reply}`, BASE_URL);
|
|
||||||
|
|
||||||
const body = new FormData();
|
const body = new FormData();
|
||||||
body.append("content", data.text);
|
body.append("content", data.text);
|
||||||
if (!replyid) {
|
body.append("zone", data.zone ?? "normal");
|
||||||
body.append("zone", data.zone ?? "normal");
|
body.append("reply_restriction", data.replyTarget ?? "all");
|
||||||
body.append("reply_restriction", data.replyTarget ?? "all");
|
if (replyid) {
|
||||||
|
body.append("reply_to_id", replyid);
|
||||||
}
|
}
|
||||||
if (data.images) {
|
if (data.images) {
|
||||||
data.images.forEach(img => body.append("images", img[0], img[1]));
|
data.images.forEach(img => body.append("images", img[0], img[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
const req = await fetch(url, {
|
const req = await fetch(new URL("posts", BASE_URL), {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body,
|
||||||
content: data.text,
|
|
||||||
zone: data.zone ?? "normal",
|
|
||||||
reply_restriction: data.replyTarget ?? "all",
|
|
||||||
}),
|
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${config.mtweet.token}`,
|
"Authorization": `Bearer ${config.mtweet.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Idempotency-Key": String(process.hrtime.bigint()),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -114,7 +109,7 @@ export const createPost = async (data: {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.warn(`${title}の投稿に失敗しました (試行 ${attempt}/${config.post.maxRetries}):`, res.error.message);
|
console.warn(`${title}の投稿に失敗しました (試行 ${attempt}/${config.post.maxRetries}):`, `${res.error.message}(${res.error.code})`);
|
||||||
if (attempt < config.post.maxRetries) {
|
if (attempt < config.post.maxRetries) {
|
||||||
const interval = res.error.details?.retry_after
|
const interval = res.error.details?.retry_after
|
||||||
? res.error.details.retry_after * 1000
|
? res.error.details.retry_after * 1000
|
||||||
|
|||||||
Reference in New Issue
Block a user