From 851088bc7988b85faa5167e27f553bad62288516 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 23 Aug 2018 17:27:39 +0900 Subject: [PATCH] nanka iroiro --- package.json | 1 + src/index.ts | 10 +++++++ src/modules/guessing-game/index.ts | 2 +- src/modules/reversi/index.ts | 2 +- src/modules/timer/index.ts | 47 ++++++++++++++++++++++++++++++ src/serifs.ts | 6 ++++ 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/modules/timer/index.ts diff --git a/package.json b/package.json index a1c746f..7487031 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "ai", "main": "./built/index.js", "scripts": { + "start": "node ./built", "build": "tsc" }, "dependencies": { diff --git a/src/index.ts b/src/index.ts index 3d5da75..5219265 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import 藍 from './ai'; import config from './config'; + import ReversiModule from './modules/reversi'; import ServerModule from './modules/server'; import PingModule from './modules/ping'; @@ -8,9 +9,13 @@ import FortuneModule from './modules/fortune'; import GuessingGameModule from './modules/guessing-game'; import KeywordModule from './modules/keyword'; import WelcomeModule from './modules/welcome'; +import TimerModule from './modules/timer'; + import * as request from 'request-promise-native'; const promiseRetry = require('promise-retry'); +console.log('starting ai...'); + promiseRetry(retry => { return request.post(`${config.apiUrl}/i`, { json: { @@ -18,6 +23,8 @@ promiseRetry(retry => { } }).catch(retry); }).then(account => { + console.log('account fetched'); + const ai = new 藍(account); ai.install(new PingModule()); @@ -27,5 +34,8 @@ promiseRetry(retry => { ai.install(new GuessingGameModule()); ai.install(new ServerModule()); ai.install(new ReversiModule()); + ai.install(new TimerModule()); if (config.keywordEnabled) ai.install(new KeywordModule()); + + console.log('ai started'); }); diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index 00b1b0f..e8b619c 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -24,7 +24,7 @@ export default class GuessingGameModule implements IModule { } public onMention = (msg: MessageLike) => { - if (msg.text && msg.text.includes('数当て')) { + if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) { const exist = guesses.findOne({ userId: msg.userId, isEnded: false diff --git a/src/modules/reversi/index.ts b/src/modules/reversi/index.ts index b3e7ef2..e185f61 100644 --- a/src/modules/reversi/index.ts +++ b/src/modules/reversi/index.ts @@ -40,7 +40,7 @@ export default class ReversiModule implements IModule { } public onMention = (msg: MessageLike) => { - if (msg.text && (msg.text.includes('リバーシ') || msg.text.toLowerCase().includes('reversi'))) { + if (msg.text && (msg.text.includes('リバーシ') || msg.text.includes('りばーし') || msg.text.includes('オセロ') || msg.text.includes('おせろ') || msg.text.toLowerCase().includes('reversi'))) { if (config.reversiEnabled) { msg.reply(serifs.REVERSI_OK); diff --git a/src/modules/timer/index.ts b/src/modules/timer/index.ts new file mode 100644 index 0000000..bf74ee2 --- /dev/null +++ b/src/modules/timer/index.ts @@ -0,0 +1,47 @@ +import 藍 from '../../ai'; +import IModule from '../../module'; +import MessageLike from '../../message-like'; +import serifs from '../../serifs'; + +export default class TimerModule implements IModule { + public name = 'timer'; + private ai: 藍; + + public install = (ai: 藍) => { + this.ai = ai; + } + + public onMention = (msg: MessageLike) => { + const seconds = (msg.text || '').match(/([0-9]+)秒/); + const minutes = (msg.text || '').match(/([0-9]+)分/); + const hours = (msg.text || '').match(/([0-9]+)時間/); + const timeStr = seconds || minutes || hours; + + if (timeStr) { + const num = parseInt(timeStr[1], 10); + + if (num <= 0) { + msg.reply(serifs.timer.invalid); + return true; + } else { + msg.reply(serifs.timer.set); + + const time = + seconds ? 1000 * num : + minutes ? 1000 * 60 * num : + hours ? 1000 * 60 * 60 * num * 1000 : + null; + + setTimeout(() => { + this.ai.sendMessage(msg.userId, { + text: serifs.timer.notify.replace('{time}', timeStr[0]) + }); + }, time); + + return true; + } + } else { + return false; + } + } +} diff --git a/src/serifs.ts b/src/serifs.ts index 0d27901..9a9f135 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -91,4 +91,10 @@ export default { * キーワード */ KEYWORD: '({word}..... {reading}..... 覚えました)', + + timer: { + set: 'わかりました!', + invalid: 'うーん...?', + notify: '{time}経ちましたよ!' + } };