From 69bf95e6bff3334ad35cf254a0cd10b6f05dfaf0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 14 Jan 2019 18:17:30 +0900 Subject: [PATCH] Refactor --- src/index.ts | 2 ++ src/modules/birthday/index.ts | 56 +++++++++++++++++++++++++++++++++++ src/modules/core/index.ts | 43 --------------------------- src/serifs.ts | 6 ++-- 4 files changed, 62 insertions(+), 45 deletions(-) create mode 100644 src/modules/birthday/index.ts diff --git a/src/index.ts b/src/index.ts index 4823a0c..4d37b38 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import 藍 from './ai'; import config from './config'; import CoreModule from './modules/core'; +import BirthdayModule from './modules/birthday'; import ReversiModule from './modules/reversi'; import PingModule from './modules/ping'; import EmojiModule from './modules/emoji'; @@ -42,6 +43,7 @@ promiseRetry(retry => { new WelcomeModule(), new ServerModule(), new FollowModule(), + new BirthdayModule(), new ValentineModule(), ]; diff --git a/src/modules/birthday/index.ts b/src/modules/birthday/index.ts new file mode 100644 index 0000000..31bc921 --- /dev/null +++ b/src/modules/birthday/index.ts @@ -0,0 +1,56 @@ +import 藍 from '../../ai'; +import IModule from '../../module'; +import Friend from '../../friend'; +import serifs from '../../serifs'; + +function zeroPadding(num: number, length: number): string { + return ('0000000000' + num).slice(-length); +} + +export default class BirthdayModule implements IModule { + public readonly name = 'birthday'; + + private ai: 藍; + + public install = (ai: 藍) => { + this.ai = ai; + + this.crawleBirthday(); + setInterval(this.crawleBirthday, 1000 * 60 * 3); + } + + /** + * 誕生日のユーザーがいないかチェック(いたら祝う) + */ + private crawleBirthday = () => { + const now = new Date(); + const m = now.getMonth(); + const d = now.getDate(); + // Misskeyの誕生日は 2018-06-16 のような形式 + const today = `${zeroPadding(m + 1, 2)}-${zeroPadding(d, 2)}`; + + const birthFriends = this.ai.friends.find({ + 'user.profile.birthday': { '$regex': new RegExp('-' + today + '$') } + } as any); + + birthFriends.forEach(f => { + const friend = new Friend(this.ai, { doc: f }); + + // 親愛度が3以上必要 + if (friend.love < 3) return; + + const data = friend.getPerModulesData(this); + + if (data.lastBirthdayChecked == today) return; + + data.lastBirthdayChecked = today; + friend.setPerModulesData(this, data); + + const text = serifs.birthday.happyBirthday(friend.name); + + this.ai.sendMessage(friend.userId, { + text: text + }); + }); + } +} diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index be25ad0..5a0bd07 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -2,13 +2,8 @@ import 藍 from '../../ai'; import IModule, { Result } from '../../module'; import MessageLike from '../../message-like'; import serifs, { getSerif } from '../../serifs'; -import Friend from '../../friend'; import getDate from '../../utils/get-date'; -function zeroPadding(num: number, length: number): string { - return ('0000000000' + num).slice(-length); -} - const titles = ['さん', 'くん', '君', 'ちゃん', '様', '先生']; const invalidChars = ['@', '#', '*', ':', '(', '[', ' ', ' ']; @@ -19,9 +14,6 @@ export default class CoreModule implements IModule { public install = (ai: 藍) => { this.ai = ai; - - this.crawleBirthday(); - setInterval(this.crawleBirthday, 1000 * 60 * 3); } public onMention = (msg: MessageLike) => { @@ -42,41 +34,6 @@ export default class CoreModule implements IModule { ); } - /** - * 誕生日のユーザーがいないかチェック(いたら祝う) - */ - private crawleBirthday = () => { - const now = new Date(); - const m = now.getMonth(); - const d = now.getDate(); - // Misskeyの誕生日は 2018-06-16 のような形式 - const today = `${zeroPadding(m + 1, 2)}-${zeroPadding(d, 2)}`; - - const birthFriends = this.ai.friends.find({ - 'user.profile.birthday': { '$regex': new RegExp('-' + today + '$') } - } as any); - - birthFriends.forEach(f => { - const friend = new Friend(this.ai, { doc: f }); - - // 親愛度が3以上必要 - if (friend.love < 3) return; - - const data = friend.getPerModulesData(this); - - if (data.lastBirthdayChecked == today) return; - - data.lastBirthdayChecked = today; - friend.setPerModulesData(this, data); - - const text = serifs.core.happyBirthday(friend.name); - - this.ai.sendMessage(friend.userId, { - text: text - }); - }); - } - private setName = (msg: MessageLike): boolean => { if (!msg.text) return false; if (!msg.text.includes('って呼んで')) return false; diff --git a/src/serifs.ts b/src/serifs.ts index efe5c9b..97e3fbd 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -42,8 +42,6 @@ export default { requireMoreLove: 'もっと仲良くなったら考えてあげてもいいですよ?', - happyBirthday: name => name ? `お誕生日おめでとうございます、${name}🎉` : 'お誕生日おめでとうございます🎉', - nadenade: { normal: 'ひゃっ…! びっくりしました', @@ -113,6 +111,10 @@ export default { done: res => `${res} です!` }, + birthday: { + happyBirthday: name => name ? `お誕生日おめでとうございます、${name}🎉` : 'お誕生日おめでとうございます🎉', + }, + /** * リバーシ */