57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { readFileSync } from "fs";
|
|
import { isSameDay, format, differenceInYears } from "date-fns/fp";
|
|
import config from "../config.js";
|
|
|
|
export default async function birthdayNotice() {
|
|
// 読み込み
|
|
const birthdays: { [key: string]: string | undefined } =
|
|
JSON.parse(readFileSync("data/birthdays.json", "utf-8"));
|
|
|
|
// 配列化
|
|
const birthdaysIndex: string[] = Object.entries(birthdays)
|
|
.map(([key, value]) => value)
|
|
.filter(value => value !== undefined) as string[];
|
|
|
|
// 初期値
|
|
const resultInitial: string = `
|
|
【今日誕生日の人】\n`;
|
|
|
|
let result = resultInitial;
|
|
|
|
for (let i = 0; i < Object.keys(birthdays).length; i++) {
|
|
const birthday = format(birthdaysIndex[i], "yyyy/MM/dd")
|
|
|
|
if (isSameDay(birthday, new Date())) {
|
|
const age = differenceInYears(new Date(), birthday);
|
|
|
|
const req = await fetch(`https://${config.uwuzu.host}/api/users/`, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
token: config.uwuzu.apiToken,
|
|
userid: Object.keys(birthdays)[i],
|
|
}),
|
|
});
|
|
|
|
const res = await req.json();
|
|
|
|
result+= `${res.username}さん(${age}歳)\n`
|
|
}
|
|
}
|
|
|
|
if (result === resultInitial) {
|
|
return;
|
|
}
|
|
|
|
const req = await fetch(`https://${config.uwuzu.host}/api/ueuse/create`, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
token: config.uwuzu.apiToken,
|
|
text: result,
|
|
}),
|
|
});
|
|
|
|
const res = await req.json();
|
|
|
|
console.log("誕生日お知らせ:", res);
|
|
}
|