WIP: implement-discord-module #4
@@ -30,6 +30,9 @@ NODE_LONG = os.getenv("NODE_LONG")
|
|||||||
# Global flag to track MQTT connection state
|
# Global flag to track MQTT connection state
|
||||||
MQTT_CONNECTED = False
|
MQTT_CONNECTED = False
|
||||||
|
|
||||||
|
# Global variable to hold the main event loop
|
||||||
|
main_loop = None
|
||||||
|
|
||||||
# Initialize the Discord Bot
|
# Initialize the Discord Bot
|
||||||
discord_bot = DiscordRadioBot(listen_port=23457, forward_ports=[23456])
|
discord_bot = DiscordRadioBot(listen_port=23457, forward_ports=[23456])
|
||||||
|
|
||||||
@@ -113,11 +116,17 @@ def handle_c2_command(topic, payload):
|
|||||||
token = data.get("token")
|
token = data.get("token")
|
||||||
channel_id = data.get("channel_id")
|
channel_id = data.get("channel_id")
|
||||||
if token and channel_id:
|
if token and channel_id:
|
||||||
asyncio.create_task(discord_bot.start_session(token, channel_id))
|
if main_loop and main_loop.is_running():
|
||||||
|
asyncio.run_coroutine_threadsafe(discord_bot.start_session(token, channel_id), main_loop)
|
||||||
|
else:
|
||||||
|
LOGGER.error("Main event loop not available to start Discord session.")
|
||||||
LOGGER.info("Initiating Discord Session...")
|
LOGGER.info("Initiating Discord Session...")
|
||||||
|
|
||||||
elif command_type == "discord_leave":
|
elif command_type == "discord_leave":
|
||||||
asyncio.create_task(discord_bot.stop_session())
|
if main_loop and main_loop.is_running():
|
||||||
|
asyncio.run_coroutine_threadsafe(discord_bot.stop_session(), main_loop)
|
||||||
|
else:
|
||||||
|
LOGGER.error("Main event loop not available to stop Discord session.")
|
||||||
LOGGER.info("Ending Discord Session...")
|
LOGGER.info("Ending Discord Session...")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -465,6 +474,8 @@ async def mqtt_lifecycle_manager():
|
|||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
|
global main_loop
|
||||||
|
main_loop = asyncio.get_running_loop()
|
||||||
# Store the task so we can cancel it if needed (optional)
|
# Store the task so we can cancel it if needed (optional)
|
||||||
app.state.mqtt_task = asyncio.create_task(mqtt_lifecycle_manager())
|
app.state.mqtt_task = asyncio.create_task(mqtt_lifecycle_manager())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user