From 140d3c324d594ddb277a5298d3ee7480817abd60 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 15 Jan 2019 12:29:11 +0900 Subject: [PATCH] Refactor --- src/ai.ts | 10 +++++++--- src/friend.ts | 22 +++++++++++++++------- src/index.ts | 5 +++-- src/log.ts | 9 +++++++++ src/message-like.ts | 13 +++++++++---- 5 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 src/log.ts diff --git a/src/ai.ts b/src/ai.ts index 3589e67..b800eb9 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -11,6 +11,7 @@ import { FriendDoc } from './friend'; import { User } from './misskey/user'; import getCollection from './utils/get-collection'; import Stream from './stream'; +import log from './log'; type MentionHook = (msg: MessageLike) => boolean | HandlerResult; type ContextHook = (msg: MessageLike, data?: any) => void | HandlerResult; @@ -49,14 +50,17 @@ export default class 藍 { constructor(account: User, ready: (run: Function) => void) { this.account = account; + this.log('Lodaing the memory...'); + this.db = new loki('memory.json', { autoload: true, autosave: true, autosaveInterval: 1000, autoloadCallback: err => { if (err) { - this.log(chalk.red(`Failed to load DB: ${err}`)); + this.log(chalk.red(`Failed to load the memory: ${err}`)); } else { + this.log(chalk.green('The memory loaded successfully')); ready(this.run); } } @@ -65,7 +69,7 @@ export default class 藍 { @autobind public log(msg: string) { - console.log(`[AiOS]: ${msg}`); + log(chalk`[{magenta AiOS}]: ${msg}`); } @autobind @@ -122,7 +126,7 @@ export default class 藍 { @autobind private onMention(msg: MessageLike) { - this.log(`mention received: ${msg.id}`); + this.log(chalk.gray(`<<< An message received: ${chalk.underline(msg.id)}`)); const context = !msg.isMessage && msg.replyId == null ? null : this.contexts.findOne(msg.isMessage ? { isMessage: true, diff --git a/src/friend.ts b/src/friend.ts index 6b76073..ebad8e0 100644 --- a/src/friend.ts +++ b/src/friend.ts @@ -1,3 +1,4 @@ +import autobind from 'autobind-decorator'; import 藍 from './ai'; import IModule from './module'; import getDate from './utils/get-date'; @@ -52,12 +53,14 @@ export default class Friend { } } - public updateUser = (user: User) => { + @autobind + public updateUser(user: User) { this.doc.user = user; this.save(); } - public getPerModulesData = (module: IModule) => { + @autobind + public getPerModulesData(module: IModule) { if (this.doc.perModulesData == null) { this.doc.perModulesData = {}; this.doc.perModulesData[module.name] = {}; @@ -70,7 +73,8 @@ export default class Friend { return this.doc.perModulesData[module.name]; } - public setPerModulesData = (module: IModule, data: any) => { + @autobind + public setPerModulesData(module: IModule, data: any) { if (this.doc.perModulesData == null) { this.doc.perModulesData = {}; } @@ -80,7 +84,8 @@ export default class Friend { this.save(); } - public incLove = () => { + @autobind + public incLove() { const today = getDate(); if (this.doc.lastLoveIncrementedAt != today) { @@ -101,7 +106,8 @@ export default class Friend { this.save(); } - public decLove = () => { + @autobind + public decLove() { if (this.doc.love == null) this.doc.love = 0; this.doc.love--; @@ -116,12 +122,14 @@ export default class Friend { this.save(); } - public updateName = (name: string) => { + @autobind + public updateName(name: string) { this.doc.name = name; this.save(); } - public save = () => { + @autobind + public save() { this.ai.friends.update(this.doc); } } diff --git a/src/index.ts b/src/index.ts index 4cea891..aa15b8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import 藍 from './ai'; import config from './config'; +import _log from './log'; import CoreModule from './modules/core'; import BirthdayModule from './modules/birthday'; @@ -21,13 +22,13 @@ import * as request from 'request-promise-native'; const promiseRetry = require('promise-retry'); function log(msg: string): void { - console.log(`[Boot]: ${msg}`); + _log(`[Boot]: ${msg}`); } log(chalk.bold('Ai v1.0')); promiseRetry(retry => { - log(`Account fetching... >>> ${config.host}`); + log(`Account fetching... (${config.host})`); return request.post(`${config.apiUrl}/i`, { json: { i: config.i diff --git a/src/log.ts b/src/log.ts new file mode 100644 index 0000000..ef58a2d --- /dev/null +++ b/src/log.ts @@ -0,0 +1,9 @@ +export default function(msg: string) { + const now = new Date(); + const date = `${zeroPad(now.getHours())}:${zeroPad(now.getMinutes())}:${zeroPad(now.getSeconds())}`; + console.log(`${date} ${msg}`); +} + +function zeroPad(num: number, length: number = 2): string { + return ('0000000000' + num).slice(-length); +} diff --git a/src/message-like.ts b/src/message-like.ts index 4751f3b..d4128f6 100644 --- a/src/message-like.ts +++ b/src/message-like.ts @@ -1,8 +1,10 @@ +import autobind from 'autobind-decorator'; import 藍 from './ai'; import Friend from './friend'; import { User } from './misskey/user'; import includes from './utils/includes'; import or from './utils/or'; +import chalk from 'chalk'; const delay = require('timeout-as-promise'); export default class MessageLike { @@ -47,10 +49,11 @@ export default class MessageLike { }); } - public reply = async (text: string, cw?: string) => { + @autobind + public async reply(text: string, cw?: string) { if (text == null) return; - this.ai.log(`sending reply of ${this.id} ...`); + this.ai.log(`>>> Sending reply to ${chalk.underline(this.id)}`); await delay(2000); @@ -67,11 +70,13 @@ export default class MessageLike { } } - public includes = (words: string[]): boolean => { + @autobind + public includes(words: string[]): boolean { return includes(this.text, words); } - public or = (words: (string | RegExp)[]): boolean => { + @autobind + public or(words: (string | RegExp)[]): boolean { return or(this.text, words); } }