mirror of
https://github.com/syuilo/ai.git
synced 2024-11-22 05:08:00 +00:00
Refactor
This commit is contained in:
parent
8b52efea4c
commit
69bf95e6bf
|
@ -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(),
|
||||
];
|
||||
|
||||
|
|
56
src/modules/birthday/index.ts
Normal file
56
src/modules/birthday/index.ts
Normal file
|
@ -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
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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}🎉` : 'お誕生日おめでとうございます🎉',
|
||||
},
|
||||
|
||||
/**
|
||||
* リバーシ
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue