This commit is contained in:
syuilo 2019-01-15 12:29:11 +09:00
parent c95c2c76ef
commit 140d3c324d
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
5 changed files with 43 additions and 16 deletions

View file

@ -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,

View file

@ -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);
}
}

View file

@ -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

9
src/log.ts Normal file
View file

@ -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);
}

View file

@ -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);
}
}