diff --git a/src/bot.ts b/src/bot.ts index cc46d3c5c2a07300c8d2cd92ec9ab8e5d5d1244d..8e0ccfb37c19df2f523d68e0bccf0222b83159f8 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1056,6 +1056,91 @@ export class DiscordBot { } try { const intent = this.GetIntentFromDiscordMember(msg.author, msg.webhookID); + if (!msg.content && msg.embeds.length === 0 && msg.attachments.array().length === 0) { + return; + } + const result = await this.discordMsgProcessor.FormatMessage(msg); + if (!result.body && msg.attachments.array().length === 0) { + return; + } + if(result.body){ + await Util.AsyncForEach(rooms, async (room) => { + const sendContent: IMatrixMessage = { + body: result.body, + format: "org.matrix.custom.html", + formatted_body: result.formattedBody, + msgtype: result.msgtype, + }; + if (msg.reference) { + const storeEvent = await this.store.Get(DbEvent, { discord_id: msg.reference?.messageID }); + if (storeEvent && storeEvent.Result) { + let replyToEventId: string | undefined = undefined; + while (storeEvent.Next()) { + const [eventId] = storeEvent.MatrixId.split(";"); + // Try to get the "deepest" event ID if this event replaces another ID + // We need to do this since a m.in_reply_to relation requires the original event ID and not the replacement one + const { chunk } = await intent.underlyingClient.unstableApis.getRelationsForEvent(room, eventId, "m.replace"); + if (!!chunk?.length) { + replyToEventId = chunk[0].content['m.relates_to'].event_id; + } else { + replyToEventId ??= eventId; + } + } + if (replyToEventId) { + sendContent["m.relates_to"] = { + "m.in_reply_to": { + event_id: replyToEventId + } + }; + } + } + } + if (editEventId) { + sendContent.body = `* ${result.body}`; + sendContent.formatted_body = `* ${result.formattedBody}`; + sendContent["m.new_content"] = { + body: result.body, + format: "org.matrix.custom.html", + formatted_body: result.formattedBody, + msgtype: result.msgtype, + }; + sendContent["m.relates_to"] = { + event_id: editEventId, + rel_type: "m.replace", + }; + } + const trySend = async () => intent.sendEvent(room, sendContent); + const afterSend = async (eventId) => { + this.lastEventIds[room] = eventId; + const evt = new DbEvent(); + evt.MatrixId = `${eventId};${room}`; + evt.DiscordId = msg.id; + evt.ChannelId = msg.channel.id; + if (msg.guild) { + evt.GuildId = msg.guild.id; + } + await this.store.Insert(evt); + this.userActivity.updateUserActivity(intent.userId); + }; + let res; + try { + res = await trySend(); + await afterSend(res); + } catch (e) { + if (e.errcode !== "M_FORBIDDEN" && e.errcode !== "M_GUEST_ACCESS_FORBIDDEN") { + log.error("Failed to send message into room.", e); + return; + } + if (msg.member && !msg.webhookID) { + await this.userSync.JoinRoom(msg.member, room); + } else { + await this.userSync.JoinRoom(msg.author, room, Boolean(msg.webhookID)); + } + res = await trySend(); + await afterSend(res); + } + }); + } // Check Attachements if (!editEventId) { // on discord you can't edit in images, you can only edit text @@ -1108,89 +1193,6 @@ export class DiscordBot { }); }); } - if (!msg.content && msg.embeds.length === 0) { - return; - } - const result = await this.discordMsgProcessor.FormatMessage(msg); - if (!result.body) { - return; - } - await Util.AsyncForEach(rooms, async (room) => { - const sendContent: IMatrixMessage = { - body: result.body, - format: "org.matrix.custom.html", - formatted_body: result.formattedBody, - msgtype: result.msgtype, - }; - if (msg.reference) { - const storeEvent = await this.store.Get(DbEvent, { discord_id: msg.reference?.messageID }); - if (storeEvent && storeEvent.Result) { - let replyToEventId: string | undefined = undefined; - while (storeEvent.Next()) { - const [eventId] = storeEvent.MatrixId.split(";"); - // Try to get the "deepest" event ID if this event replaces another ID - // We need to do this since a m.in_reply_to relation requires the original event ID and not the replacement one - const { chunk } = await intent.underlyingClient.unstableApis.getRelationsForEvent(room, eventId, "m.replace"); - if (!!chunk?.length) { - replyToEventId = chunk[0].content['m.relates_to'].event_id; - } else { - replyToEventId ??= eventId; - } - } - if (replyToEventId) { - sendContent["m.relates_to"] = { - "m.in_reply_to": { - event_id: replyToEventId - } - }; - } - } - } - if (editEventId) { - sendContent.body = `* ${result.body}`; - sendContent.formatted_body = `* ${result.formattedBody}`; - sendContent["m.new_content"] = { - body: result.body, - format: "org.matrix.custom.html", - formatted_body: result.formattedBody, - msgtype: result.msgtype, - }; - sendContent["m.relates_to"] = { - event_id: editEventId, - rel_type: "m.replace", - }; - } - const trySend = async () => intent.sendEvent(room, sendContent); - const afterSend = async (eventId) => { - this.lastEventIds[room] = eventId; - const evt = new DbEvent(); - evt.MatrixId = `${eventId};${room}`; - evt.DiscordId = msg.id; - evt.ChannelId = msg.channel.id; - if (msg.guild) { - evt.GuildId = msg.guild.id; - } - await this.store.Insert(evt); - this.userActivity.updateUserActivity(intent.userId); - }; - let res; - try { - res = await trySend(); - await afterSend(res); - } catch (e) { - if (e.errcode !== "M_FORBIDDEN" && e.errcode !== "M_GUEST_ACCESS_FORBIDDEN") { - log.error("Failed to send message into room.", e); - return; - } - if (msg.member && !msg.webhookID) { - await this.userSync.JoinRoom(msg.member, room); - } else { - await this.userSync.JoinRoom(msg.author, room, Boolean(msg.webhookID)); - } - res = await trySend(); - await afterSend(res); - } - }); MetricPeg.get.requestOutcome(msg.id, true, "success"); } catch (err) { MetricPeg.get.requestOutcome(msg.id, true, "fail"); @@ -1208,6 +1210,9 @@ export class DiscordBot { if (storeEvent && storeEvent.Result) { while (storeEvent.Next()) { const matrixIds = storeEvent.MatrixId.split(";"); + if(!oldMsg.content && oldMsg.attachments.array().length>0){ + newMsg.content = newMsg.content + " " + oldMsg.attachments.array()[0].url; + } await this.OnMessage(newMsg, matrixIds[0]); return; }