fix: safer parse_chat

This commit is contained in:
əlemi 2022-06-18 16:17:27 +02:00
parent 778fbe76f3
commit c359e2221a
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -39,13 +39,15 @@ def _parse_formatted_block(msg:dict) -> str:
def parse_chat(msg:Union[dict,str], ansi_color:bool=False) -> str: def parse_chat(msg:Union[dict,str], ansi_color:bool=False) -> str:
"""Recursive function to parse minecraft chat json, with optional colors""" """Recursive function to parse minecraft chat json, with optional colors"""
if isinstance(msg, str): if isinstance(msg, dict):
data = msg
elif isinstance(msg, str):
try: try:
data = json.loads(msg) data = json.loads(msg)
except ValueError: except ValueError:
return str(msg) # It's not json, it's already plaintext return str(msg) # It's not json, it's already plaintext
else: else:
data = msg return str(msg)
out = "" out = ""
if "text" in data or "translate" in data: if "text" in data or "translate" in data:
if ansi_color: if ansi_color: