Commit baf82013 authored by Grant's avatar Grant
Browse files

add basic attachment support (related #5)

parent fc9dcded
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -86,7 +86,14 @@ class Discord_ {
          type: "m.room.message",
          content: {
            msgtype: "m.text",
            body: content,
            body:
              content +
              (message.attachments.size > 0
                ? "\n" +
                  message.attachments
                    .map((attachment) => attachment.url)
                    .join("\n")
                : ""),
            "m.mentions": {
              user_ids: Matrix.scanForMatrixMentions(message.content),
            },
@@ -262,6 +269,7 @@ class Discord_ {
       * Discord Message ID
       */
      replyTo?: string;
      attachments?: { url: string }[];
    } = {}
  ) {
    const webhook = await this.getWebhookFor(guild_id, channel_id);
@@ -285,11 +293,28 @@ class Discord_ {
      );
    }

    let embeds = [];

    if (options.attachments && options.attachments?.length > 0) {
      embeds.push({
        image: {
          url: options.attachments[0].url,
        },
        footer:
          options.attachments.length - 1 > 0
            ? {
                text: "+" + (options.attachments.length - 1) + " others",
              }
            : undefined,
      });
    }

    return webhook.send({
      username: displayName,
      avatarURL: Matrix.getMediaURL(user.avatar_url),
      content,
      components,
      embeds,
      allowedMentions: {
        parse: ["users"],
        roles: [],
+24 −0
Original line number Diff line number Diff line
@@ -207,6 +207,30 @@ class MatrixHandler_ {
        });
        break;
      }
      case "m.image": {
        const discMsg = await Discord.sendMessage(
          linkedDiscord.guildId,
          linkedDiscord.channelId,
          profile,
          Discord.matrixToDiscord(message),
          {
            replyTo: inReplyTo || undefined,
            attachments: [
              { url: Matrix.getMediaURL(message.content.url as any) as string },
            ],
          }
        );

        await prisma.matrixMessages.create({
          data: {
            matrix_event_id: message.event_id,
            discord_id: discMsg.id,
            discord_channel_id: discMsg.channelId,
            discord_guild_id: discMsg.guildId!,
          },
        });
        break;
      }
      default:
        console.warn("Unhandled new room message type", message);
    }