リファクタリングなど

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>; public friends: loki.Collection<FriendDoc>;
constructor(account: User, ready: (run: Function) => void) { constructor(account: User, modules: Module[]) {
this.account = account; this.account = account;
this.modules = modules;
this.log('Lodaing the memory...'); this.log('Lodaing the memory...');
@ -63,7 +64,7 @@ export default class 藍 {
this.log(chalk.red(`Failed to load the memory: ${err}`)); this.log(chalk.red(`Failed to load the memory: ${err}`));
} else { } else {
this.log(chalk.green('The memory loaded successfully')); this.log(chalk.green('The memory loaded successfully'));
ready(this.run); this.run();
} }
} }
}); });
@ -116,6 +117,7 @@ export default class 藍 {
// Install modules // Install modules
this.modules.forEach(m => { this.modules.forEach(m => {
this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`); this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`);
m.init(this);
const res = m.install(); const res = m.install();
if (res != null) { if (res != null) {
if (res.mentionHook) this.mentionHooks.push(res.mentionHook); 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')); log(chalk.bold('Ai v1.0'));
promiseRetry(retry => { promiseRetry(retry => {
log(`Account fetching... (${config.host})`); log(`Account fetching... ${chalk.gray(config.host)}`);
return request.post(`${config.apiUrl}/i`, { return request.post(`${config.apiUrl}/i`, {
json: { json: {
i: config.i i: config.i
@ -37,28 +37,27 @@ promiseRetry(retry => {
}, { }, {
retries: 3 retries: 3
}).then(account => { }).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...'); log('Starting AiOS...');
const ai = new (account, run => { new (account, [
new EmojiModule(ai); new EmojiModule(),
new FortuneModule(ai); new FortuneModule(),
new GuessingGameModule(ai); new GuessingGameModule(),
new ReversiModule(ai); new ReversiModule(),
new TimerModule(ai); new TimerModule(),
new DiceModule(ai); new DiceModule(),
new CoreModule(ai); new CoreModule(),
new PingModule(ai); new PingModule(),
new WelcomeModule(ai); new WelcomeModule(),
new ServerModule(ai); new ServerModule(),
new FollowModule(ai); new FollowModule(),
new BirthdayModule(ai); new BirthdayModule(),
new ValentineModule(ai); new ValentineModule(),
if (config.keywordEnabled) new KeywordModule(ai); new KeywordModule(),
]);
run();
});
}).catch(e => { }).catch(e => {
log(chalk.red('Failed to fetch the account')); log(chalk.red('Failed to fetch the account'));
}); });

View file

@ -6,9 +6,8 @@ export default abstract class Module {
protected ai: ; protected ai: ;
constructor(ai: ) { public init(ai: ) {
this.ai = ai; this.ai = ai;
this.ai.modules.push(this);
} }
public abstract install(): InstallerResult; public abstract install(): InstallerResult;

View file

@ -24,6 +24,8 @@ export default class KeywordModule extends Module {
@autobind @autobind
public install() { public install() {
if (!config.keywordEnabled) return {};
//#region Init DB //#region Init DB
this.learnedKeywords = getCollection(this.ai.db, '_keyword_learnedKeywords', { this.learnedKeywords = getCollection(this.ai.db, '_keyword_learnedKeywords', {
indices: ['userId'] indices: ['userId']

View file

@ -1,7 +1,9 @@
import chalk from 'chalk';
export default function(msg: string) { export default function(msg: string) {
const now = new Date(); const now = new Date();
const date = `${zeroPad(now.getHours())}:${zeroPad(now.getMinutes())}:${zeroPad(now.getSeconds())}`; 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 { function zeroPad(num: number, length: number = 2): string {