From 87b6ba55c5f402b148bd03c5470ae8815aa368e4 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 15 Jan 2019 12:01:58 +0900 Subject: [PATCH] Refactor --- src/ai.ts | 20 ++++++++++---------- src/modules/core/index.ts | 8 ++++---- src/modules/dice/index.ts | 4 ++-- src/modules/emoji/index.ts | 4 ++-- src/modules/follow/index.ts | 4 ++-- src/modules/fortune/index.ts | 4 ++-- src/modules/guessing-game/index.ts | 8 ++++---- src/modules/ping/index.ts | 4 ++-- src/modules/reversi/index.ts | 4 ++-- src/modules/timer/index.ts | 4 ++-- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/ai.ts b/src/ai.ts index 4f1d3d2..3589e67 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -12,16 +12,16 @@ import { User } from './misskey/user'; import getCollection from './utils/get-collection'; import Stream from './stream'; -type OnMentionHandler = (msg: MessageLike) => boolean | HandlerResult; -type OnContextReplyHandler = (msg: MessageLike, data?: any) => void | HandlerResult; +type MentionHook = (msg: MessageLike) => boolean | HandlerResult; +type ContextHook = (msg: MessageLike, data?: any) => void | HandlerResult; export type HandlerResult = { reaction: string; }; export type InstallerResult = { - onMention?: OnMentionHandler; - onContextReply?: OnContextReplyHandler; + mentionHook?: MentionHook; + contextHook?: ContextHook; }; /** @@ -31,8 +31,8 @@ export default class 藍 { public account: User; public connection: Stream; public modules: Module[] = []; - private onMentionHandlers: OnMentionHandler[] = []; - private onContextReplyHandlers: { [moduleName: string]: OnContextReplyHandler } = {}; + private mentionHooks: MentionHook[] = []; + private contextHooks: { [moduleName: string]: ContextHook } = {}; public db: loki; private contexts: loki.Collection<{ @@ -112,8 +112,8 @@ export default class 藍 { this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`); const res = m.install(); if (res != null) { - if (res.onMention) this.onMentionHandlers.push(res.onMention); - if (res.onContextReply) this.onContextReplyHandlers[m.name] = res.onContextReply; + if (res.mentionHook) this.mentionHooks.push(res.mentionHook); + if (res.contextHook) this.contextHooks[m.name] = res.contextHook; } }); @@ -135,7 +135,7 @@ export default class 藍 { let reaction = 'love'; if (context != null) { - const handler = this.onContextReplyHandlers[context.module]; + const handler = this.contextHooks[context.module]; const res = handler(msg, context.data); if (res != null && typeof res === 'object') { @@ -144,7 +144,7 @@ export default class 藍 { } else { let res: boolean | HandlerResult; - this.onMentionHandlers.some(handler => { + this.mentionHooks.some(handler => { res = handler(msg); return res === true || typeof res === 'object'; }); diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index feda1c6..cab6f7c 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -15,13 +15,13 @@ export default class CoreModule extends Module { @autobind public install() { return { - onMention: this.onMention, - onContextReply: this.onContextReply + mentionHook: this.mentionHook, + contextHook: this.contextHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { if (!msg.text) return false; return ( @@ -313,7 +313,7 @@ export default class CoreModule extends Module { } @autobind - private onContextReply(msg: MessageLike, data: any) { + private contextHook(msg: MessageLike, data: any) { if (msg.text == null) return; const done = () => { diff --git a/src/modules/dice/index.ts b/src/modules/dice/index.ts index d3904b0..d432567 100644 --- a/src/modules/dice/index.ts +++ b/src/modules/dice/index.ts @@ -9,12 +9,12 @@ export default class DiceModule extends Module { @autobind public install() { return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { 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 ec43924..360f854 100644 --- a/src/modules/emoji/index.ts +++ b/src/modules/emoji/index.ts @@ -132,12 +132,12 @@ export default class EmojiModule extends Module { @autobind public install() { return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { 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 b7c8b67..eea177b 100644 --- a/src/modules/follow/index.ts +++ b/src/modules/follow/index.ts @@ -8,12 +8,12 @@ export default class FollowModule extends Module { @autobind public install() { return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { 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 3d25728..e8b9037 100644 --- a/src/modules/fortune/index.ts +++ b/src/modules/fortune/index.ts @@ -11,12 +11,12 @@ export default class FortuneModule extends Module { @autobind public install() { return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { 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 bade03c..006c77f 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -25,13 +25,13 @@ export default class GuessingGameModule extends Module { //#endregion return { - onMention: this.onMention, - onContextReply: this.onContextReply + mentionHook: this.mentionHook, + contextHook: this.contextHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { if (msg.includes(['数当て', '数あて'])) { const exist = this.guesses.findOne({ userId: msg.userId, @@ -70,7 +70,7 @@ export default class GuessingGameModule extends Module { } @autobind - private onContextReply(msg: MessageLike) { + private contextHook(msg: MessageLike) { 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 f6854b3..59228f0 100644 --- a/src/modules/ping/index.ts +++ b/src/modules/ping/index.ts @@ -8,12 +8,12 @@ export default class PingModule extends Module { @autobind public install() { return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { 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 2542a95..01ca0d0 100644 --- a/src/modules/reversi/index.ts +++ b/src/modules/reversi/index.ts @@ -28,12 +28,12 @@ export default class ReversiModule extends Module { this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg)); return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { 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 c20c69f..12b89e3 100644 --- a/src/modules/timer/index.ts +++ b/src/modules/timer/index.ts @@ -9,12 +9,12 @@ export default class TimerModule extends Module { @autobind public install() { return { - onMention: this.onMention + mentionHook: this.mentionHook }; } @autobind - private onMention(msg: MessageLike) { + private mentionHook(msg: MessageLike) { const secondsQuery = (msg.text || '').match(/([0-9]+)秒/); const minutesQuery = (msg.text || '').match(/([0-9]+)分/); const hoursQuery = (msg.text || '').match(/([0-9]+)時間/);