MOTD Parsing

We provide a really powerful system to parse servers MOTDs.

The main class

Firstly there is the main class, which you get directly from status methods.

class Motd(parsed: list[ParsedMotdComponent], raw: RawJavaResponseMotd, bedrock: bool = False)[source]

Represents parsed MOTD.

parsed: list[ParsedMotdComponent]

Parsed MOTD, which then will be transformed.

Bases on this attribute, you can easily write your own MOTD-to-something parser.

raw: RawJavaResponseMotd

MOTD in raw format, returning back the received server response unmodified.

bedrock: bool = False

Is the server Bedrock Edition?

classmethod parse(raw: RawJavaResponseMotd, *, bedrock: bool = False) Self[source]

Parse a raw MOTD to less raw MOTD (parsed attribute).

Parameters:
  • raw – Raw MOTD, directly from server.

  • bedrock – Is server Bedrock Edition? Nothing changes here, just sets attribute.

Returns:

New Motd instance.

simplify() Self[source]

Create new MOTD without unused elements.

After parsing, the MOTD may contain some unused elements, like empty strings, or formatting/colors that don’t apply to anything. This method is responsible for creating a new motd with all such elements removed, providing a much cleaner representation.

Returns:

New simplified MOTD, with any unused elements removed.

to_plain() str[source]

Get plain text from a MOTD, without any colors/formatting.

Example:

&0Hello &oWorld turns into Hello World.

to_minecraft() str[source]

Transform MOTD to the Minecraft representation.

Note

This will always use §, even if in original MOTD used &.

Example:
>>> Motd.parse("&0Hello &oWorld")
"§0Hello §oWorld"
to_html() str[source]

Transform MOTD to the HTML format.

The result is always wrapped in a <p> tag, if you need to remove it, just do result.removeprefix("<p>").removesuffix("</p>").

Note

You should implement the “obfuscated” CSS class yourself using this snippet:

const obfuscatedCharacters =
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]\"';:<>,./?";
const obfuscatedElems = document.querySelectorAll(".obfuscated");

if (obfuscatedElems !== undefined) {
  const render = () => {
    obfuscatedElems.forEach((elem) => {
      let value = "";

      for (let i = 0, l = elem.innerText.length; i < l; i++) {
        value += obfuscatedCharacters.charAt(
          Math.floor(Math.random() * obfuscatedCharacters.length),
        );
      }

      elem.innerText = value;
    });
    setTimeout(render, 50);
  };
  render();
}

Also do note that this formatting does not make sense with non-monospace fonts.

Example:

&6Hello&o from &rAnother &kWorld turns into

<!-- there are no new lines in the actual output, those are added for readability -->
<p>
 <span style='color:rgb(255, 170, 0);text-shadow:0 0 1px rgb(42, 42, 0)'>
  Hello<i> from </span></i>
  Another <span class=obfuscated>World</span>
</p>
to_ansi() str[source]

Transform MOTD to the ANSI 24-bit format.

ANSI is mostly used for printing colored text in the terminal.

“Obfuscated” formatting (&k) is shown as a blinking one.

Components

Those are used in parsed field.

type ParsedMotdComponent = Formatting | MinecraftColor | WebColor | TranslationTag | str
class Formatting(*values)[source]

Enum for Formatting codes.

See Minecraft wiki for more info.

Note

STRIKETHROUGH and UNDERLINED don’t work on Bedrock, which our parser doesn’t keep it in mind. See MCPE-41729.

BOLD = 'l'
ITALIC = 'o'
UNDERLINED = 'n'
STRIKETHROUGH = 'm'
OBFUSCATED = 'k'
RESET = 'r'
class MinecraftColor(*values)[source]

Enum for Color codes.

See Minecraft wiki for more info.

BLACK = '0'
DARK_BLUE = '1'
DARK_GREEN = '2'
DARK_AQUA = '3'
DARK_RED = '4'
DARK_PURPLE = '5'
GOLD = '6'
GRAY = '7'
DARK_GRAY = '8'
BLUE = '9'
GREEN = 'a'
AQUA = 'b'
RED = 'c'
LIGHT_PURPLE = 'd'
YELLOW = 'e'
WHITE = 'f'
MINECOIN_GOLD = 'g'
MATERIAL_QUARTZ = 'h'
MATERIAL_IRON = 'i'
MATERIAL_NETHERITE = 'j'
MATERIAL_REDSTONE = 'm'
MATERIAL_COPPER = 'n'
MATERIAL_GOLD = 'p'
MATERIAL_EMERALD = 'q'
MATERIAL_DIAMOND = 's'
MATERIAL_LAPIS = 't'
MATERIAL_AMETHYST = 'u'
MATERIAL_RESIN = 'v'
class TranslationTag(id: str)[source]

Represents a translate field in server’s answer.

This just exists, but is completely ignored by our transformers. You can find translation tags in Motd.parsed attribute.

id: str
class WebColor(hex: str, rgb: tuple[int, int, int])[source]

Raw HTML color from MOTD.

Can be found in MOTD when someone uses gradient.

Note

Actually supported in Minecraft 1.16+ only.

hex: str
rgb: tuple[int, int, int]
classmethod from_hex(hex: str) Self[source]

Construct web color using hex color string.

Raises:

ValueError – Invalid hex color string.

Returns:

New WebColor instance.

classmethod from_rgb(rgb: tuple[int, int, int]) Self[source]

Construct web color using rgb color tuple.

Raises:

ValueError – When RGB color is out of its 8-bit range.

Returns:

New WebColor instance.