nanka iroiro

This commit is contained in:
syuilo 2018-08-23 17:27:39 +09:00
parent ec2f6c4630
commit 851088bc79
6 changed files with 66 additions and 2 deletions

View file

@ -2,6 +2,7 @@
"name": "ai", "name": "ai",
"main": "./built/index.js", "main": "./built/index.js",
"scripts": { "scripts": {
"start": "node ./built",
"build": "tsc" "build": "tsc"
}, },
"dependencies": { "dependencies": {

View file

@ -1,5 +1,6 @@
import from './ai'; import from './ai';
import config from './config'; import config from './config';
import ReversiModule from './modules/reversi'; import ReversiModule from './modules/reversi';
import ServerModule from './modules/server'; import ServerModule from './modules/server';
import PingModule from './modules/ping'; import PingModule from './modules/ping';
@ -8,9 +9,13 @@ import FortuneModule from './modules/fortune';
import GuessingGameModule from './modules/guessing-game'; import GuessingGameModule from './modules/guessing-game';
import KeywordModule from './modules/keyword'; import KeywordModule from './modules/keyword';
import WelcomeModule from './modules/welcome'; import WelcomeModule from './modules/welcome';
import TimerModule from './modules/timer';
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
const promiseRetry = require('promise-retry'); const promiseRetry = require('promise-retry');
console.log('starting ai...');
promiseRetry(retry => { promiseRetry(retry => {
return request.post(`${config.apiUrl}/i`, { return request.post(`${config.apiUrl}/i`, {
json: { json: {
@ -18,6 +23,8 @@ promiseRetry(retry => {
} }
}).catch(retry); }).catch(retry);
}).then(account => { }).then(account => {
console.log('account fetched');
const ai = new (account); const ai = new (account);
ai.install(new PingModule()); ai.install(new PingModule());
@ -27,5 +34,8 @@ promiseRetry(retry => {
ai.install(new GuessingGameModule()); ai.install(new GuessingGameModule());
ai.install(new ServerModule()); ai.install(new ServerModule());
ai.install(new ReversiModule()); ai.install(new ReversiModule());
ai.install(new TimerModule());
if (config.keywordEnabled) ai.install(new KeywordModule()); if (config.keywordEnabled) ai.install(new KeywordModule());
console.log('ai started');
}); });

View file

@ -24,7 +24,7 @@ export default class GuessingGameModule implements IModule {
} }
public onMention = (msg: MessageLike) => { public onMention = (msg: MessageLike) => {
if (msg.text && msg.text.includes('数当て')) { if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) {
const exist = guesses.findOne({ const exist = guesses.findOne({
userId: msg.userId, userId: msg.userId,
isEnded: false isEnded: false

View file

@ -40,7 +40,7 @@ export default class ReversiModule implements IModule {
} }
public onMention = (msg: MessageLike) => { 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) { if (config.reversiEnabled) {
msg.reply(serifs.REVERSI_OK); msg.reply(serifs.REVERSI_OK);

View file

@ -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;
}
}
}

View file

@ -91,4 +91,10 @@ export default {
* *
*/ */
KEYWORD: '({word}..... {reading}..... 覚えました)', KEYWORD: '({word}..... {reading}..... 覚えました)',
timer: {
set: 'わかりました!',
invalid: 'うーん...',
notify: '{time}経ちましたよ!'
}
}; };