CreateAttachment
CreateAttachment wraps raw binary data into a named attachment object for use in message sends.
Import
Section titled “Import”import { CreateAttachment } from "lunibee";import { CreateAttachment } from "lunibee";Constructor
Section titled “Constructor”new CreateAttachment(data: Uint8Array | ArrayBuffer | Buffer | Blob | string, options?: { name?: string; description?: string; root?: string } | string)new CreateAttachment(data, options)| 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" });Methods
Section titled “Methods”setName(name)
Section titled “setName(name)”Sets or changes the filename.
attachment.setName("welcome-banner.png");attachment.setName("welcome-banner.png");setDescription(description)
Section titled “setDescription(description)”Sets the alt text for the file.
attachment.setDescription("A welcome banner for new members.");attachment.setDescription("A welcome banner for new members.");setFile(file)
Section titled “setFile(file)”Replaces the file contents (binary data or a path).
toBuffer()
Section titled “toBuffer()”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.
Examples
Section titled “Examples”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] });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] });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] });