diff options
| author | Ritabrata Das <[email protected]> | 2025-02-11 23:35:57 +0530 |
|---|---|---|
| committer | Ritabrata Das <[email protected]> | 2025-02-11 23:35:57 +0530 |
| commit | 328ed234d9c5ce96c2b8079b08e91c6064bb6ff2 (patch) | |
| tree | 62edc772bbd64316d038272995b29693b0e77b30 | |
| parent | 37ab4afb4b35a292a2b02d28d8406407a5ff6263 (diff) | |
Fix discord chat sync
| -rw-r--r-- | config.py | 6 | ||||
| -rw-r--r-- | lib/discordSync.py | 28 | ||||
| -rw-r--r-- | proxy.py | 1 |
3 files changed, 18 insertions, 17 deletions
@@ -61,11 +61,11 @@ SMOKE_LIFE = 5 # On Modern Linux distros you may need to create a virtual environment to install # the dependencies -DISCORD_ENABLED = False +DISCORD_ENABLED = True # Make sure to enable read message intent for the bot. -DISCORD_TOKEN = "YOUR_DISCORD_BOT_TOKEN" +DISCORD_TOKEN = "DISCORD_BOT_TOKEN" # Channel ID for the chat -CHANNEL_ID = 0 +CHANNEL_ID = 0 # Channel ID, as an integer diff --git a/lib/discordSync.py b/lib/discordSync.py index e6618a9..9fe4f00 100644 --- a/lib/discordSync.py +++ b/lib/discordSync.py @@ -31,22 +31,23 @@ async def discord_send_message(channel_id, content): # 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 # Fetch the most recent message - } + params = {'limit': 1} if last_message_id: - params['after'] = last_message_id # Fetch only new messages + 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() - return messages # Return the list of messages + debug(f"Fetched messages: {messages}") # Debugging + return messages else: - print(f'Failed to fetch messages. Status Code: {response.status} | {await response.text()}') + print(f"Failed to fetch messages: {response.status} | {await response.text()}") return [] + # Callback function when a new message is detected def on_new_message(message): author = message['author'] @@ -56,7 +57,7 @@ def on_new_message(message): username = author['username'] content = message['content'] - debug(f'New Discord message from {username}: {content}') + print(f'New Discord message from {username}: {content}') # Asynchronous function to monitor a Discord channel for new messages async def monitor_channel(channel_id, playerList:list): @@ -69,11 +70,12 @@ async def monitor_channel(channel_id, playerList:list): message = messages[0] if messages: message = messages[0] - if last_message_id is None or message['id'] > last_message_id and not message['author'].get("bot"): # Ensure only newer messages are processed and bots are ignored + if last_message_id is None or message['id'] > last_message_id: # Ensure only newer messages are processed last_message_id = message['id'] - encoded_msg = txtMsgr.encode(f"[Discord] {message['author']['username']}: {message['content']}", True) - for player in playerList: - player.streamWriterObject.write(encoded_msg) - await player.streamWriterObject.drain() - on_new_message(message) + if not message['author'].get('bot'): # Skip messages from bot + encoded_msg = txtMsgr.encode(f"[Discord] {message['author']['username']}: {message['content']}", True) + for player in playerList: + player.streamWriterObject.write(encoded_msg) + await player.streamWriterObject.drain() + on_new_message(message) await asyncio.sleep(1) # Poll every second (adjust as needed) @@ -283,7 +283,6 @@ 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) - print("Here!") if DISCORD_ENABLED: asyncio.create_task(discord_send_message(CHANNEL_ID, f"{player.username} has joined the server!")) |
