From 5e216862a6f81220d4ccc1daefcd5a25e8908d57 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Fri, 6 Dec 2024 12:34:18 +0000 Subject: [PATCH] fix spam + add player disconnect message --- cogs/console.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/cogs/console.py b/cogs/console.py index 0f8546e..0d53661 100644 --- a/cogs/console.py +++ b/cogs/console.py @@ -34,6 +34,14 @@ class ConsoleCog(commands.Cog): raise FileNotFoundError("Server console file doesn't exist, task cancelled.") async with aiofiles.open(CONSOLE_FILE_PATH, "r", encoding="utf-8") as file: + + # If we are at 0, restarting the bot would cause rapid fire of all log lines, + # instead lets grab the latest line, to prevent spam. + if self._last_line_number == 0: + await file.seek(0, 2) + self._last_line_number = await file.tell() + return + await file.seek(self._last_line_number) lines = await file.readlines() if not lines: @@ -59,7 +67,7 @@ class ConsoleCog(commands.Cog): log.debug("mod update instruction: %s", line) async def handle_player_joined(self, line: str): - # example + # example for connect: # ConnectionManager: [fully-connected] "" connection: guid=1733487070761473 ip=192.168.1.23 steam-id=76561198202697217 access=admin username="corbz" connection-type="UDPRakNet" re_pattern = r"guid=(\d+) ip=([\d\.]+) steam-id=(\d+) access=(\w+) username=\"([^\"]+)\" connection-type=\"([^\"]+)\"" re_match = re.search(re_pattern, line) @@ -82,9 +90,28 @@ class ConsoleCog(commands.Cog): await channel.send(content=f"Player Joined: **{data['username']}**") async def handle_player_left(self, line: str): - # example + # Example for disconnect: # ConnectionManager: [disconnect] "receive-disconnect" connection: guid=1733487070761473 ip=192.168.1.23 steam-id=76561198202697217 access=admin username="corbz" connection-type="Disconnected" - pass + + re_pattern = r"guid=(\d+) ip=([\d\.]+) steam-id=(\d+) access=(\w+) username=\"([^\"]+)\" connection-type=\"([^\"]+)\"" + re_match = re.search(re_pattern, line) + if not re_match: + log.warning("failed to parse player data: %s", line) + return + + data = { + "guid": re_match.group(1), + "ip": re_match.group(2), + "steam_id": re_match.group(3), + "access": re_match.group(4), + "username": re_match.group(5), + "connection_type": re_match.group(6), + } + + channel = self.bot.get_channel(self.bot.in_game_channel_id) + channel = await self.bot.fetch_channel(self.bot.in_game_channel_id) if not channel else channel + + await channel.send(content=f"Player Left: **{data['username']}**") async def setup(bot: commands.Bot):