From d495ffc0fa39d1cc52ca05117522caad1c79f996 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Mon, 9 Dec 2024 01:22:55 +0000 Subject: [PATCH] simplify join/leave alert --- cogs/console.py | 8 --- cogs/players.py | 141 ++++++------------------------------------------ 2 files changed, 17 insertions(+), 132 deletions(-) diff --git a/cogs/console.py b/cogs/console.py index 373e009..e7bfc30 100644 --- a/cogs/console.py +++ b/cogs/console.py @@ -46,14 +46,6 @@ class ConsoleCog(commands.Cog): if "CheckModsNeedUpdate: Mods need update" in line: await self.handle_mod_needs_update(line) - # elif "ConnectionManager: [fully-connected]" in line: - # cog = self.bot.get_cog("PlayersCog") - # await cog.handle_player_connected(line) - - # elif "ConnectionManager: [disconnect]" in line: - # cog = self.bot.get_cog("PlayersCog") - # await cog.handle_player_disconnected(line) - async def alert_and_wait_for_restart(self, intervals_ms: list[int], reason: str): for interval_ms in intervals_ms: seconds_remaining = interval_ms / 1000 diff --git a/cogs/players.py b/cogs/players.py index d492d8f..bfe5c7b 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -28,64 +28,6 @@ for path in LOGS_FOLDER_PATH.iterdir(): log = logging.getLogger(__name__) -# @dataclass(slots=True) -# class SteamProfileSummary: -# steam_id: int -# profile_name: str -# url: str -# avatar_url: str - - -# @dataclass(slots=True) -# class ZomboidUser: -# guid: str -# ip: str -# steam_id: str -# access: str -# username: str -# connection_type: str -# steam_profile_summary: SteamProfileSummary | None = None - -# async def fetch_steam_profile(self, steam_api_key: str): -# if not steam_api_key: -# log.warning("No steam API key, can't get profile summary.") -# return - -# async with httpx.AsyncClient() as client: -# response = await client.get(url=f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={steam_api_key}&steamids={self.steam_id}") -# response.raise_for_status() - -# all_data = response.json() -# user_data = all_data["response"]["players"][0] - -# log.debug("fetched user data for: %s", self.steam_id) - -# self.steam_profile_summary = SteamProfileSummary( -# steam_id=user_data["steamid"], -# profile_name=user_data["personaname"], -# url=user_data["profileurl"], -# avatar_url=user_data["avatarfull"] -# ) - -# log.debug("successfully parsed steam profile summary for: %s", self.steam_id) - -# @property -# def embed(self) -> Embed: -# if not self.steam_profile_summary: -# raise ValueError("You must fetch the steam_profile_summary before creating an embed.") - -# embed = Embed( -# title="Player", -# description=( -# f"{self.username} ([{self.steam_profile_summary.profile_name}]({self.steam_profile_summary.url}))\n" -# "Deaths: ???\n" -# "Playtime: ???" -# ) -# ) -# embed.set_thumbnail(url=self.steam_profile_summary.avatar_url) -# return embed - - class PlayersCog(commands.Cog): """ Handles tasks related to in-game players. @@ -139,10 +81,9 @@ class PlayersCog(commands.Cog): log.debug("successfully registered player death to %s", re_match.group("username")) - async def process_connected_player(self, line: str): + async def show_player_join_leave_alert(self, line: str, re_pattern: str, embed_title: str): """ """ - re_pattern = r'\[(?P.*?)\] (?P/*?) "(?P.*?)" fully connected \((?P.*?)\)' re_match = re.search(re_pattern, line) if not re_match: @@ -156,77 +97,29 @@ class PlayersCog(commands.Cog): channel = channel or await self.bot.fetch_channel(self.bot.in_game_channel_id) embed = await player.get_embed() - embed.title = "Player Has Connected" + embed.title = embed_title await channel.send(embed=embed) + async def process_connected_player(self, line: str): + """ + """ + await self.show_player_join_leave_alert( + line=line, + re_pattern=r'\[(?P.*?)\] (?P/*?) "(?P.*?)" fully connected \((?P.*?)\)', + embed_title="Player Has Connected" + ) async def process_disconnected_player(self, line: str): - pass + """ + """ + await self.show_player_join_leave_alert( + line=line, + re_pattern=r'\[(?P.*?)\] (?P/*?) "(?P.*?)" disconnected player \((?P.*?)\)', + embed_title="Player Has Disconnected" + ) - # async def handle_player_connected(self, line: str): - # """ - # Report when a user has joined the server into a specified Discord channel. - # Example of line: - # ConnectionManager: [fully-connected] "" connection: guid=*** ip=*** steam-id=*** access=admin username="corbz" connection-type="UDPRakNet" - # """ - # re_pattern = r"guid=(\d+)\s+ip=([\d\.]+)\s+steam-id=(\d+)\s+access=(\w*)\s+username=\"([^\"]+)\"\s+connection-type=\"([^\"]+)\"" - # re_match = re.search(re_pattern, line) - # if not re_match: - # log.warning("failed to parse player data: %s", line) - # return - - # user = ZomboidUser( - # 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) - # ) - # await user.fetch_steam_profile(self.bot.steam_api_key) - - # 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 - - # embed = user.embed - # embed.title = "Player Has Connected" - # embed.colour = Colour.brand_green() - - # await channel.send(embed=embed) - - # async def handle_player_disconnected(self, line: str): - # """ - # Report when a user has left the server into a specified Discord channel. - # Example of line: - # ConnectionManager: [disconnect] "receive-disconnect" connection: guid=*** ip=*** steam-id=*** access=admin username="corbz" connection-type="Disconnected" - # """ - # re_pattern = r"guid=(\d+)\s+ip=([\d\.]+)\s+steam-id=(\d+)\s+access=(\w*)\s+username=\"([^\"]+)\"\s+connection-type=\"([^\"]+)\"" - # re_match = re.search(re_pattern, line) - # if not re_match: - # log.warning("failed to parse player data: %s", line) - # return - - # user = ZomboidUser( - # 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) - # ) - # await user.fetch_steam_profile(self.bot.steam_api_key) - - # 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 - - # embed = user.embed - # embed.title = "Player Has Disconnected" - # embed.colour = Colour.brand_red() - - # await channel.send(embed=embed) - async def setup(bot: commands.Bot): cog = PlayersCog(bot) await bot.add_cog(cog)