Skip to content

CreateAttachment

CreateAttachment wraps raw binary data into a named attachment object for use in message sends.

import { CreateAttachment } from "lunibee";
new CreateAttachment(data: Uint8Array | ArrayBuffer | Buffer | Blob | string, options?: { name?: string; description?: string; root?: string } | string)
Parameter Type Description
data Uint8Array | ArrayBuffer | Buffer | Blob | string Raw file data, or a file path read when the message is sent.
options object | string Options, or just the filename as a string.
options.name string Filename including extension (e.g. "image.png"). Defaults to the path’s basename, or "file.bin" for binary data.
options.description string Alt text description for the file.
options.root string For a path: the folder it must stay inside (symlinks resolved). Set it whenever the path comes from a user, e.g. a command option; otherwise any readable file could be uploaded.
// A path chosen by a user can only reach files under ./uploads.
new CreateAttachment(userPath, { root: "./uploads" });

Sets or changes the filename.

attachment.setName("welcome-banner.png");

Sets the alt text for the file.

attachment.setDescription("A welcome banner for new members.");

Replaces the file contents (binary data or a path).

Resolves the contents to a Promise<Uint8Array>, reading a path from disk (and enforcing root when set). Throws when a rooted path resolves outside its root.

The name, description, file and root fields are public properties.

Send a generated image

import { CreateAttachment } from "lunibee";
import { readFile } from "node:fs/promises";
const imageData = await readFile("./assets/welcome.png");
const attachment = new CreateAttachment(imageData, {
name: "welcome.png",
description: "Welcome banner",
});
await channel.send({ files: [attachment] });

Send with an embed referencing the attachment

const attachment = new CreateAttachment(imageData, { name: "chart.png" });
const embed = new CreateEmbed()
.setTitle("Monthly Stats")
.setImage("attachment://chart.png"); // reference the attachment by name
await channel.send({ embeds: [embed], files: [attachment] });