platforms.discord_rich_content module

Serialize Discord rich message content into LLM-visible text.

platforms.discord_rich_content.serialize_embed(embed)[source]

Serialize a single Discord embed into XML-ish text for the LLM.

Renders an embed (author, title, url, description, fields, image, thumbnail, footer, video, timestamp, color, provider) into an indented <embed>...</embed> block so the model can read structured embed content that would otherwise be invisible in the plain message body. Any embed keys not covered by a dedicated tag are dumped into a <raw_json> fallback rather than dropped, and an embed with no renderable content yields an empty string.

It seeds a raw dict via _dict_from_embed and then delegates to the module’s _author_values, _footer_values, _append_tag, _append_url_tag, _append_fields, _append_provider and _append_raw_fallback helpers; it performs no I/O and no mutation of embed. Called by serialize_rich_content (for both top-level and forwarded-snapshot embeds) in this module, and exercised directly by tests/test_discord_rich_content.py; no other callers.

Parameters:

embed (Any) – A Discord embed, ideally a discord.py / selfcord Embed exposing both attributes and to_dict().

Returns:

The serialized <embed> block, or "" when the embed has no renderable content.

Return type:

str

platforms.discord_rich_content.serialize_rich_content(message)[source]

Convert a message’s embeds, snapshots, stickers and polls to text.

Walks every non-textual artifact attached to a Discord message and renders each into LLM-readable text so the model perceives the same rich content a human sees: embeds become <embed> blocks, forwarded message_snapshots become <forwarded_message> blocks (with their own nested content, embeds and attachments), stickers become [Sticker: name] lines, and a poll becomes a <poll> block listing its options. Parts are joined with newlines; a message with no rich content yields an empty string.

Reads only attributes off message (embeds, message_snapshots, stickers, poll) and delegates per-embed rendering to serialize_embed; it performs no I/O. Called by merge_content_with_rich_content in this module, by the Discord bot and selfbot adapters (platforms/discord.py and platforms/discord_self.py import it as _serialize_rich_content to diff edited messages), and by tests/test_discord_rich_content.py.

Parameters:

message (Any) – A Discord message object exposing any of embeds, message_snapshots, stickers and poll.

Returns:

The newline-joined serialized rich content, or "" when the message carries none.

Return type:

str

platforms.discord_rich_content.merge_content_with_rich_content(content, message)[source]

Combine a message’s plain text with its serialized rich content.

Produces the single text blob the rest of the pipeline stores and feeds to the LLM by appending the embeds/snapshots/stickers/polls rendering to the message’s plain content (separated by a newline), so structured artifacts are not lost when a message is reduced to text. When there is no rich content the plain text is returned unchanged, and when there is no plain text only the rich content is returned.

Stringifies content via _as_text and obtains the rich portion from serialize_rich_content; no I/O. Called wherever a Discord message is flattened to history/prompt text: the bot adapter (platforms/discord.py) and selfbot adapter (this package’s platforms/discord_self.py) on-message and history paths, the knowledge-graph builder build_kg.py, and the tools/discord_embed.py tool; also covered by tests/test_discord_rich_content.py.

Parameters:
  • content (Any) – The message’s plain text content (any value; coerced to string, with None treated as empty).

  • message (Any) – The Discord message whose rich content should be appended.

Returns:

The merged text, the plain text alone, or the rich content alone depending on which parts are present.

Return type:

str