Compare commits

...

5 Commits

6 changed files with 46 additions and 35 deletions
+13 -10
View File
@@ -2,25 +2,28 @@ import type { configTypes } from "types/config";
// READMEの設定項目を参照 // READMEの設定項目を参照
const config: configTypes = { const config: configTypes = {
// 時報設定
time: { time: {
// 時報休止期間
stopTimes: { stopTimes: {
start: new Date("23:00"), start: 23, // 開始
stop: new Date("6:00"), stop: 6, // 停止
} }
}, },
// 地震速報設定
earthquake: { earthquake: {
reconnectTimes: 5000, reconnectTimes: 5000, // 再接続時間(ミリ秒)
websocketUrl: "wss://api.p2pquake.net/v2/ws", websocketUrl: "wss://api.p2pquake.net/v2/ws", // WebSocketのURL
areasCsvUrl: "https://raw.githubusercontent.com/p2pquake/epsp-specifications/master/epsp-area.csv", areasCsvUrl: "https://raw.githubusercontent.com/p2pquake/epsp-specifications/master/epsp-area.csv", // 対象地域CSVファイルのURL
maxScaleMax: 30, maxScaleMin: 30, // 地震発生の際の最低震度(10-70)
rateLimit: 30, rateLimit: 30, // 地域情報更新のレート制限(分)
}, },
weather: { weather: {
splitCount: 4, splitCount: 4, // 返信の分割数
}, },
apiToken: "TOKEN_EXAMPLE", apiToken: "TOKEN_EXAMPLE", // BOTアカウントのAPIトークン
uwuzuServer: "uwuzu.example.com", uwuzuServer: "uwuzu.example.com", // uwuzuのサーバー
}; };
export default config; export default config;
+1 -1
View File
@@ -26,4 +26,4 @@ cron.schedule("1 7 * * *", () => {
}); });
// コンソールで表示 // コンソールで表示
console.log("サーバーが起動しました"); console.log("BOTサーバーが起動しました");
+5 -4
View File
@@ -1,6 +1,6 @@
{ {
"name": "noticeuwuzu", "name": "noticeuwuzu",
"version": "v4.0@uwuzu1.5.4", "version": "v4.2@uwuzu1.5.4",
"description": "uwuzu Notice Bot", "description": "uwuzu Notice Bot",
"main": "dist/main.js", "main": "dist/main.js",
"scripts": { "scripts": {
@@ -12,16 +12,17 @@
"uwuzu", "uwuzu",
"bot", "bot",
"cron", "cron",
"notice",
"weather",
"time", "time",
"timenotice", "earthquake"
"notice"
], ],
"author": { "author": {
"name": "Last2014", "name": "Last2014",
"url": "https://last2014.com", "url": "https://last2014.com",
"email": "info@last2014.com" "email": "info@last2014.com"
}, },
"license": "ISC", "license": "Apache-2.0",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@types/date-fns": "^2.5.3", "@types/date-fns": "^2.5.3",
+12 -9
View File
@@ -1,9 +1,9 @@
import WebSocket from "ws"; import WebSocket from "ws";
import { differenceInMinutes } from "date-fns"; import { differenceInMinutes, subMinutes } from "date-fns";
import config from "../config.js"; import config from "../config.js";
let rateLimit: Date; let rateLimit: Date | null = null;
class P2PEarthquakeClient { class P2PEarthquakeClient {
private ws: WebSocket | null = null; private ws: WebSocket | null = null;
@@ -63,6 +63,8 @@ class P2PEarthquakeClient {
} }
private handleMessage(message: any): void { private handleMessage(message: any): void {
console.log("----------------");
switch (message.code) { switch (message.code) {
case 551: // 地震情報 case 551: // 地震情報
console.log("地震情報を受信しました"); console.log("地震情報を受信しました");
@@ -72,12 +74,12 @@ class P2PEarthquakeClient {
console.log("緊急地震速報を受信しました"); console.log("緊急地震速報を受信しました");
this.executeEventFunc(message); this.executeEventFunc(message);
break; break;
case 555: // 震度情報 case 555: // 地域情報更新情報
console.log("震度情報を受信しました"); console.log("地域情報更新情報を受信しました");
this.executeEventFunc(message); this.executeEventFunc(message);
break; break;
default: default:
console.log(`その他の情報 (コード: ${message.code})`); console.log(`未対応の情報を受信しました(コード: ${message.code})`);
break; break;
} }
} }
@@ -144,7 +146,7 @@ async function areaMap(): Promise<Record<number, string>> {
// 情報受信 // 情報受信
async function event(earthquakeInfo: any): Promise<void> { async function event(earthquakeInfo: any): Promise<void> {
console.log(JSON.stringify(earthquakeInfo)); console.log(`受信データ:${JSON.stringify(earthquakeInfo)}`);
// ----処理---- // ----処理----
@@ -243,7 +245,8 @@ async function event(earthquakeInfo: any): Promise<void> {
// 最大震度 // 最大震度
let maxScale: string = "最大深度:"; let maxScale: string = "最大深度:";
if (earthquakeInfo.earthquake.maxScale < config.earthquake.maxScaleMax) { if (earthquakeInfo.earthquake.maxScale < config.earthquake.maxScaleMin) {
console.log("最低震度に満たしていないため投稿されませんでした");
return; return;
} }
@@ -331,7 +334,7 @@ async function event(earthquakeInfo: any): Promise<void> {
// 地域情報更新の場合 // 地域情報更新の場合
else if (earthquakeInfo.code === 555) { else if (earthquakeInfo.code === 555) {
if (rateLimit === null) { if (rateLimit === null) {
rateLimit = new Date(); rateLimit = subMinutes(new Date(), 15);
} }
// 対象地域マッピング // 対象地域マッピング
@@ -349,7 +352,7 @@ async function event(earthquakeInfo: any): Promise<void> {
const areas = areaNames.join("・"); const areas = areaNames.join("・");
if (differenceInMinutes(rateLimit, new Date()) >= config.earthquake.rateLimit) { if (Math.abs(differenceInMinutes(rateLimit, new Date())) >= config.earthquake.rateLimit) {
ueuse(` ueuse(`
==地震情報== ==地震情報==
【地域情報更新】 【地域情報更新】
+12 -8
View File
@@ -1,20 +1,24 @@
import { format, isAfter, isBefore } from "date-fns"; import { format } from "date-fns";
import type * as types from "types/types"; import type * as types from "types/types";
import config from "../config.js"; import config from "../config.js";
// 停止時間
// 時刻取得 // 時刻取得
const now = new Date();
const start = config.time.stopTimes.start; const start = config.time.stopTimes.start;
const stop = config.time.stopTimes.stop; const stop = config.time.stopTimes.stop;
// 停止時刻外 // 現在の時間を取得
let inRange: Boolean = false; const nowHour = new Date().getHours();
if (isBefore(start, stop)) { // 停止時刻内かどうかの判定
inRange = isAfter(now, start) && isBefore(now, stop); let inRange: boolean = false;
if (start < stop) {
inRange = nowHour >= start && nowHour < stop;
} else { } else {
inRange = isAfter(now, start) || isBefore(now, stop); inRange = nowHour >= start || nowHour < stop;
} }
@@ -27,7 +31,7 @@ export default async function timeNotice() {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
token: config.apiToken, token: config.apiToken,
text: `${now}になりました`, text: `${format(new Date(), "HH:mm")}になりました`,
}), }),
}, },
); );
+3 -3
View File
@@ -2,7 +2,7 @@ interface earthquakeTypes {
reconnectTimes: number; reconnectTimes: number;
websocketUrl: string; websocketUrl: string;
areasCsvUrl: string; areasCsvUrl: string;
maxScaleMax: number; maxScaleMin: number;
rateLimit: number; rateLimit: number;
} }
@@ -11,8 +11,8 @@ interface weatherTypes {
} }
interface stopsTypes { interface stopsTypes {
start: Date; start: number;
stop: Date; stop: number;
} }
interface timeTypes { interface timeTypes {