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, just like the server gave.

bedrock: bool = False

Is server Bedrock Edition? Some details may change in work of this class.

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 formattings/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.

This is just a shortcut to PlainTransformer.

to_minecraft() str[source]

Get Minecraft variant from a MOTD.

This is just a shortcut to MinecraftTransformer.

Note

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

to_html() str[source]

Get HTML from a MOTD.

This is just a shortcut to HtmlTransformer.

to_ansi() str[source]

Get ANSI variant from a MOTD.

This is just a shortcut to AnsiTransformer.

Note

We support only ANSI 24 bit colors, please implement your own transformer if you need other standards.

Components

Those are used in parsed field.

class Formatting(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[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(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[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'
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.

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

Transformers

These are basic transformers, that you can use to show a MOTD in different places (like browser or even terminal).

class HtmlTransformer(*, bedrock: bool = False)[source]

Formatter for HTML variant of a MOTD.

Warning

You should implement obfuscated CSS class yourself (name - obfuscated). See this answer as example.

transform(motd_components: Sequence[Formatting | MinecraftColor | WebColor | TranslationTag | str]) str[source]
_format_output(results: list[str]) str[source]
_handle_minecraft_color(element: MinecraftColor, /) str[source]
_handle_web_color(element: WebColor, /) str[source]
_handle_formatting(element: Formatting, /) str[source]
class AnsiTransformer[source]
ansi_color(color: tuple[int, int, int] | MinecraftColor) str[source]

Transform RGB color to ANSI color code.

_format_output(results: list[str]) str[source]
_handle_minecraft_color(element: MinecraftColor, /) str[source]
_handle_web_color(element: WebColor, /) str[source]
_handle_formatting(element: Formatting, /) str[source]
class BaseTransformer[source]

Base motd transformer class.

Motd transformer is responsible for providing a way to generate an alternative representation of motd, such as one that is able to be printed in the terminal.

transform(motd_components: Sequence[Formatting | MinecraftColor | WebColor | TranslationTag | str]) _END_RESULT_TYPE[source]
abstract _format_output(results: list[_HOOK_RETURN_TYPE]) _END_RESULT_TYPE[source]
_handle_component(component: Formatting | MinecraftColor | WebColor | TranslationTag | str) tuple[_HOOK_RETURN_TYPE, _HOOK_RETURN_TYPE] | tuple[_HOOK_RETURN_TYPE][source]
abstract _handle_str(element: str, /) _HOOK_RETURN_TYPE[source]
abstract _handle_translation_tag(_: TranslationTag, /) _HOOK_RETURN_TYPE[source]
abstract _handle_web_color(element: WebColor, /) _HOOK_RETURN_TYPE[source]
abstract _handle_formatting(element: Formatting, /) _HOOK_RETURN_TYPE[source]
abstract _handle_minecraft_color(element: MinecraftColor, /) _HOOK_RETURN_TYPE[source]
class NothingTransformer[source]

Transformer that transforms all elements into empty strings.

This transformer acts as a base for other transformers with string result type.

_format_output(results: list[str]) str[source]
_handle_str(element: str, /) str[source]
_handle_minecraft_color(element: MinecraftColor, /) str[source]
_handle_web_color(element: WebColor, /) str[source]
_handle_formatting(element: Formatting, /) str[source]
_handle_translation_tag(element: TranslationTag, /) str[source]
class PlainTransformer[source]
_handle_str(element: str, /) str[source]
class MinecraftTransformer[source]
_handle_component(component: Formatting | MinecraftColor | WebColor | TranslationTag | str) tuple[str, str] | tuple[str][source]
_handle_minecraft_color(element: MinecraftColor, /) str[source]
_handle_formatting(element: Formatting, /) str[source]