Skip to main content

`/img` — HTML-to-PNG renderer

Rendered from docs/discord/img/README.md

Generated on every docs build. Edit the source in docs/, not here.

Renders styled card and meme images as PNG attachments using Satori + @resvg/resvg-js. Other features can call renderToPng directly to produce image attachments without going through a /img slash command.

Goal​

Ship a two-subcommand surface (/img meme, /img card) backed by an internal rendering API that leaderboard and quote-book features can call once those land.

Command surface​

CommandEffect
/img meme top:<text> bottom:<text> [template:<name>]Top/bottom meme caption over a solid-color background. Templates: classic (black), bonk (brown), disaster-girl (dark red).
/img card title:<text> body:<text>Announcement/quote card with a dark gradient background.

Replies with a PNG file attachment. The interaction is deferred first so render time (~100 ms) doesn't hit Discord's 3-second ACK deadline.

How it works​

Library choice: Satori + @resvg/resvg-js​

Satori converts a React-style element tree (or plain { type, props } objects) into an SVG string. It does not require a DOM, a browser, or a build step.

@resvg/resvg-js rasterizes the SVG to a PNG buffer using Rust's resvg library via a native Node.js addon.

Total footprint: ~10 MB of dependencies. Render time: ~100 ms. No Chromium download required. Ships a prebuilt native binary for linux-x64-gnu (the target platform for the bot).

Tradeoffs accepted: CSS subset only (no z-index stacking, no CSS Grid, no JS). Sufficient for cards and memes.

Plain object trees (no JSX)​

The project runs TypeScript directly via tsx with no JSX transform configured. Templates are plain { type, props } objects. See docs/architecture.md for the no-build-step rationale.

Rendering pipeline​

memeTemplate(props) → plain object tree
↓
renderToPng(tree, { width, height })
↓ satori → SVG string
↓ Resvg → PNG buffer
pngAttachment(buf, 'meme.png')
↓
interaction.editReply({ files: [attachment] })

Source layout​

ConcernLocation
PNG rendererbots/discord/src/img/render.ts
Font loadingbots/discord/src/img/fonts.ts
Bundled fontsbots/discord/src/img/fonts/
Discord attachment helperbots/discord/src/img/attach.ts
Meme templatebots/discord/src/img/templates/meme.ts
Card templatebots/discord/src/img/templates/card.ts
Shared style constantsbots/discord/src/img/templates/_styles.ts
Slash commandbots/discord/src/commands/img.ts

renderToPng API​

Other features call this to produce PNG attachments:

import { renderToPng } from '../img/render.ts';
import { pngAttachment } from '../img/attach.ts';

const buf = await renderToPng(myTemplate(props), { width: 600, height: 340 });
await interaction.editReply({ files: [pngAttachment(buf, 'output.png')] });

renderToPng accepts any plain { type, props } object tree that Satori understands and returns a Buffer containing a PNG.

Bundled fonts​

Fonts are loaded once at module init from src/img/fonts/ and reused for every render call.

FileFamilyWeightLicense
Inter-Regular.ttfInter400SIL Open Font License 1.1
Inter-Bold.ttfInter700SIL Open Font License 1.1
Anton-Regular.woffAnton400SIL Open Font License 1.1

Inter is used for body text and card headings. Anton is an Impact-style condensed display font used for meme captions.

License texts: Inter, Anton.

Cross-feature hooks​

/top and /quote both shipped as text-only commands. Neither has an image or card option, and no leaderboard or quote template exists. Adding one would be a new renderToPng call with its own template.

Design notes​

  • Defer + editReply, not reply. Satori + resvg takes ~100 ms. Deferring before rendering guarantees Discord's 3-second ACK window is met even under load.
  • Solid-color meme backgrounds. v1 bundles no background images to avoid SSRF risk from arbitrary URL fetching and licensing questions around specific meme images. Background color encodes the template identity.
  • Templates are code, not data. No user-uploadable templates. Adding a new template means adding a new .ts file and a choice entry in the slash command options.