From e88bffa93cfc80de53ff7358252a083f6e331544 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 23 Jan 2019 21:49:10 +0900 Subject: [PATCH] Make async --- src/ai.ts | 14 +++++++------- src/modules/core/index.ts | 4 ++-- src/modules/dice/index.ts | 2 +- src/modules/emoji/index.ts | 2 +- src/modules/follow/index.ts | 2 +- src/modules/fortune/index.ts | 2 +- src/modules/guessing-game/index.ts | 4 ++-- src/modules/ping/index.ts | 2 +- src/modules/reversi/index.ts | 2 +- src/modules/timer/index.ts | 2 +- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/ai.ts b/src/ai.ts index cba940c..8fb599e 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -15,8 +15,8 @@ import getCollection from './utils/get-collection'; import Stream from './stream'; import log from './utils/log'; -type MentionHook = (msg: Message) => boolean | HandlerResult; -type ContextHook = (msg: Message, data?: any) => void | HandlerResult; +type MentionHook = (msg: Message) => Promise; +type ContextHook = (msg: Message, data?: any) => Promise; export type HandlerResult = { reaction: string; @@ -165,7 +165,7 @@ export default class 藍 { // なければそれぞれのモジュールについてフックが引っかかるまで呼び出し if (context != null) { const handler = this.contextHooks[context.module]; - const res = handler(msg, context.data); + const res = await handler(msg, context.data); if (res != null && typeof res === 'object') { reaction = res.reaction; @@ -173,10 +173,10 @@ export default class 藍 { } else { let res: boolean | HandlerResult; - this.mentionHooks.some(handler => { - res = handler(msg); - return res === true || typeof res === 'object'; - }); + for (const handler of this.mentionHooks) { + res = await handler(msg); + if (res === true || typeof res === 'object') break; + } if (res != null && typeof res === 'object') { reaction = res.reaction; diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index e6dc72d..cb1108d 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -21,7 +21,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (!msg.text) return false; return ( @@ -313,7 +313,7 @@ export default class extends Module { } @autobind - private contextHook(msg: Message, data: any) { + private async contextHook(msg: Message, data: any) { if (msg.text == null) return; const done = () => { diff --git a/src/modules/dice/index.ts b/src/modules/dice/index.ts index 030ff34..e46e42b 100644 --- a/src/modules/dice/index.ts +++ b/src/modules/dice/index.ts @@ -14,7 +14,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (msg.text == null) return false; const query = msg.text.match(/([0-9]+)[dD]([0-9]+)/); diff --git a/src/modules/emoji/index.ts b/src/modules/emoji/index.ts index edf0785..b56be5c 100644 --- a/src/modules/emoji/index.ts +++ b/src/modules/emoji/index.ts @@ -137,7 +137,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (msg.includes(['顔文字', '絵文字', 'emoji', '福笑い'])) { const hand = hands[Math.floor(Math.random() * hands.length)]; const face = faces[Math.floor(Math.random() * faces.length)]; diff --git a/src/modules/follow/index.ts b/src/modules/follow/index.ts index d7e33a3..b6a1b26 100644 --- a/src/modules/follow/index.ts +++ b/src/modules/follow/index.ts @@ -13,7 +13,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (msg.text && msg.includes(['フォロー', 'フォロバ', 'follow me'])) { if (!msg.user.isFollowing) { this.ai.api('following/create', { diff --git a/src/modules/fortune/index.ts b/src/modules/fortune/index.ts index f3e54f5..dac4c37 100644 --- a/src/modules/fortune/index.ts +++ b/src/modules/fortune/index.ts @@ -16,7 +16,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (msg.includes(['占', 'うらな', '運勢', 'おみくじ'])) { const date = new Date(); const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDate()}@${msg.userId}`; diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index 0c232fc..bc9e089 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -32,7 +32,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (!msg.includes(['数当て', '数あて'])) return false; const exist = this.guesses.findOne({ @@ -69,7 +69,7 @@ export default class extends Module { } @autobind - private contextHook(msg: Message) { + private async contextHook(msg: Message) { if (msg.text == null) return; const exist = this.guesses.findOne({ diff --git a/src/modules/ping/index.ts b/src/modules/ping/index.ts index f09f08f..f8672c6 100644 --- a/src/modules/ping/index.ts +++ b/src/modules/ping/index.ts @@ -13,7 +13,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (msg.text && msg.text.includes('ping')) { msg.reply('PONG!'); return true; diff --git a/src/modules/reversi/index.ts b/src/modules/reversi/index.ts index 12b4544..5b0d62a 100644 --- a/src/modules/reversi/index.ts +++ b/src/modules/reversi/index.ts @@ -33,7 +33,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) { if (config.reversiEnabled) { msg.reply(serifs.reversi.ok); diff --git a/src/modules/timer/index.ts b/src/modules/timer/index.ts index b14d0fb..8f51985 100644 --- a/src/modules/timer/index.ts +++ b/src/modules/timer/index.ts @@ -14,7 +14,7 @@ export default class extends Module { } @autobind - private mentionHook(msg: Message) { + private async mentionHook(msg: Message) { const secondsQuery = (msg.text || '').match(/([0-9]+)秒/); const minutesQuery = (msg.text || '').match(/([0-9]+)分/); const hoursQuery = (msg.text || '').match(/([0-9]+)時間/);