fix: safer parse_chat
This commit is contained in:
parent
778fbe76f3
commit
c359e2221a
1 changed files with 4 additions and 2 deletions
|
@ -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:
|
||||||
|
|
Loading…
Reference in a new issue