summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRitabrata Das <[email protected]>2025-02-11 23:35:57 +0530
committerRitabrata Das <[email protected]>2025-02-11 23:35:57 +0530
commit328ed234d9c5ce96c2b8079b08e91c6064bb6ff2 (patch)
tree62edc772bbd64316d038272995b29693b0e77b30 /lib
parent37ab4afb4b35a292a2b02d28d8406407a5ff6263 (diff)
Fix discord chat sync
Diffstat (limited to 'lib')
-rw-r--r--lib/discordSync.py28
1 files changed, 15 insertions, 13 deletions
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)