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.
- 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
andUNDERLINED
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.
- classmethod from_hex(hex: str) Self [source]¶
Construct web color using hex color string.
- Raises:
ValueError – Invalid hex color string.
- 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.See also
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]¶
- _handle_minecraft_color(element: MinecraftColor, /) 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.
- _handle_minecraft_color(element: MinecraftColor, /) 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]¶
- _handle_component(component: Formatting | MinecraftColor | WebColor | TranslationTag | str) tuple[_HOOK_RETURN_TYPE, _HOOK_RETURN_TYPE] | tuple[_HOOK_RETURN_TYPE] [source]¶
- abstract _handle_translation_tag(_: TranslationTag, /) _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.
- _handle_minecraft_color(element: MinecraftColor, /) str [source]¶
- _handle_formatting(element: Formatting, /) str [source]¶
- _handle_translation_tag(element: TranslationTag, /) 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]¶