diff --git a/src/ai.ts b/src/ai.ts index aedc8ef..508eadc 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -49,8 +49,9 @@ export default class 藍 { public friends: loki.Collection; - constructor(account: User, ready: (run: Function) => void) { + constructor(account: User, modules: Module[]) { this.account = account; + this.modules = modules; this.log('Lodaing the memory...'); @@ -63,7 +64,7 @@ export default class 藍 { this.log(chalk.red(`Failed to load the memory: ${err}`)); } else { this.log(chalk.green('The memory loaded successfully')); - ready(this.run); + this.run(); } } }); @@ -116,6 +117,7 @@ export default class 藍 { // Install modules this.modules.forEach(m => { this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`); + m.init(this); const res = m.install(); if (res != null) { if (res.mentionHook) this.mentionHooks.push(res.mentionHook); diff --git a/src/index.ts b/src/index.ts index 852692a..4d116db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,7 @@ function log(msg: string): void { log(chalk.bold('Ai v1.0')); promiseRetry(retry => { - log(`Account fetching... (${config.host})`); + log(`Account fetching... ${chalk.gray(config.host)}`); return request.post(`${config.apiUrl}/i`, { json: { i: config.i @@ -37,28 +37,27 @@ promiseRetry(retry => { }, { retries: 3 }).then(account => { - log(chalk.green(`Account fetched successfully: @${account.username}`)); + const acct = `@${account.username}`; + log(chalk.green(`Account fetched successfully: ${chalk.underline(acct)}`)); log('Starting AiOS...'); - const ai = new 藍(account, run => { - new EmojiModule(ai); - new FortuneModule(ai); - new GuessingGameModule(ai); - new ReversiModule(ai); - new TimerModule(ai); - new DiceModule(ai); - new CoreModule(ai); - new PingModule(ai); - new WelcomeModule(ai); - new ServerModule(ai); - new FollowModule(ai); - new BirthdayModule(ai); - new ValentineModule(ai); - if (config.keywordEnabled) new KeywordModule(ai); - - run(); - }); + new 藍(account, [ + new EmojiModule(), + new FortuneModule(), + new GuessingGameModule(), + new ReversiModule(), + new TimerModule(), + new DiceModule(), + new CoreModule(), + new PingModule(), + new WelcomeModule(), + new ServerModule(), + new FollowModule(), + new BirthdayModule(), + new ValentineModule(), + new KeywordModule(), + ]); }).catch(e => { log(chalk.red('Failed to fetch the account')); }); diff --git a/src/module.ts b/src/module.ts index eec0a58..b2a84ff 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,9 +6,8 @@ export default abstract class Module { protected ai: 藍; - constructor(ai: 藍) { + public init(ai: 藍) { this.ai = ai; - this.ai.modules.push(this); } public abstract install(): InstallerResult; diff --git a/src/modules/keyword/index.ts b/src/modules/keyword/index.ts index 76ea27d..7fb6d33 100644 --- a/src/modules/keyword/index.ts +++ b/src/modules/keyword/index.ts @@ -24,6 +24,8 @@ export default class KeywordModule extends Module { @autobind public install() { + if (!config.keywordEnabled) return {}; + //#region Init DB this.learnedKeywords = getCollection(this.ai.db, '_keyword_learnedKeywords', { indices: ['userId'] diff --git a/src/utils/log.ts b/src/utils/log.ts index ef58a2d..8b9eba8 100644 --- a/src/utils/log.ts +++ b/src/utils/log.ts @@ -1,7 +1,9 @@ +import chalk from 'chalk'; + export default function(msg: string) { const now = new Date(); const date = `${zeroPad(now.getHours())}:${zeroPad(now.getMinutes())}:${zeroPad(now.getSeconds())}`; - console.log(`${date} ${msg}`); + console.log(`${chalk.gray(date)} ${msg}`); } function zeroPad(num: number, length: number = 2): string {