From fd51b232ebf3aff8d99b1c6b22357f6e355163a4 Mon Sep 17 00:00:00 2001
From: deurstann <tristan.derouet@gmail.com>
Date: Thu, 17 Aug 2023 17:16:02 +0200
Subject: [PATCH] Send discord attachments after text

---
 src/bot.ts | 104 ++++++++++++++++++++++++++---------------------------
 1 file changed, 52 insertions(+), 52 deletions(-)

diff --git a/src/bot.ts b/src/bot.ts
index 0a19847..66822ed 100644
--- a/src/bot.ts
+++ b/src/bot.ts
@@ -1056,58 +1056,6 @@ export class DiscordBot {
         }
         try {
             const intent = this.GetIntentFromDiscordMember(msg.author, msg.webhookID);
-            // Check Attachements
-            if (!editEventId) {
-                // on discord you can't edit in images, you can only edit text
-                // so it is safe to only check image upload stuff if we don't have
-                // an edit
-                await Util.AsyncForEach(msg.attachments.array(), async (attachment) => {
-                    const content = await Util.DownloadFile(attachment.url);
-                    const fileMime = content.mimeType || mime.getType(attachment.name || "")
-                        || "application/octet-stream";
-                    const mxcUrl = await intent.underlyingClient.uploadContent(
-                        content.buffer,
-                        fileMime,
-                        attachment.name || "",
-                    );
-                    const type = fileMime.split("/")[0];
-                    let msgtype = {
-                        audio: "m.audio",
-                        image: "m.image",
-                        video: "m.video",
-                    }[type];
-                    if (!msgtype) {
-                        msgtype = "m.file";
-                    }
-                    const info = {
-                        mimetype: fileMime,
-                        size: attachment.size,
-                    } as IMatrixMediaInfo;
-                    if (msgtype === "m.image" || msgtype === "m.video") {
-                        info.w = attachment.width!;
-                        info.h = attachment.height!;
-                    }
-                    await Util.AsyncForEach(rooms, async (room) => {
-                        const eventId = await intent.sendEvent(room, {
-                            body: attachment.name || "file",
-                            external_url: attachment.url,
-                            info,
-                            msgtype,
-                            url: mxcUrl,
-                        });
-                        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);
-                    });
-                });
-            }
             if (!msg.content && msg.embeds.length === 0) {
                 return;
             }
@@ -1150,6 +1098,58 @@ export class DiscordBot {
                         rel_type: "m.replace",
                     };
                 }
+                // Check Attachements
+                else {
+                    // on discord you can't edit in images, you can only edit text
+                    // so it is safe to only check image upload stuff if we don't have
+                    // an edit
+                    await Util.AsyncForEach(msg.attachments.array(), async (attachment) => {
+                        const content = await Util.DownloadFile(attachment.url);
+                        const fileMime = content.mimeType || mime.getType(attachment.name || "")
+                            || "application/octet-stream";
+                        const mxcUrl = await intent.underlyingClient.uploadContent(
+                            content.buffer,
+                            fileMime,
+                            attachment.name || "",
+                        );
+                        const type = fileMime.split("/")[0];
+                        let msgtype = {
+                            audio: "m.audio",
+                            image: "m.image",
+                            video: "m.video",
+                        }[type];
+                        if (!msgtype) {
+                            msgtype = "m.file";
+                        }
+                        const info = {
+                            mimetype: fileMime,
+                            size: attachment.size,
+                        } as IMatrixMediaInfo;
+                        if (msgtype === "m.image" || msgtype === "m.video") {
+                            info.w = attachment.width!;
+                            info.h = attachment.height!;
+                        }
+                        await Util.AsyncForEach(rooms, async (room) => {
+                            const eventId = await intent.sendEvent(room, {
+                                body: attachment.name || "file",
+                                external_url: attachment.url,
+                                info,
+                                msgtype,
+                                url: mxcUrl,
+                            });
+                            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);
+                        });
+                    });
+                }
                 const trySend = async () =>  intent.sendEvent(room, sendContent);
                 const afterSend = async (eventId) => {
                     this.lastEventIds[room] = eventId;
-- 
GitLab