From 160ed4a191f82616a02bb20637843d28f64f4fd3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 2 Sep 2018 21:50:57 +0900 Subject: [PATCH] Refactor --- src/message-like.ts | 5 +++++ src/modules/core/index.ts | 19 +++++++++---------- src/modules/emoji/index.ts | 3 +-- src/modules/fortune/index.ts | 3 +-- src/modules/guessing-game/index.ts | 3 +-- src/modules/reversi/index.ts | 4 +--- src/utils/includes.ts | 2 +- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/message-like.ts b/src/message-like.ts index 530acda..b720029 100644 --- a/src/message-like.ts +++ b/src/message-like.ts @@ -1,6 +1,7 @@ import 藍 from './ai'; import Friend from './friend'; import { User } from './misskey/user'; +import includes from './utils/includes'; const delay = require('timeout-as-promise'); export default class MessageLike { @@ -62,4 +63,8 @@ export default class MessageLike { }); } } + + public includes = (words: string[]): boolean => { + return includes(this.text, words); + } } diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index 719780b..14a7a26 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -4,7 +4,6 @@ import MessageLike from '../../message-like'; import serifs from '../../serifs'; import Friend from '../../friend'; import getDate from '../../utils/get-date'; -import includes from '../../utils/includes'; function zeroPadding(num: number, length: number): string { return ('0000000000' + num).slice(-length); @@ -137,25 +136,25 @@ export default class CoreModule implements IModule { .sort((a, b) => a.length < b.length ? 1 : -1)[0] .substr(1); - if (includes(msg.text, ['こんにちは', 'こんにちわ'])) { + if (msg.includes(['こんにちは', 'こんにちわ'])) { msg.reply(serifs.core.hello(msg.friend.name)); incLove(); return true; } - if (includes(msg.text, ['こんばんは', 'こんばんわ'])) { + if (msg.includes(['こんばんは', 'こんばんわ'])) { msg.reply(serifs.core.helloNight(msg.friend.name)); incLove(); return true; } - if (includes(msg.text, ['おは', 'お早う'])) { + if (msg.includes(['おは', 'お早う'])) { msg.reply(serifs.core.goodMorning(tension, msg.friend.name)); incLove(); return true; } - if (includes(msg.text, ['おやすみ', 'お休み'])) { + if (msg.includes(['おやすみ', 'お休み'])) { msg.reply(serifs.core.goodNight(msg.friend.name)); incLove(); return true; @@ -165,7 +164,7 @@ export default class CoreModule implements IModule { } private nadenade = (msg: MessageLike): boolean => { - if (!includes(msg.text, ['なでなで'])) return false; + if (!msg.includes(['なでなで'])) return false; // メッセージのみ if (!msg.isMessage) return true; @@ -199,7 +198,7 @@ export default class CoreModule implements IModule { } private kawaii = (msg: MessageLike): boolean => { - if (!includes(msg.text, ['かわいい', '可愛い'])) return false; + if (!msg.includes(['かわいい', '可愛い'])) return false; msg.reply( msg.friend.love >= 5 ? serifs.core.kawaii.love : @@ -210,7 +209,7 @@ export default class CoreModule implements IModule { } private humu = (msg: MessageLike): boolean => { - if (!includes(msg.text, ['踏んで'])) return false; + if (!msg.includes(['踏んで'])) return false; // メッセージのみ if (!msg.isMessage) return true; @@ -223,7 +222,7 @@ export default class CoreModule implements IModule { } private batou = (msg: MessageLike): boolean => { - if (!includes(msg.text, ['罵倒して', '罵って'])) return false; + if (!msg.includes(['罵倒して', '罵って'])) return false; // メッセージのみ if (!msg.isMessage) return true; @@ -237,7 +236,7 @@ export default class CoreModule implements IModule { } private ponkotu = (msg: MessageLike): boolean => { - if (!includes(msg.text, ['ぽんこつ'])) return false; + if (!msg.includes(['ぽんこつ'])) return false; msg.friend.decLove(); diff --git a/src/modules/emoji/index.ts b/src/modules/emoji/index.ts index ffb207c..54da9ef 100644 --- a/src/modules/emoji/index.ts +++ b/src/modules/emoji/index.ts @@ -2,7 +2,6 @@ import 藍 from '../../ai'; import IModule from '../../module'; import MessageLike from '../../message-like'; import serifs from '../../serifs'; -import includes from '../../utils/includes'; const hands = [ '👏', @@ -125,7 +124,7 @@ export default class EmojiModule implements IModule { public install = (ai: 藍) => { } public onMention = (msg: MessageLike) => { - if (includes(msg.text, ['絵文字', 'emoji'])) { + if (msg.includes(['絵文字', 'emoji'])) { const hand = hands[Math.floor(Math.random() * hands.length)]; const face = faces[Math.floor(Math.random() * faces.length)]; const emoji = Array.isArray(hand) ? hand[0] + face + hand[1] : hand + face + hand; diff --git a/src/modules/fortune/index.ts b/src/modules/fortune/index.ts index b455f75..5a4807c 100644 --- a/src/modules/fortune/index.ts +++ b/src/modules/fortune/index.ts @@ -3,7 +3,6 @@ import IModule from '../../module'; import MessageLike from '../../message-like'; import serifs from '../../serifs'; import * as seedrandom from 'seedrandom'; -import includes from '../../utils/includes'; const omikujis = [ '大大吉', @@ -31,7 +30,7 @@ export default class FortuneModule implements IModule { public install = (ai: 藍) => { } public onMention = (msg: MessageLike) => { - if (includes(msg.text, ['占', 'うらな', '運勢', 'おみくじ'])) { + if (msg.includes(['占', 'うらな', '運勢', 'おみくじ'])) { const date = new Date(); const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`; const rng = seedrandom(seed); diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index 62d8ce1..a96125b 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -4,7 +4,6 @@ import IModule from '../../module'; import MessageLike from '../../message-like'; import serifs from '../../serifs'; import getCollection from '../../utils/get-collection'; -import includes from '../../utils/includes'; export default class GuessingGameModule implements IModule { public readonly name = 'guessingGame'; @@ -29,7 +28,7 @@ export default class GuessingGameModule implements IModule { } public onMention = (msg: MessageLike) => { - if (includes(msg.text, ['数当て', '数あて'])) { + if (msg.includes(['数当て', '数あて'])) { const exist = this.guesses.findOne({ userId: msg.userId, isEnded: false diff --git a/src/modules/reversi/index.ts b/src/modules/reversi/index.ts index f96511a..abcd85f 100644 --- a/src/modules/reversi/index.ts +++ b/src/modules/reversi/index.ts @@ -8,8 +8,6 @@ import MessageLike from '../../message-like'; import * as WebSocket from 'ws'; import Friend from '../../friend'; import getDate from '../../utils/get-date'; -import { User } from '../../misskey/user'; -import includes from '../../utils/includes'; export default class ReversiModule implements IModule { public readonly name = 'reversi'; @@ -46,7 +44,7 @@ export default class ReversiModule implements IModule { } public onMention = (msg: MessageLike) => { - if (includes(msg.text, ['リバーシ', 'オセロ', 'reversi', 'othello'])) { + if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) { if (config.reversiEnabled) { msg.reply(serifs.reversi.ok); diff --git a/src/utils/includes.ts b/src/utils/includes.ts index e92d81f..3f66967 100644 --- a/src/utils/includes.ts +++ b/src/utils/includes.ts @@ -1,4 +1,4 @@ -import { hiraganaToKatagana, zenkakuToHankaku, hankakuToZenkaku } from './japanese'; +import { hiraganaToKatagana, hankakuToZenkaku } from './japanese'; export default function(text: string, words: string[]): boolean { if (text == null) return false;