This commit is contained in:
syuilo 2020-08-29 15:52:33 +09:00
parent 36b06ebbd7
commit f2dadeecc5
4 changed files with 90 additions and 0 deletions

View file

@ -30,6 +30,10 @@ export type InstallerResult = {
timeoutCallback?: TimeoutCallback;
};
export type Meta = {
lastWakingAt: number;
};
/**
*
*/
@ -41,6 +45,9 @@ export default class 藍 {
private contextHooks: { [moduleName: string]: ContextHook } = {};
private timeoutCallbacks: { [moduleName: string]: TimeoutCallback } = {};
public db: loki;
public lastSleepedAt: number;
private meta: loki.Collection<Meta>;
private contexts: loki.Collection<{
isDm: boolean;
@ -96,6 +103,8 @@ export default class 藍 {
@autobind
private run() {
//#region Init DB
this.meta = this.getCollection('meta', {});
this.contexts = this.getCollection('contexts', {
indices: ['key']
});
@ -113,6 +122,9 @@ export default class 藍 {
});
//#endregion
const meta = this.getMeta();
this.lastSleepedAt = meta.lastWakingAt;
// Init stream
this.connection = new Stream();
@ -173,6 +185,8 @@ export default class 藍 {
this.crawleTimer();
setInterval(this.crawleTimer, 1000);
setInterval(this.logWaking, 10000);
this.log(chalk.green.bold('Ai am now running!'));
}
@ -258,6 +272,13 @@ export default class 藍 {
}
}
@autobind
private logWaking() {
this.setMeta({
lastWakingAt: Date.now(),
});
}
/**
*
*/
@ -395,4 +416,31 @@ export default class 藍 {
this.log(`Timer persisted: ${module.name} ${id} ${delay}ms`);
}
@autobind
public getMeta() {
const rec = this.meta.findOne();
if (rec) {
return rec;
} else {
const initial: Meta = {
lastWakingAt: Date.now(),
};
this.meta.insertOne(initial);
return initial;
}
}
@autobind
public setMeta(meta: Partial<Meta>) {
const rec = this.getMeta();
for (const [k, v] of Object.entries(meta)) {
rec[k] = v;
}
this.meta.update(rec);
}
}

View file

@ -26,6 +26,7 @@ import FollowModule from './modules/follow';
import ValentineModule from './modules/valentine';
import MazeModule from './modules/maze';
import ChartModule from './modules/chart';
import SleepReportModule from './modules/sleep-report';
console.log(' __ ____ _____ ___ ');
console.log(' /__\\ (_ _)( _ )/ __)');
@ -75,6 +76,7 @@ promiseRetry(retry => {
new KeywordModule(),
new MazeModule(),
new ChartModule(),
new SleepReportModule(),
]);
}).catch(e => {
log(chalk.red('Failed to fetch the account'));

View file

@ -0,0 +1,35 @@
import autobind from 'autobind-decorator';
import Module from '../../module';
import serifs from '../../serifs';
export default class extends Module {
public readonly name = 'sleepReport';
@autobind
public install() {
this.report();
return {};
}
@autobind
private report() {
const now = Date.now();
const sleepTime = now - this.ai.lastSleepedAt;
const sleepHours = sleepTime / 1000 / 60 / 60;
if (sleepHours < 0.1) return;
if (sleepHours >= 1) {
this.ai.post({
text: serifs.sleepReport.report(Math.round(sleepHours))
});
} else {
this.ai.post({
text: serifs.sleepReport.reportUtatane
});
}
}
}

View file

@ -355,6 +355,11 @@ export default {
post: 'インスタンスの投稿数です!',
foryou: '描きました!'
},
sleepReport: {
report: hours => `んぅ、${hours}時間くらい寝ちゃってたみたいです`,
reportUtatane: 'ん... うたた寝しちゃってました',
},
};
export function getSerif(variant: string | string[]): string {