Bläddra i källkod

[2.9.1] Fix errors when corrupted pictures

Penta 3 veckor sedan
förälder
incheckning
517a142664
1 ändrade filer med 14 tillägg och 14 borttagningar
  1. 14 14
      chatbot.py

+ 14 - 14
chatbot.py

@@ -35,7 +35,7 @@ DELAY_TASK_UPDATE_STATUS = int(os.getenv('DELAY_TASK_UPDATE_STATUS', '30'))
 # Initialiser le client OpenAI asynchrone ici
 openai_client = AsyncOpenAI(api_key=OPENAI_API_KEY, base_url=URL_OPENAI_API)
 
-BOT_VERSION = "2.9.0"
+BOT_VERSION = "2.9.1"
 
 # Vérifier que les tokens et le prompt de personnalité sont récupérés
 if DISCORD_TOKEN is None or OPENAI_API_KEY is None or DISCORD_CHANNEL_ID is None:
@@ -172,20 +172,16 @@ def has_text(text):
 def resize_image(image_bytes, attachment_filename=None):
     """Redimensionne l'image selon le mode spécifié."""
 
-    try:
-        with Image.open(BytesIO(image_bytes)) as img:
-            original_format = img.format  # Stocker le format original
+    with Image.open(BytesIO(image_bytes)) as img:
+        original_format = img.format  # Stocker le format original
 
-            img.thumbnail((2000, 2000))
+        img.thumbnail((2000, 2000))
 
-            buffer = BytesIO()
-            img_format = img.format or _infer_image_format(attachment_filename)
-            img.save(buffer, format=img_format)
+        buffer = BytesIO()
+        img_format = img.format or _infer_image_format(attachment_filename)
+        img.save(buffer, format=img_format)
 
-            return buffer.getvalue()
-    except Exception as e:
-        logger.error(f"Erreur lors du redimensionnement de l'image : {e}")
-        raise
+        return buffer.getvalue()
 
 async def encode_image_from_attachment(attachment):
     """Encode une image depuis une pièce jointe en base64 après redimensionnement."""
@@ -448,8 +444,12 @@ async def on_message(message):
         for attachment in message.attachments:
             # Vérifier si c'est un fichier avec une extension autorisée
             if attachment.content_type and attachment.content_type.startswith('image/'):
-                image_data = await encode_image_from_attachment(attachment)
-                break
+                try:
+                    image_data = await encode_image_from_attachment(attachment)
+                    break
+                except Exception as e:
+                    await message.channel.send("Il semble qu'il y ai un souci avec ton image, je ne peux pas l'ouvrir.")
+                    logger.error(f"Erreur lors de la conversion de l'image : {e}")
             else:
                 try:
                     file_content = await read_text_file(attachment)