Skip to content

Refactor: consolidate embed extraction and plain-text rendering in message commands #19

@DevRohit06

Description

@DevRohit06

Background

src/discli/commands/message.py has grown duplicated embed-handling logic across the list, history, search, and get subcommands. The shape of the embed dict in JSON output is also inconsistent between commands. A small refactor would tighten the module and prevent drift as more embed fields are added later.

What to fix

1. Extract duplicated embed helpers

The following block currently appears in three places (list, history, search):

```python
{
"title": e.title,
"description": e.description,
"fields": [{"name": f.name, "value": f.value, "inline": f.inline} for f in e.fields],
"footer": e.footer.text if e.footer else None,
"author": e.author.name if e.author else None,
}
```

And this ~14-line plain-text renderer is also duplicated three times:

```python
for e in m["embeds"]:
embed_parts = []
if e.get("author"):
embed_parts.append(e["author"])
if e.get("title"):
embed_parts.append(f"[{e['title']}]")
...
```

Extract both into module-level helpers:

  • `_embed_to_dict(embed) -> dict`
  • `_format_embeds_plain(embeds: list[dict]) -> str`

2. Normalize `message get` embed shape

`message get` (around line 225) still emits the old `{"title": ..., "description": ...}` shape. After the helper lands, it should use `_embed_to_dict` so JSON consumers see a uniform schema across all message read commands.

3. Skip dangling `: ` on empty-content messages

For embed-only messages, plain-text output currently renders as:

```
[2026-04-16 10:12:03] BotName: | [Title] | description
```

The empty content plus trailing colon-space looks awkward. When `content` is empty, drop the `: {content}` piece and let the embed text follow the author directly.

4. Simplify `embed_text` construction in search

The current search implementation nests `filter(None, ...)` inside a generator inside `" ".join(...)`. Once `_embed_to_dict` exists, the searchable text can be built with a much simpler helper — one pass over the embed dicts.

5. (Optional) Add a unit test for the new helpers

`tests/` has no coverage for `message.py` yet. A small fixture-based test for `_format_embeds_plain` with a few embed shapes (empty, title-only, fields-only, full) would lock in the behavior cheaply.

Out of scope

  • Additional embed fields (url, color, timestamp) — file a separate issue if needed.
  • Changes to command flags or JSON field names beyond normalizing `get`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions