JonnyPtn / zomboi

A discord bot for project zomboid multiplayer servers
49 stars 32 forks source link

Unhandled exceptions with certain characters in chat log #42

Closed thefyLoX closed 1 year ago

thefyLoX commented 2 years ago

We are getting error messages from time to time that seem to stop the chat processing in its tracks. In our case it's often related to a faction with the acute accent ´ (it corresponds to 0xb4) in its name. It seems that whenever their faction name shows up (at server start or when one of their members join) it crashes:

Unhandled exception in internal background task 'update'.
Traceback (most recent call last):
  File "C:\xxx\AppData\Roaming\Python\Python310\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "C:\xxx\serverdata\Zomboid\zomboi-master\chat.py", line 35, in update
    for line in f:
  File "C:\Python310\lib\site-packages\file_read_backwards\file_read_backwards.py", line 112, in next
    return r.decode(self.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 46: invalid start byte

The exeption above corresponds to these lines in the chat log:

[17-08-22 13:31:14.642][info] Creating faction chat '80´s'.
[17-08-22 13:31:14.642][info] Faction chat '80´s' created.

It seems that the game-to-discord flow is interrupted after that (events etc. keep getting notified) and we need to manually restart zomboi.py

The nature of this exception suggests that other characters may cause similar crashes. We are running the server on Windows and the chat log appears to be codified in ANSI instead of UTF8

thefyLoX commented 2 years ago

It appears that file_read_backwards only supports ascii, latin-1 and utf-8

Adding latin-1 encoding in line 33 of chat.py seems to solve it for us (barely tested in a live server):

with FileReadBackwards(files[0], encoding="latin-1") as f:

petegilb commented 2 years ago

you can also just add 'ignore' as the third param if you want to keep the encoding as 'utf-8' then instead of throwing an error it just skips the character

thefyLoX commented 2 years ago

you can also just add 'ignore' as the third param if you want to keep the encoding as 'utf-8' then instead of throwing an error it just skips the character

We'd rather get the whole text on Discord, but I will try adding this third param just in case, while keeping latin-1 encoding.

Thank you!

thefyLoX commented 2 years ago

I haven't been able to make 'ignore' work but we haven't had problems since we switched to 'latin-1' encoding.

Our server has people writing in chat in multiple languages and this change was needed. Is there a chance to implement it into the main branch for future updates?

Thanks in advance

petegilb commented 2 years ago

Sure, I'll see if I can do that today

JonnyPtn commented 1 year ago

Believe this should be fixed by #44 which is now merged