-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
42 lines (36 loc) · 1.45 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { DateTime } from 'luxon';
import { api as misskeyApi } from 'misskey-js';
const misskeyToken = process.env.MISSKEY_TOKEN!;
const year = Number(process.env.YEAR!);
const timezone = process.env.TIMEZONE || 'Asia/Tokyo'; // default to 'Asia/Tokyo' See: https://moment.github.io/luxon/#/tour?id=time-zones
// Check all required variables are set.
if (typeof misskeyToken == undefined || typeof year == undefined) {
throw new Error('Some required ENV is not set (MISSKEY_TOKEN, YEAR)')
}
const currentDate = DateTime.now().setZone(timezone);
let todaysBirthday = DateTime.local(year, currentDate.month, currentDate.day, {zone: timezone});
// Check date is valid.
if(!todaysBirthday.isValid) {
// Recover if today is leap year day
if(currentDate.month !== 2 || currentDate.day !== 29) {
throw new Error(`Invalid date ${todaysBirthday.toISODate()}`);
}
// In this case, today is leap year day(Feb 29th) but your birth year does not have that day.
// Find other year that fits leap year day as your birthday.
var alternateYear = year;
while(!todaysBirthday.isValid) {
alternateYear--;
todaysBirthday = DateTime.local(alternateYear, currentDate.month, currentDate.day)
}
}
const cli = new misskeyApi.APIClient({
origin: 'https://misskey.io',
credential: misskeyToken,
});
(async () => {
const meta = await cli.request('i/update', {birthday: todaysBirthday.toISODate()});
console.log(meta);
})().catch((e) => {
console.error(e);
process.exitCode = 1;
});