diff --git a/cogs/players.py b/cogs/players.py index e86ad6c..58c8309 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -16,7 +16,6 @@ from utils.models import Player ZOMBOID_FOLDER_PATH = Path(getenv("SPIFFO__ZOMBOID_FOLDER_PATH")) LOGS_FOLDER_PATH = ZOMBOID_FOLDER_PATH / "Logs" -USER_LOG_FILE_PATH = next(LOGS_FOLDER_PATH.glob("*_user.txt"), None) TIMESTAMP_FORMAT = getenv("SPIFFO__ZOMBOID_LOG_TIMESTAMP_FORMAT", "%d-%m-%y %H:%M:%S.%f") log = logging.getLogger(__name__) @@ -30,7 +29,7 @@ class PlayersCog(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - self.file_handler = LogFileReader(USER_LOG_FILE_PATH) + self.create_file_handler() self.listen_for_changes.start() cmd_group = app_commands.Group( @@ -61,13 +60,22 @@ class PlayersCog(commands.Cog): self.listen_for_changes.start() await inter.followup.send("Completed") + def create_file_handler(self): + user_log_file_path = next(LOGS_FOLDER_PATH.glob("*_user.txt"), None) + self.file_handler = LogFileReader(user_log_file_path) + @tasks.loop(seconds=3) async def listen_for_changes(self): """ + Listen for changes in the user.txt log file, and process them. """ log.debug("listening for changes") - for line in await self.file_handler.read(): - await self.process_log_line(line) + try: + for line in await self.file_handler.read(): + await self.process_log_line(line) + except FileNotFoundError: + log.info("User log file not found, assuming it rotated to a new file.") + self.create_file_handler() async def process_log_line(self, line: str, alert: bool = True): log.debug("processing log line")