From 91e8e90998d15293d4871086cb98ac203304ee8b Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Mon, 15 Apr 2024 00:16:39 -0400 Subject: [PATCH] Add updated changes from DRB repo --- main.py | 73 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index a0491d7..fb9b35f 100644 --- a/main.py +++ b/main.py @@ -9,8 +9,31 @@ from discord import Intents, opus from discord.ext import commands from NoiseGatev2 import NoiseGate -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +running_dir = os.path.dirname(__file__) + +# Ensure the directory exists +log_dir = f"{running_dir}/logs" +os.makedirs(log_dir, exist_ok=True) + +# Create the file if it doesn't exist +log_file = f"{running_dir}/logs/pdab.log" +open(log_file, 'a').close() + +# Configure logging +logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s") +logger = logging.getLogger('main') +logger.setLevel(logging.INFO) + +fileHandler = logging.FileHandler(log_file) +fileHandler.setFormatter(logFormatter) +logger.addHandler(fileHandler) + +consoleHandler = logging.StreamHandler() +consoleHandler.setFormatter(logFormatter) +logger.addHandler(consoleHandler) + +# Example usage +logger.info("Logging initialized successfully.") sio = socketio.AsyncClient() client = None @@ -24,9 +47,9 @@ def load_opus(): logger.info(f"Processor: {processor}") if os.name == 'nt': - opus_path = './opus/libopus_amd64.dll' if processor == "AMD64" else None + opus_path = f'{running_dir}/opus/libopus_amd64.dll' if processor == "AMD64" else None else: - opus_path = './opus/libopus_aarcch64.so' if processor == "aarch64" else './opus/libopus_armv7l.so' + opus_path = f'{running_dir}/opus/libopus_aarcch64.so' if processor == "aarch64" else f'{running_dir}/opus/libopus_armv7l.so' if opus_path: opus.load_opus(opus_path) @@ -41,22 +64,26 @@ async def join_voice_channel(channel_id): global device_id, ng_threshold channel = client.get_channel(int(channel_id)) logger.info(f"Joining voice channel {channel}") - voice_connection = await channel.connect(timeout=60.0, reconnect=True) - - if opus.is_loaded(): - logger.info("OPUS library loaded successfully") - stream_handler = NoiseGate( - _input_device_index=device_id, - _voice_connection=voice_connection, - _noise_gate_threshold=ng_threshold - ) - stream_handler.run() - logger.info("Audio stream started") - return True - else: - logger.error("Failed to load OPUS library") - return False + try: + voice_connection = await channel.connect(timeout=60.0, reconnect=True) + if opus.is_loaded(): + logger.info("OPUS library loaded successfully") + logger.info(f"Input index: {device_id}, Voice Connection: {voice_connection}, Channel: {channel}") + stream_handler = NoiseGate( + _input_device_index=device_id, + _voice_connection=voice_connection, + _noise_gate_threshold=ng_threshold + ) + stream_handler.run() + logger.info("Audio stream started") + return True + else: + logger.error("Failed to load OPUS library") + return False + except Exception as e: + logging.error(e) + logging.info('error encountered') async def leave_voice_channel(guild_id): guild = await client.fetch_guild(guild_id) @@ -161,11 +188,17 @@ async def check_client_is_open(): async def request_discord_id(): return get_discord_id() +@sio.event +async def request_client_close(): + exit() + async def on_ready(): logger.info(f"We have logged in as {client.user}") logger.info("Loading OPUS library") - if not load_opus(): + load_opus() + + if not opus.is_loaded(): return # Send update to socket server