61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import * as fs from "fs";
|
|
import { isAfter } from "date-fns";
|
|
|
|
import config from "../config.js";
|
|
import sendMail from "../src/mailer.js";
|
|
|
|
export default function successExit() {
|
|
// 初期化
|
|
if (!fs.existsSync("logs/boot.json")) {
|
|
fs.writeFileSync("logs/boot.json", JSON.stringify({
|
|
start: new Date(),
|
|
stop: "",
|
|
}), "utf-8");
|
|
}
|
|
|
|
const iolog = JSON.parse(fs.readFileSync("logs/boot.json", "utf-8"));
|
|
|
|
if (config.emergency.isEnabled) {
|
|
// 前回の終了確認
|
|
const start = iolog.start;
|
|
const stop = iolog.stop;
|
|
|
|
if (isAfter(start, stop)) {
|
|
console.log("前回の終了が適切でない可能性があります");
|
|
|
|
if (config.emergency.mail.isEnabled) {
|
|
sendMail({
|
|
to: config.emergency.mail.to,
|
|
subject: "【警告】前回終了が不適切な可能性",
|
|
text: `
|
|
※noticeUwuzu自動送信によるメールです。
|
|
【警告】
|
|
BOT管理者さん、noticeUwuzu自動送信メールです。
|
|
BOTの前回終了で不適切なデータを検出しました。
|
|
これは適切な終了時にはデータを残しデータがない場合に送信されます。
|
|
電源を強制的に遮断するなどの行為による可能性があります。
|
|
その場合は今後やめ、OSからのシャットダウンを使用してください。
|
|
BOTのプログラムが破損していないかご確認ください。
|
|
`
|
|
});
|
|
}
|
|
|
|
console.log("----------------");
|
|
}
|
|
}
|
|
|
|
// 起動時に起動時刻を保存
|
|
iolog.start = new Date();
|
|
fs.writeFileSync("logs/boot.json", JSON.stringify(iolog), "utf-8");
|
|
|
|
// 終了時に終了時刻を保存
|
|
process.on("exit", () => {
|
|
const iolog = JSON.parse(fs.readFileSync("logs/boot.json", "utf-8"));
|
|
iolog.stop = new Date();
|
|
|
|
fs.writeFileSync("logs/boot.json", JSON.stringify(iolog), "utf-8");
|
|
});
|
|
}
|
|
|
|
successExit();
|