How to create an attachment without using AttachmentBuilder?

i'm trying to attach an image inside of a broadcastEval. i have the image converted into a dataURI string, and i'm rebuilding it in the broadcastEval using Buffer.from. but i'm not aware of any way to create the attachment with calling new AttachmentBuilder, which doesn't work in broadcastEval.

here's my code:

  const logEvent = (
    c: Client,
    {
      embed,
      logOutputChannelID,
      canvasString,
    }: {
      embed: any;
      logOutputChannelID?: string;
      canvasString?: string;
    }
  ) => {
    console.log("logEvent ran");
    if (!logOutputChannelID) return;
    const logOutputChannel = c.channels.cache.get(
      logOutputChannelID
    ) as TextChannel;
    if (logOutputChannel) {
      if (canvasString) {
        const attachment = new AttachmentBuilder(
          Buffer.from(canvasString.split(",")[1], "base64"),
          { name: "currentDice.png" }
        );
        logOutputChannel.send({
          embeds: [
            {
              image: {
                url: "attachment://currentDice.png",
              },
            },
          ],
          files: [attachment],
        });
      } else {
        logOutputChannel
          .send({
            embeds: [embed],
          })
          .catch((err: Error) => console.error(err));
      }
    }
  };

  console.log("shard", discord?.shard);

  discord?.shard
    ?.broadcastEval(logEvent, {
      context: {
        embed: generateEmbed(),
        logOutputChannelID,
        canvasString,
      },
    })
    // .then(console.log)
    .catch(console.error);
};
Was this page helpful?