From af1e75ade3173783603e7f6ee5259a1c9d568375 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 27 Aug 2018 17:24:04 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E5=BA=A6=E5=90=8C=E3=81=98=E3=82=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AF=E3=83=BC=E3=83=89=E3=82=92=E8=A6=9A=E3=81=88?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/keyword/index.ts | 56 ++++++++++++++++++++++-------------- src/serifs.ts | 10 +++---- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/modules/keyword/index.ts b/src/modules/keyword/index.ts index a66955c..00ee0f9 100644 --- a/src/modules/keyword/index.ts +++ b/src/modules/keyword/index.ts @@ -1,3 +1,4 @@ +import * as loki from 'lokijs'; import 藍 from '../../ai'; import IModule from '../../module'; import config from '../../config'; @@ -12,22 +13,37 @@ function kanaToHira(str: string) { }); } +const db = '_keyword_learnedKeywords'; + export default class KeywordModule implements IModule { public name = 'keyword'; private ai: 藍; private tokenizer: any; + private learnedKeywords: loki.Collection<{ + keyword: string; + learnedAt: number; + }>; public install = (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.command = config.mecab; setInterval(this.say, 1000 * 60 * 60); } - private say = async (msg?: MessageLike) => { + private say = async () => { const tl = await this.ai.api('notes/local-timeline', { 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 keyword = keywords.sort((a, b) => a[0].length < b[0].length ? 1 : -1)[rnd]; - const text = serifs.KEYWORD - .replace('{word}', keyword[0]) - .replace('{reading}', kanaToHira(keyword[8])) + const exist = this.learnedKeywords.findOne({ + keyword: keyword[0] + }); - if (msg) { - msg.reply(text); + let text: string; + + if (exist) { + text = serifs.keyword.learned + .replace('{word}', keyword[0]) + .replace('{reading}', kanaToHira(keyword[8])); } else { - this.ai.post({ - text: text + this.learnedKeywords.insertOne({ + keyword: keyword[0], + learnedAt: Date.now() }); - } - } - public onMention = (msg: MessageLike) => { - if (msg.user.isAdmin && msg.isMessage && msg.text && msg.text.includes('なんか皆に言って')) { - this.say(); - return true; - } else if (msg.text && msg.text.includes('なんか言って')) { - this.say(msg); - return true; - } else { - return false; + text = serifs.keyword.remembered + .replace('{reading}', kanaToHira(keyword[8])); } + + this.ai.post({ + text: text + }); } } diff --git a/src/serifs.ts b/src/serifs.ts index 83053ad..f0b7a0a 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -10,6 +10,11 @@ export default { tooLong: '長すぎる気がします...' }, + keyword: { + learned: '({word}..... {reading}..... 覚えました)', + remembered: '{reading}!' + }, + /** * リバーシへの誘いを承諾するとき */ @@ -98,11 +103,6 @@ export default { */ GUESSINGGAME_CONGRATS: '正解です🎉 ({tries}回目で当てました)', - /** - * キーワード - */ - KEYWORD: '({word}..... {reading}..... 覚えました)', - timer: { set: 'わかりました!', invalid: 'うーん...?',