Skip to content

MessageContentBuilder - easily create message content with formatting #11057

@DanieliusDev

Description

@DanieliusDev

Which application or package is this feature request for?

builders

Feature

When creating message content, code can look very messy and possibly unclear. For example:

// this is a VERY simple example, code may be far larger than this with complex logic
interaction.reply(`${heading("Here is a list:")}\n${unorderedList(["something 1", "something 2"])}`);

Note: technically you could make a content variable and use += to make it easier to read (useful for conditionally adding content) but it can still be kind of tedious.

My proposal is MessageContentBuilder which guarantees correctly formatted message content - no more forgetting to add \n and so on.

This is what the above example could look like using this builder:

const content = new MessageContentBuilder()
  .addHeading("Here is a list:")
  .addUnorderedList(
    "Something 1",
    "Something 2"
  );

// Maybe later on discord.js can directly take MessageContentBuilder same way it can take EmbedBuilder
interaction.reply(content.toString());

This a very basic example so it doesn't show its full potential, you may understand the potential in the api example below.

Ideal solution or implementation

I already have my own implementation in my discord bot codebase, and it works like this:

  • MessageContentBuilder is the main builder. It handles things like adding headings (addHeading), adding new lines (addLine) and adding lists (addUnorderedList, addOrderedList).
  • MessageContentLineBuilder is a builder for each line of message content (used in MessageContentBuilder.addLine). It has methods like addText, addBoldText, addUnderlinedText, addSpoilerText and more.

Here is a simple api preview with some of the possible message content formatting.

type MessageContentLineBuilderFunction = (builder: MessageContentLineBuilder) => void;

interface MessageContentBuilder {
  #content: string;

  constructor(): MessageContentBuilder;

  addLine(line?: string | MessageContentLineBuilderFunction |  MessageContentLineBuilder): MessageContentBuilder;

  addHeading(text: string | MessageContentLineBuilderFunction |  MessageContentLineBuilder, level?: HeadingLevel): MessageContentBuilder;

  addUnorderedList(...items: (string | MessageContentLineBuilderFunction |  MessageContentLineBuilder)[]): MessageContentBuilder;

  addOrderedList(...items: (string | MessageContentLineBuilderFunction |  MessageContentLineBuilder)[]): MessageContentBuilder;

  toString(): string;
}

interface MessageContentLineBuilder {
  #line: string;

  constructor(): MessageContentLineBuilder;

  addText(text: string): MessageContentLineBuilder;

  addBoldText(text: string): MessageContentLineBuilder;

  addUnderlinedText(text: string): MessageContentLineBuilder;

  addSpoilerText(text: string): MessageContentLineBuilder;

  toString(): string;
}

Alternative solutions or implementations

No response

Other context

I would be happy to open up a pr and implement this, im just looking for feedback for this api.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions