Revert voice activity changes
This commit is contained in:
@@ -4,12 +4,11 @@ import os
|
||||
from discord import VoiceClient, VoiceChannel, opus, Activity, ActivityType, Intents
|
||||
from discord.ext import commands
|
||||
from typing import Optional, Dict
|
||||
from internal.NoiseGatev2 import AudioStreamManager, NoiseGateSource
|
||||
from internal.logger import create_logger
|
||||
from internal.NoiseGatev2 import AudioStreamManager, NoiseGateSource
|
||||
|
||||
LOGGER = create_logger(__name__)
|
||||
|
||||
# Configure discord intents
|
||||
intents = Intents.default()
|
||||
intents.voice_states = True
|
||||
intents.guilds = True
|
||||
@@ -42,18 +41,13 @@ class DiscordBotManager:
|
||||
|
||||
@self.bot.event
|
||||
async def on_voice_state_update(member, before, after):
|
||||
if member != self.bot.user:
|
||||
return
|
||||
|
||||
if member != self.bot.user: return
|
||||
if before.channel is None and after.channel is not None:
|
||||
LOGGER.info(f"{member.name} joined voice channel {after.channel.name}")
|
||||
self._voice_ready_event.set()
|
||||
|
||||
elif before.channel is not None and after.channel is not None and before.channel != after.channel:
|
||||
LOGGER.info(f"{member.name} was moved to voice channel {after.channel.name}")
|
||||
if not self._voice_ready_event.is_set():
|
||||
self._voice_ready_event.set()
|
||||
|
||||
if not self._voice_ready_event.is_set(): self._voice_ready_event.set()
|
||||
elif before.channel is not None and after.channel is None:
|
||||
LOGGER.warning(f"{member.name} left voice channel {before.channel.name}")
|
||||
guild_id = before.channel.guild.id
|
||||
@@ -75,8 +69,7 @@ class DiscordBotManager:
|
||||
LOGGER.info("Bot is ready.")
|
||||
except asyncio.TimeoutError:
|
||||
LOGGER.error("Timeout waiting for bot to become ready.")
|
||||
if self.bot_task and not self.bot_task.done():
|
||||
self.bot_task.cancel()
|
||||
if self.bot_task and not self.bot_task.done(): self.bot_task.cancel()
|
||||
raise RuntimeError("Bot failed to become ready within timeout.")
|
||||
|
||||
async def stop_bot(self):
|
||||
@@ -107,14 +100,10 @@ class DiscordBotManager:
|
||||
voice_client = await channel.connect(timeout=60.0, reconnect=True)
|
||||
await asyncio.wait_for(self._voice_ready_event.wait(), timeout=15.0)
|
||||
|
||||
# Create a single audio manager for this connection
|
||||
audio_manager = AudioStreamManager(input_device_index=device_id)
|
||||
|
||||
# Create the noise-gated audio source
|
||||
audio_source = NoiseGateSource(audio_manager.get_stream(), threshold=ng_threshold)
|
||||
|
||||
# Play the source
|
||||
voice_client.play(audio_source, after=lambda e: print(f'Player error: {e}') if e else None)
|
||||
voice_client.play(audio_source, after=lambda e: LOGGER.error(f'Player error: {e}') if e else None)
|
||||
|
||||
self.voice_connections[guild_id] = {
|
||||
"client": voice_client,
|
||||
@@ -124,6 +113,8 @@ class DiscordBotManager:
|
||||
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Failed to connect to voice channel: {e}", exc_info=True)
|
||||
if guild_id in self.voice_connections: # Cleanup if join fails midway
|
||||
await self.leave_voice_channel(guild_id)
|
||||
raise
|
||||
|
||||
async def leave_voice_channel(self, guild_id: int):
|
||||
@@ -137,19 +128,23 @@ class DiscordBotManager:
|
||||
voice_client.stop()
|
||||
await voice_client.disconnect()
|
||||
|
||||
# Terminate the audio manager to release PyAudio resources
|
||||
audio_manager = connection_info.get("audio_manager")
|
||||
if audio_manager:
|
||||
audio_manager.terminate()
|
||||
|
||||
del self.voice_connections[guild_id]
|
||||
# Use pop to safely remove the key
|
||||
self.voice_connections.pop(guild_id, None)
|
||||
LOGGER.info(f"Left guild {guild_id} voice channel.")
|
||||
|
||||
async def load_opus(self):
|
||||
# ... this method is unchanged ...
|
||||
if opus.is_loaded():
|
||||
LOGGER.info("Opus library is already loaded.")
|
||||
return
|
||||
|
||||
processor = platform.machine()
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
LOGGER.debug(f"Processor: {processor}, OS: {os.name}")
|
||||
|
||||
LOGGER.debug(f"Attempting to load Opus. Processor: {processor}, OS: {os.name}")
|
||||
try:
|
||||
if os.name == 'nt':
|
||||
if processor == "AMD64":
|
||||
@@ -164,20 +159,11 @@ class DiscordBotManager:
|
||||
LOGGER.info("Loaded OPUS library for armv7l")
|
||||
else:
|
||||
opus.load_opus('libopus.so.0')
|
||||
LOGGER.info(f"Loaded system OPUS library for {processor}")
|
||||
LOGGER.info(f"Attempted to load system OPUS library for {processor}")
|
||||
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Failed to load OPUS library: {e}")
|
||||
raise RuntimeError("Could not load a valid Opus library. Voice functionality will fail.")
|
||||
|
||||
async def set_presence(self, system_name: str):
|
||||
# ... this method is unchanged ...
|
||||
if not self.bot or not self.bot.is_ready():
|
||||
LOGGER.warning("Bot is not ready, cannot set presence.")
|
||||
return
|
||||
|
||||
try:
|
||||
activity = Activity(type=ActivityType.listening, name=system_name)
|
||||
await self.bot.change_presence(activity=activity)
|
||||
LOGGER.info(f"Bot presence set to 'Listening to {system_name}'")
|
||||
except Exception as pe:
|
||||
LOGGER.error(f"Unable to set presence: '{pe}'")
|
||||
|
||||
if not opus.is_loaded():
|
||||
raise RuntimeError("Opus library could not be loaded. Please ensure it is installed correctly.")
|
||||
Reference in New Issue
Block a user