|
|
@@ -1,6 +1,12 @@
|
|
|
import os
|
|
|
import logging
|
|
|
+
|
|
|
+import discord.utils
|
|
|
+
|
|
|
+discord.utils.setup_logging = lambda *args, **kwargs: None
|
|
|
+
|
|
|
import discord
|
|
|
+
|
|
|
import json
|
|
|
import urllib3
|
|
|
import base64
|
|
|
@@ -9,6 +15,8 @@ import asyncio
|
|
|
import websockets
|
|
|
import numpy as np
|
|
|
import time
|
|
|
+import signal
|
|
|
+import sys
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
from openai import AsyncOpenAI, OpenAIError
|
|
|
@@ -20,7 +28,6 @@ from enum import Enum
|
|
|
from discord.sinks import Sink
|
|
|
from scipy.signal import resample_poly
|
|
|
|
|
|
-
|
|
|
from discord.ext import tasks
|
|
|
|
|
|
# Charger les variables d'environnement depuis le fichier .env
|
|
|
@@ -45,7 +52,12 @@ WHISPER_WS_URL = os.getenv("WHISPER_WS_URL", "ws://whisper-stt:8000/ws/transcrib
|
|
|
# Initialiser le client OpenAI asynchrone ici
|
|
|
openai_client = AsyncOpenAI(api_key=OPENAI_API_KEY, base_url=URL_OPENAI_API)
|
|
|
|
|
|
-BOT_VERSION = "3.0.0-alpha1"
|
|
|
+BOT_VERSION = "3.0.0-alpha2"
|
|
|
+
|
|
|
+# Vérifier la version de Discord
|
|
|
+if not sys.version_info[:2] >= (3, 13):
|
|
|
+ print("ERROR: Requires python 3.13 or newer.")
|
|
|
+ exit(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:
|
|
|
@@ -59,7 +71,7 @@ with open(PERSONALITY_PROMPT_FILE, 'r', encoding='utf-8') as f:
|
|
|
PERSONALITY_PROMPT = f.read().strip()
|
|
|
|
|
|
# Log configuration
|
|
|
-log_format = '%(asctime)-13s : %(name)-15s : %(levelname)-8s : %(message)s'
|
|
|
+log_format = '%(asctime)-13s : %(name)-25s : %(levelname)-8s : %(message)s'
|
|
|
logging.basicConfig(handlers=[logging.FileHandler("./chatbot.log", 'a', 'utf-8')], format=log_format, level=LOG_LEVEL)
|
|
|
|
|
|
console = logging.StreamHandler()
|
|
|
@@ -71,6 +83,10 @@ logger.setLevel("DEBUG")
|
|
|
|
|
|
logging.getLogger('').addHandler(console)
|
|
|
|
|
|
+discord_logger = logging.getLogger("discord")
|
|
|
+discord_logger.handlers.clear()
|
|
|
+discord_logger.propagate = True
|
|
|
+
|
|
|
httpx_logger = logging.getLogger('httpx')
|
|
|
httpx_logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
@@ -738,6 +754,28 @@ async def before_update_status():
|
|
|
await client_discord.wait_until_ready()
|
|
|
logger.info("Démarrage de la tâche update_status")
|
|
|
|
|
|
-
|
|
|
-# Démarrer le bot Discord
|
|
|
-client_discord.run(DISCORD_TOKEN)
|
|
|
+async def exit_app(signum=None):
|
|
|
+ globals.logger.debug("Received signal {}".format(signum))
|
|
|
+ globals.logger.info("Closing all tasks...")
|
|
|
+
|
|
|
+ # On ferme la connexion à Discord
|
|
|
+ if client_discord:
|
|
|
+ await client_discord.close()
|
|
|
+
|
|
|
+ globals.logger.critical("Script halted.")
|
|
|
+
|
|
|
+def handle_signal(signum, frame):
|
|
|
+ loop = asyncio.get_event_loop()
|
|
|
+ loop.create_task(exit_app(signum))
|
|
|
+
|
|
|
+# Starting main function
|
|
|
+if __name__ == "__main__":
|
|
|
+ # Catch SIGINT signal (Ctrl-C)
|
|
|
+ signal.signal(signal.SIGINT, handle_signal)
|
|
|
+ signal.signal(signal.SIGTERM, handle_signal)
|
|
|
+
|
|
|
+ try:
|
|
|
+ # Démarrer le bot Discord
|
|
|
+ client_discord.run(DISCORD_TOKEN)
|
|
|
+ except Exception as e:
|
|
|
+ globals.logger.error("Encountered exception while running the bot: {}".format(e))
|