リファクタリングなど

This commit is contained in:
syuilo 2019-01-16 02:10:42 +09:00
parent c32c871d4d
commit 09fc4291f0
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
5 changed files with 29 additions and 25 deletions

View file

@ -49,8 +49,9 @@ export default class 藍 {
public friends: loki.Collection<FriendDoc>;
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);

View file

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

View file

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

View file

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

View file

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