From bde4086bac3ee7757907214d23a60852732d4f4a Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 19 Sep 2018 08:52:06 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=BD=E3=82=93=E3=81=93=E3=81=A4=E5=91=BC?= =?UTF-8?q?=E3=81=B0=E3=82=8F=E3=82=8A=E3=81=95=E3=82=8C=E3=81=9F=E3=81=A8?= =?UTF-8?q?=E3=81=8D=E3=81=AF=E3=83=AA=E3=82=A2=E3=82=AF=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=82=92=E5=A4=89=E3=81=88=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ai.ts | 49 +++++++++++++++++++++++++-------------- src/module.ts | 8 +++++-- src/modules/core/index.ts | 8 ++++--- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/ai.ts b/src/ai.ts index 9869fe3..a2161ec 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -152,6 +152,36 @@ export default class 藍 { private onMention = (msg: MessageLike) => { console.log(`mention received: ${msg.id}`); + const context = !msg.isMessage && msg.replyId == null ? null : this.contexts.findOne(msg.isMessage ? { + isMessage: true, + userId: msg.userId + } : { + isMessage: false, + noteId: msg.replyId + }); + + let reaction = 'love'; + + if (context != null) { + const module = this.modules.find(m => m.name == context.module); + const res = module.onReplyThisModule(msg, context.data); + + if (res != null && typeof res === 'object') { + reaction = res.reaction; + } + } else { + let res: ReturnType; + + this.modules.filter(m => m.hasOwnProperty('onMention')).some(m => { + res = m.onMention(msg); + return res === true || typeof res === 'object'; + }); + + if (res != null && typeof res === 'object') { + reaction = res.reaction; + } + } + setTimeout(() => { if (msg.isMessage) { // 既読にする @@ -162,27 +192,10 @@ export default class 藍 { // リアクションする this.api('notes/reactions/create', { noteId: msg.id, - reaction: 'love' + reaction: reaction }); } }, 1000); - - const context = !msg.isMessage && msg.replyId == null ? null : this.contexts.findOne(msg.isMessage ? { - isMessage: true, - userId: msg.userId - } : { - isMessage: false, - noteId: msg.replyId - }); - - if (context != null) { - const module = this.modules.find(m => m.name == context.module); - module.onReplyThisModule(msg, context.data); - } else { - this.modules.filter(m => m.hasOwnProperty('onMention')).some(m => { - return m.onMention(msg); - }); - } } public post = async (param: any) => { diff --git a/src/module.ts b/src/module.ts index 672214c..0f1ac16 100644 --- a/src/module.ts +++ b/src/module.ts @@ -4,7 +4,11 @@ import MessageLike from './message-like'; export default interface IModule { name: string; install?: (ai: 藍) => void; - onMention?: (msg: MessageLike) => boolean; + onMention?: (msg: MessageLike) => boolean | Result; onLocalNote?: (note: any) => void; - onReplyThisModule?: (msg: MessageLike, data?: any) => void; + onReplyThisModule?: (msg: MessageLike, data?: any) => void | Result; } + +export type Result = { + reaction: string; +}; diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index d2fa37a..1d0f806 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -1,5 +1,5 @@ import 藍 from '../../ai'; -import IModule from '../../module'; +import IModule, { Result } from '../../module'; import MessageLike from '../../message-like'; import serifs from '../../serifs'; import Friend from '../../friend'; @@ -304,12 +304,14 @@ export default class CoreModule implements IModule { return true; } - private ponkotu = (msg: MessageLike): boolean => { + private ponkotu = (msg: MessageLike): boolean |Result => { if (!msg.includes(['ぽんこつ'])) return false; msg.friend.decLove(); - return true; + return { + reaction: 'angry' + }; } public onReplyThisModule = (msg: MessageLike, data: any) => {