From 982ef15f1a5e882d451a3f8cd8dc8fba7ff1194a Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Wed, 12 Feb 2025 10:10:39 +0530 Subject: Add exception handling on no internet, Sanitize messages before sending and implement leave messages --- lib/Player.py | 1 + lib/discordSync.py | 36 +++++++++++++++++++++++------------- proxy.py | 1 + 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/Player.py b/lib/Player.py index bc94bf2..747f8cd 100644 --- a/lib/Player.py +++ b/lib/Player.py @@ -11,6 +11,7 @@ class Player: self.version = 0 self.ip = "" self.streamWriterObject = streamWriterObject + self.is_a_bot = True # We check if they are still present after LOGIN packet, then they're not a bot def set_aircraft(self, aircraft:Aircraft): self.aircraft = aircraft diff --git a/lib/discordSync.py b/lib/discordSync.py index e85a92f..fe5c37b 100644 --- a/lib/discordSync.py +++ b/lib/discordSync.py @@ -3,6 +3,7 @@ import asyncio from config import * from lib.PacketManager.packets.FSNETCMD_TEXTMESSAGE import FSNETCMD_TEXTMESSAGE as txtMsgr from logging import debug, warning +import re BOT_TOKEN = DISCORD_TOKEN @@ -16,10 +17,15 @@ HEADERS = { } # Function to send a message to a specific Discord channel -async def discord_send_message(channel_id, content): +async def discord_send_message(channel_id:int, message:str, santize_message:bool = True): + """ + Santizes the message of @everyone and @here pings + and then sends the message to given channel id + """ + if santize_message : message = re.sub(r'@(?:everyone|here)', '', message) url = f'{BASE_URL}/channels/{channel_id}/messages' payload = { - 'content': content + 'content': message } async with aiohttp.ClientSession() as session: @@ -30,22 +36,24 @@ async def discord_send_message(channel_id, content): warning(f'Failed to send message. Status Code: {response.status} | {await response.text()}') # Function to fetch messages from a specific Discord channel + async def discord_fetch_messages(channel_id, last_message_id=None): - debug("Fetching messages...") # Debugging url = f'{BASE_URL}/channels/{channel_id}/messages' params = {'limit': 1} if last_message_id: params['after'] = last_message_id - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=HEADERS, params=params) as response: - if response.status == 200: - messages = await response.json() - debug(f"Fetched messages: {messages}") # Debugging - return messages - else: - print(f"Failed to fetch messages: {response.status} | {await response.text()}") - return [] + try: + async with aiohttp.ClientSession() as session: + async with session.get(url, headers=HEADERS, params=params) as response: + if response.status == 200: + return await response.json() + else: + warning(f"Failed to fetch messages. Status Code: {response.status} | {await response.text()}") + return [] + except (aiohttp.ClientError, asyncio.TimeoutError) as e: + warning(f"Network error while fetching messages: {e}") + return [] # Return empty list to avoid crashing # Callback function when a new message is detected @@ -76,8 +84,10 @@ async def monitor_channel(channel_id, playerList:list): encoded_msg = txtMsgr.encode(f"[Discord] {message['author']['username']}: {message['content']}", True) for player in playerList: if player.streamWriterObject.is_closing(): + if not player.is_a_bot: + asyncio.create_task(discord_send_message(channel_id, f"{player.username} has left the server!")) playerList.remove(player) # Remove disconnected players - continue # Skip this player + continue player.streamWriterObject.write(encoded_msg) await player.streamWriterObject.drain() on_new_message(message) diff --git a/proxy.py b/proxy.py index b26d526..e4e28e3 100644 --- a/proxy.py +++ b/proxy.py @@ -283,6 +283,7 @@ async def handle_client(client_reader, client_writer): elif packet_type == "FSNETCMD_PREPARESIMULATION": welcomeMsg = YSchat.message(WELCOME_MESSAGE.format(username=player.username)) message_to_server.append(welcomeMsg) + player.is_a_bot = False if DISCORD_ENABLED: asyncio.create_task(discord_send_message(CHANNEL_ID, f"{player.username} has joined the server!")) -- cgit v1.2.3