Tagscript

EmbedParser

Build a Discord embed from a template.

embed describes an embed. It renders nothing into the message body. Instead it collects the embed onto response.actions.embed for the bot to send.

For template authors

Syntax

Either paste a whole embed as JSON:

{embed:json}

or set one property per tag:

{embed(property):value}

Examples

The JSON form, useful when you have built the embed somewhere else:

{embed:{"title":"Hello!","description":"A test embed.","color":"Red"}}

The property form, easier to read and to edit one line at a time:

{embed(color):0x37b2cb}
{embed(title):Rules}
{embed(description):Follow these to keep the server pleasant.}
{embed(field):Rule 1|Be nice.|false}
{embed(image):https://random-d.uk/api/randomimg}
{embed(footer):Posted by the mods|https://example.com/icon.png}

Repeated tags merge, so the six above build one embed.

Four properties take more than a plain value:

PropertyWritten asBecomes
fieldname|value|inlineAnother entry in fields.
image, thumbnaila URL{ "url": "..." }.
authorname|url|iconUrl, the last two optional{ "name": ..., "url": ..., "icon_url": ... }.
footertext|iconUrl, the icon optional{ "text": ..., "icon_url": ... }.

Colours accept 0x37b2cb, #ed4245, ed4245, a plain number, Random, or a Discord colour name such as Red.

For developers

Register the parser

import { EmbedParser } from '@tagscript/plugin-discord';
import { Interpreter } from 'tagscript';

const ts = new Interpreter(new EmbedParser());

Behaviour

The payload is required. parse merges into ctx.response.actions.embed and returns an empty string, so nothing reaches the message body.

Colours go through the exported resolveColor, which returns the input unchanged instead of throwing when it cannot resolve, so a bad colour reaches you as a string rather than a number.

response.actions.embed is an APIEmbed: the shape Discord's API takes, the shape EmbedBuilder.toJSON() produces, and the shape EmbedBuilder.from() reads. Nothing needs reshaping on the way out.

const response = await ts.run(template);

if (response.actions.embed) {
	await interaction.reply({ embeds: [EmbedBuilder.from(response.actions.embed)] });
}

The value is typed APIEmbed, but it is assembled from text a user wrote, so the type is a convenience and not a guarantee. A template can set any property name to any string. Validate it before handing it to EmbedBuilder, which throws on malformed input, and check field counts and lengths against Discord's limits.

The JSON form is parsed by the protected parseEmbedJSON method. Subclass EmbedParser and override it to validate, to strip properties you do not allow, or to resolve image URLs yourself.

API reference

EmbedParser

Last updated on

On this page

Edit on Github