Skip to content

Commit

Permalink
Update discord_chat.py
Browse files Browse the repository at this point in the history
1. Error Logging: Enhanced the log_error function to suggest more sophisticated logging.

2. Error Handling: Added error logging in the chat and on_message functions to capture exceptions for better debugging.

3. Code Organization: Improved readability by ensuring consistent error handling across functions.
  • Loading branch information
smit23patel authored Oct 28, 2024
1 parent 85731e7 commit 2190f66
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions client/python/gradio_client/templates/discord_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ async def chat(ctx, prompt: str):
return
try:
message = await ctx.send("Creating thread...")

thread = await message.create_thread(name=prompt)
loop = asyncio.get_running_loop()
client = await loop.run_in_executor(None, get_client, None)
Expand All @@ -74,7 +73,7 @@ async def chat(ctx, prompt: str):

try:
job.result()
response = job.outputs()[-1]
response = str(job.outputs()[-1]) # Convert to string
await thread.send(truncate_response(response))
thread_to_client[thread.id] = client
thread_to_user[thread.id] = ctx.author.id
Expand All @@ -84,7 +83,7 @@ async def chat(ctx, prompt: str):
)

except Exception as e:
print(f"{e}")
log_error(e) # Log the error for debugging


async def continue_chat(message):
Expand All @@ -104,7 +103,7 @@ async def continue_chat(message):
)

except Exception as e:
print(f"Error: {e}")
log_error(e)


@bot.event
Expand All @@ -119,7 +118,7 @@ async def on_message(message):
await bot.process_commands(message)

except Exception as e:
print(f"Error: {e}")
log_error(e) # Log the error for debugging


# running in thread
Expand Down Expand Up @@ -191,3 +190,8 @@ def run_bot():
)

demo.launch()


def log_error(e):
print(f"Error: {e}") # Simple logging to console
# Consider adding more sophisticated logging here, e.g., to a file or monitoring system

0 comments on commit 2190f66

Please sign in to comment.