|
|
@@ -15,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
|
|
|
@@ -26,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
|
|
|
@@ -753,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))
|