diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index fee2c302fbde48d9676b082b12a166d90bd44ae7..af8e1df00789dc442c77e02b3e45df630805323b 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -324,6 +324,54 @@ export class MatrixEventProcessor { }; } + public async ReplyEventToEmbed( + event: IMatrixEvent, repliedEvent: IMatrixEvent, channel: Discord.TextChannel, getReply: boolean = true, + ): Promise<IMatrixEventProcessorResult> { + const mxClient = this.bridge.botIntent.underlyingClient; + const profile = await this.GetUserProfileForRoom(repliedEvent.room_id, repliedEvent.sender); + const params = { + mxClient, + roomId: repliedEvent.room_id, + userId: repliedEvent.sender, + } as IMatrixMessageProcessorParams; + if (profile) { + params.displayname = profile.displayname; + } + let formattedBodyRegex = new RegExp("<br \/>(.*)<\/blockquote>") + let bodyRegex = new RegExp("> <@[^\.>]*:[^\.>]*\.[^\.>]*> (.*)"); + let body: string = ""; + if (event.type !== "m.sticker") { + const replyContent = repliedEvent.content; + const baseContent = event.content; + if(replyContent && baseContent && replyContent["body"] && baseContent["body"]){ + replyContent["body"] = baseContent["body"].matchAll(bodyRegex)[0] + } + if(replyContent && baseContent && replyContent["formatted_body"] && baseContent["formatted_body"]){ + replyContent["formatted_body"] = baseContent["formatted_body"].matchAll(formattedBodyRegex)[0] + } + body = await this.matrixMsgProcessor.FormatMessage(replyContent as IMatrixMessage, channel.guild, params); + } + + const messageEmbed = new Discord.MessageEmbed(); + messageEmbed.setDescription(body); + await this.SetEmbedAuthor(messageEmbed, repliedEvent.sender, profile); + const replyEmbed = getReply ? (await this.GetEmbedForReply(repliedEvent, channel)) : undefined; + if (replyEmbed && replyEmbed.fields) { + for (let i = 0; i < replyEmbed.fields.length; i++) { + const f = replyEmbed.fields[i]; + if (f.name === "ping") { + messageEmbed.description += `\n(${f.value})`; + replyEmbed.fields.splice(i, 1); + break; + } + } + } + return { + messageEmbed, + replyEmbed, + }; + } + public async HandleAttachment( event: IMatrixEvent, mxClient: MatrixClient, @@ -391,7 +439,7 @@ export class MatrixEventProcessor { if (!sourceEvent || !sourceEvent.content || !sourceEvent.content.body) { throw Error("No content could be found"); } - const replyEmbed = (await this.EventToEmbed(sourceEvent, channel, true)).messageEmbed; + const replyEmbed = (await this.ReplyEventToEmbed(event, sourceEvent, channel, true)).messageEmbed; // if we reply to a discord member, ping them! if (this.bridge.isNamespacedUser(sourceEvent.sender)) {