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) => {