From f2dadeecc5c4d87711299b4f17308d4214f6c499 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 29 Aug 2020 15:52:33 +0900 Subject: [PATCH] Resolve #59 --- src/ai.ts | 48 +++++++++++++++++++++++++++++++ src/index.ts | 2 ++ src/modules/sleep-report/index.ts | 35 ++++++++++++++++++++++ src/serifs.ts | 5 ++++ 4 files changed, 90 insertions(+) create mode 100644 src/modules/sleep-report/index.ts diff --git a/src/ai.ts b/src/ai.ts index 1c60743..582a9bd 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -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; 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) { + const rec = this.getMeta(); + + for (const [k, v] of Object.entries(meta)) { + rec[k] = v; + } + + this.meta.update(rec); + } } diff --git a/src/index.ts b/src/index.ts index 7faa7cf..92cee26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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')); diff --git a/src/modules/sleep-report/index.ts b/src/modules/sleep-report/index.ts new file mode 100644 index 0000000..bda02cc --- /dev/null +++ b/src/modules/sleep-report/index.ts @@ -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 + }); + } + } +} diff --git a/src/serifs.ts b/src/serifs.ts index 6103ca3..a32acea 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -355,6 +355,11 @@ export default { post: 'インスタンスの投稿数です!', foryou: '描きました!' }, + + sleepReport: { + report: hours => `んぅ、${hours}時間くらい寝ちゃってたみたいです`, + reportUtatane: 'ん... うたた寝しちゃってました', + }, }; export function getSerif(variant: string | string[]): string {