This commit is contained in:
syuilo 2018-08-29 16:26:33 +09:00
parent 256a2d25b0
commit 9654ffab42
4 changed files with 28 additions and 26 deletions

View file

@ -8,6 +8,7 @@ import IModule from './module';
import MessageLike from './message-like'; import MessageLike from './message-like';
import { FriendDoc } from './friend'; import { FriendDoc } from './friend';
import { User } from './misskey/user'; import { User } from './misskey/user';
import getCollection from './utils/get-collection';
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js'); const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
/** /**
@ -55,19 +56,13 @@ export default class 藍 {
private init = () => { private init = () => {
//#region Init DB //#region Init DB
this.contexts = this.db.getCollection('contexts'); this.contexts = getCollection(this.db, 'contexts', {
if (this.contexts === null) { indices: ['key']
this.contexts = this.db.addCollection('contexts', { });
indices: ['key']
});
}
this.friends = this.db.getCollection('friends'); this.friends = getCollection(this.db, 'friends', {
if (this.friends === null) { indices: ['userId']
this.friends = this.db.addCollection('friends', { });
indices: ['userId']
});
}
//#endregion //#endregion
// Install modules // Install modules

View file

@ -3,6 +3,7 @@ import 藍 from '../../ai';
import IModule from '../../module'; import IModule from '../../module';
import MessageLike from '../../message-like'; import MessageLike from '../../message-like';
import serifs from '../../serifs'; import serifs from '../../serifs';
import getCollection from '../../utils/get-collection';
export default class GuessingGameModule implements IModule { export default class GuessingGameModule implements IModule {
public name = 'guessingGame'; public name = 'guessingGame';
@ -20,12 +21,9 @@ export default class GuessingGameModule implements IModule {
this.ai = ai; this.ai = ai;
//#region Init DB //#region Init DB
this.guesses = this.ai.db.getCollection('guessingGame'); this.guesses = getCollection(this.ai.db, 'guessingGame', {
if (this.guesses === null) { indices: ['userId']
this.guesses = this.ai.db.addCollection('guessingGame', { });
indices: ['userId']
});
}
//#endregion //#endregion
} }

View file

@ -3,6 +3,7 @@ import 藍 from '../../ai';
import IModule from '../../module'; import IModule 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) {
@ -12,8 +13,6 @@ function kanaToHira(str: string) {
}); });
} }
const db = '_keyword_learnedKeywords';
export default class KeywordModule implements IModule { export default class KeywordModule implements IModule {
public name = 'keyword'; public name = 'keyword';
@ -28,12 +27,9 @@ export default class KeywordModule implements IModule {
this.ai = ai; this.ai = ai;
//#region Init DB //#region Init DB
this.learnedKeywords = this.ai.db.getCollection(db); this.learnedKeywords = getCollection(this.ai.db, '_keyword_learnedKeywords', {
if (this.learnedKeywords === null) { indices: ['userId']
this.learnedKeywords = this.ai.db.addCollection(db, { });
indices: ['keyword']
});
}
//#endregion //#endregion
this.tokenizer = new MeCab(); this.tokenizer = new MeCab();

View file

@ -0,0 +1,13 @@
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;
}