Refactoring

This commit is contained in:
syuilo 2019-01-24 09:34:03 +09:00
parent 4b13c6deae
commit 971d78a957
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
5 changed files with 21 additions and 26 deletions

View file

@ -11,7 +11,6 @@ import Module from './module';
import Message from './message'; import Message from './message';
import { FriendDoc } from './friend'; import { FriendDoc } from './friend';
import { User } from './misskey/user'; import { User } from './misskey/user';
import getCollection from './utils/get-collection';
import Stream from './stream'; import Stream from './stream';
import log from './utils/log'; import log from './utils/log';
@ -83,11 +82,11 @@ export default class 藍 {
@autobind @autobind
private run() { private run() {
//#region Init DB //#region Init DB
this.contexts = getCollection(this.db, 'contexts', { this.contexts = this.getCollection('contexts', {
indices: ['key'] indices: ['key']
}); });
this.friends = getCollection(this.db, 'friends', { this.friends = this.getCollection('friends', {
indices: ['userId'] indices: ['userId']
}); });
//#endregion //#endregion
@ -207,6 +206,22 @@ export default class 藍 {
} }
} }
/**
*
*/
@autobind
public getCollection(name: string, opts?: any): loki.Collection {
let collection: loki.Collection;
collection = this.db.getCollection(name);
if (collection == null) {
collection = this.db.addCollection(name, opts);
}
return collection;
}
/** /**
* 稿 * 稿
*/ */

View file

@ -3,7 +3,6 @@ import * as loki from 'lokijs';
import Module from '../../module'; import Module from '../../module';
import Message from '../../message'; import Message from '../../message';
import serifs from '../../serifs'; import serifs from '../../serifs';
import getCollection from '../../utils/get-collection';
export default class extends Module { export default class extends Module {
public readonly name = 'guessingGame'; public readonly name = 'guessingGame';
@ -19,11 +18,9 @@ export default class extends Module {
@autobind @autobind
public install() { public install() {
//#region Init DB this.guesses = this.ai.getCollection('guessingGame', {
this.guesses = getCollection(this.ai.db, 'guessingGame', {
indices: ['userId'] indices: ['userId']
}); });
//#endregion
return { return {
mentionHook: this.mentionHook, mentionHook: this.mentionHook,

View file

@ -3,7 +3,6 @@ import * as loki from 'lokijs';
import Module from '../../module'; import Module from '../../module';
import Message from '../../message'; import Message from '../../message';
import serifs from '../../serifs'; import serifs from '../../serifs';
import getCollection from '../../utils/get-collection';
import { User } from '../../misskey/user'; import { User } from '../../misskey/user';
type Game = { type Game = {
@ -23,7 +22,7 @@ export default class extends Module {
@autobind @autobind
public install() { public install() {
this.games = getCollection(this.ai.db, 'kazutori'); this.games = this.ai.getCollection('kazutori');
this.crawleGameEnd(); this.crawleGameEnd();
setInterval(this.crawleGameEnd, 1000); setInterval(this.crawleGameEnd, 1000);

View file

@ -3,7 +3,6 @@ import * as loki from 'lokijs';
import Module from '../../module'; import Module from '../../module';
import config from '../../config'; import config from '../../config';
import serifs from '../../serifs'; import serifs from '../../serifs';
import getCollection from '../../utils/get-collection';
const MeCab = require('mecab-async'); const MeCab = require('mecab-async');
function kanaToHira(str: string) { function kanaToHira(str: string) {
@ -26,11 +25,9 @@ export default class extends Module {
public install() { public install() {
if (!config.keywordEnabled) return {}; if (!config.keywordEnabled) return {};
//#region Init DB this.learnedKeywords = this.ai.getCollection('_keyword_learnedKeywords', {
this.learnedKeywords = getCollection(this.ai.db, '_keyword_learnedKeywords', {
indices: ['userId'] indices: ['userId']
}); });
//#endregion
this.tokenizer = new MeCab(); this.tokenizer = new MeCab();
this.tokenizer.command = config.mecab; this.tokenizer.command = config.mecab;

View file

@ -1,13 +0,0 @@
import * as loki from 'lokijs';
export default function(db: loki, name: string, opts?: any): loki.Collection {
let collection;
collection = db.getCollection(name);
if (collection == null) {
collection = db.addCollection(name, opts);
}
return collection;
}