二度同じキーワードを覚えないように

This commit is contained in:
syuilo 2018-08-27 17:24:04 +09:00
parent f60a48659b
commit af1e75ade3
2 changed files with 40 additions and 26 deletions

View file

@ -1,3 +1,4 @@
import * as loki from 'lokijs';
import from '../../ai'; import from '../../ai';
import IModule from '../../module'; import IModule from '../../module';
import config from '../../config'; import config from '../../config';
@ -12,22 +13,37 @@ 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';
private ai: ; private ai: ;
private tokenizer: any; private tokenizer: any;
private learnedKeywords: loki.Collection<{
keyword: string;
learnedAt: number;
}>;
public install = (ai: ) => { public install = (ai: ) => {
this.ai = ai; this.ai = ai;
//#region Init DB
this.learnedKeywords = this.ai.db.getCollection(db);
if (this.learnedKeywords === null) {
this.learnedKeywords = this.ai.db.addCollection(db, {
indices: ['keyword']
});
}
//#endregion
this.tokenizer = new MeCab(); this.tokenizer = new MeCab();
this.tokenizer.command = config.mecab; this.tokenizer.command = config.mecab;
setInterval(this.say, 1000 * 60 * 60); setInterval(this.say, 1000 * 60 * 60);
} }
private say = async (msg?: MessageLike) => { private say = async () => {
const tl = await this.ai.api('notes/local-timeline', { const tl = await this.ai.api('notes/local-timeline', {
limit: 30 limit: 30
}); });
@ -44,33 +60,31 @@ export default class KeywordModule implements IModule {
}); });
}))); })));
console.log(keywords);
const rnd = Math.floor((1 - Math.sqrt(Math.random())) * keywords.length); const rnd = Math.floor((1 - Math.sqrt(Math.random())) * keywords.length);
const keyword = keywords.sort((a, b) => a[0].length < b[0].length ? 1 : -1)[rnd]; const keyword = keywords.sort((a, b) => a[0].length < b[0].length ? 1 : -1)[rnd];
const text = serifs.KEYWORD const exist = this.learnedKeywords.findOne({
.replace('{word}', keyword[0]) keyword: keyword[0]
.replace('{reading}', kanaToHira(keyword[8])) });
if (msg) { let text: string;
msg.reply(text);
if (exist) {
text = serifs.keyword.learned
.replace('{word}', keyword[0])
.replace('{reading}', kanaToHira(keyword[8]));
} else { } else {
this.ai.post({ this.learnedKeywords.insertOne({
text: text keyword: keyword[0],
learnedAt: Date.now()
}); });
}
}
public onMention = (msg: MessageLike) => { text = serifs.keyword.remembered
if (msg.user.isAdmin && msg.isMessage && msg.text && msg.text.includes('なんか皆に言って')) { .replace('{reading}', kanaToHira(keyword[8]));
this.say();
return true;
} else if (msg.text && msg.text.includes('なんか言って')) {
this.say(msg);
return true;
} else {
return false;
} }
this.ai.post({
text: text
});
} }
} }

View file

@ -10,6 +10,11 @@ export default {
tooLong: '長すぎる気がします...' tooLong: '長すぎる気がします...'
}, },
keyword: {
learned: '({word}..... {reading}..... 覚えました)',
remembered: '{reading}'
},
/** /**
* *
*/ */
@ -98,11 +103,6 @@ export default {
*/ */
GUESSINGGAME_CONGRATS: '正解です🎉 ({tries}回目で当てました)', GUESSINGGAME_CONGRATS: '正解です🎉 ({tries}回目で当てました)',
/**
*
*/
KEYWORD: '({word}..... {reading}..... 覚えました)',
timer: { timer: {
set: 'わかりました!', set: 'わかりました!',
invalid: 'うーん...', invalid: 'うーん...',