diff --git a/src/ai.ts b/src/ai.ts index 679500c..4042ddf 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -123,17 +123,6 @@ export default class 藍 { this.modules.push(module); } - /** - * 指定したユーザーの「呼んでもらいたい名前」を取得します - */ - public getName = (userId: string) => { - const friend = this.friends.findOne({ - userId: userId - }); - - return friend != null ? friend.name : null; - } - private onMessage = (msg: any) => { switch (msg.type) { // メンションされたとき diff --git a/src/message-like.ts b/src/message-like.ts index 4d1a6b6..9a8a7b5 100644 --- a/src/message-like.ts +++ b/src/message-like.ts @@ -26,10 +26,22 @@ export default class MessageLike { return this.messageOrNote.replyId; } + public friend: ReturnType<藍['friends']['findOne']>; + constructor(ai: 藍, messageOrNote: any, isMessage: boolean) { this.ai = ai; this.messageOrNote = messageOrNote; this.isMessage = isMessage; + + this.friend = this.ai.friends.findOne({ + userId: this.userId + }); + + if (this.friend == null) { + this.friend = this.ai.friends.insertOne({ + userId: this.userId + }); + } } public reply = async (text: string, cw?: string) => { diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index b668abd..9abdf81 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -16,16 +16,6 @@ export default class CoreModule implements IModule { if (!msg.text) return false; if (msg.text.includes('って呼んで') && !msg.text.startsWith('って呼んで')) { - let friend = this.ai.friends.findOne({ - userId: msg.userId - }); - - if (friend == null) { - friend = this.ai.friends.insertOne({ - userId: msg.userId - }); - } - const name = msg.text.match(/^(.+?)って呼んで/)[1]; if (name.length > 10) { @@ -41,13 +31,12 @@ export default class CoreModule implements IModule { name.endsWith('様'); if (withSan) { - friend.name = name; - this.ai.friends.update(friend); + msg.friend.name = name; + this.ai.friends.update(msg.friend); msg.reply(serifs.core.setNameOk.replace('{name}', name)); } else { msg.reply(serifs.core.san).then(reply => { this.ai.subscribeReply(this, msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id, { - friend: friend, name: name }); }); @@ -55,24 +44,16 @@ export default class CoreModule implements IModule { return true; } else if (msg.text.includes('おはよう')) { - const friend = this.ai.friends.findOne({ - userId: msg.userId - }); - - if (friend && friend.name) { - msg.reply(serifs.core.goodMorningWithName.replace('{name}', friend.name)); + if (msg.friend.name) { + msg.reply(serifs.core.goodMorningWithName.replace('{name}', msg.friend.name)); } else { msg.reply(serifs.core.goodMorning); } return true; } else if (msg.text.includes('おやすみ')) { - const friend = this.ai.friends.findOne({ - userId: msg.userId - }); - - if (friend && friend.name) { - msg.reply(serifs.core.goodNightWithName.replace('{name}', friend.name)); + if (msg.friend.name) { + msg.reply(serifs.core.goodNightWithName.replace('{name}', msg.friend.name)); } else { msg.reply(serifs.core.goodNight); } @@ -87,16 +68,16 @@ export default class CoreModule implements IModule { if (msg.text == null) return; const done = () => { - this.ai.friends.update(data.friend); - msg.reply(serifs.core.setNameOk.replace('{name}', data.friend.name)); + this.ai.friends.update(msg.friend); + msg.reply(serifs.core.setNameOk.replace('{name}', msg.friend.name)); this.ai.unsubscribeReply(this, msg.userId); }; if (msg.text.includes('はい')) { - data.friend.name = data.name + 'さん'; + msg.friend.name = data.name + 'さん'; done(); } else if (msg.text.includes('いいえ')) { - data.friend.name = data.name; + msg.friend.name = data.name; done(); } else { msg.reply(serifs.core.yesOrNo).then(reply => { diff --git a/src/modules/timer/index.ts b/src/modules/timer/index.ts index 50cd33b..2ebd005 100644 --- a/src/modules/timer/index.ts +++ b/src/modules/timer/index.ts @@ -35,7 +35,7 @@ export default class TimerModule implements IModule { const str = `${hours ? hoursQuery[0] : ''}${minutes ? minutesQuery[0] : ''}${seconds ? secondsQuery[0] : ''}`; setTimeout(() => { - const name = this.ai.getName(msg.userId); + const name = msg.friend.name; this.ai.sendMessage(msg.userId, { text: name ? serifs.timer.notifyWithName.replace('{time}', str).replace('{name}', name) : serifs.timer.notify.replace('{time}', str) });