diff options
Diffstat (limited to 'cogs/voice.py')
| -rw-r--r-- | cogs/voice.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/cogs/voice.py b/cogs/voice.py new file mode 100644 index 0000000..600a7eb --- /dev/null +++ b/cogs/voice.py | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | import discord | ||
| 2 | from discord.ext import commands | ||
| 3 | import logging | ||
| 4 | import asyncio | ||
| 5 | |||
| 6 | logger = logging.getLogger(__name__) | ||
| 7 | |||
| 8 | class Voice(commands.Cog): | ||
| 9 | def __init__(self, bot): | ||
| 10 | self.bot = bot | ||
| 11 | |||
| 12 | async def ensure_voice_client(self, ctx): | ||
| 13 | """Ensure we have a valid voice client connection.""" | ||
| 14 | if not ctx.author.voice or not ctx.author.voice.channel: | ||
| 15 | await ctx.send("You need to be in a voice channel first!") | ||
| 16 | return None | ||
| 17 | |||
| 18 | voice_client = discord.utils.get(self.bot.voice_clients, guild=ctx.guild) | ||
| 19 | |||
| 20 | # If no voice client exists, create one | ||
| 21 | if not voice_client or not voice_client.is_connected(): | ||
| 22 | try: | ||
| 23 | voice_client = await ctx.author.voice.channel.connect(timeout=30.0, reconnect=True) | ||
| 24 | logger.info(f"Connected to voice channel: {ctx.author.voice.channel}") | ||
| 25 | except asyncio.TimeoutError: | ||
| 26 | await ctx.send("Connection to voice channel timed out.") | ||
| 27 | except discord.ClientException as e: | ||
| 28 | logger.error(f"Failed to connect to voice channel: {e}") | ||
| 29 | await ctx.send("Failed to connect to voice channel. Please try again.", ephemeral=True) | ||
| 30 | # If we're in a different channel, move to the user's channel | ||
| 31 | elif voice_client.channel != ctx.author.voice.channel: | ||
| 32 | try: | ||
| 33 | await voice_client.move_to(ctx.author.voice.channel) | ||
| 34 | logger.info(f"Moved to voice channel: {ctx.author.voice.channel}") | ||
| 35 | except asyncio.TimeoutError: | ||
| 36 | await ctx.send("Connection to voice channel timed out.") | ||
| 37 | except discord.ClientException as e: | ||
| 38 | logger.error(f"Failed to move to voice channel: {e}") | ||
| 39 | await ctx.send("Failed to move to your voice channel. Please try again.", ephemeral=True) | ||
| 40 | |||
| 41 | # Verify the connection is healthy | ||
| 42 | if not voice_client.is_connected(): | ||
| 43 | try: | ||
| 44 | await voice_client.connect(reconnect=True, timeout=10.0) | ||
| 45 | except Exception as e: | ||
| 46 | logger.error(f"Failed to reconnect to voice: {e}") | ||
| 47 | await ctx.send("Failed to establish voice connection. Please try again.") | ||
| 48 | return None | ||
| 49 | |||
| 50 | async def connect(self, ctx): | ||
| 51 | """Connect to the user's voice channel with improved connection handling.""" | ||
| 52 | voice_client = await self.ensure_voice_client(ctx) | ||
| 53 | if not voice_client: | ||
| 54 | return None | ||
| 55 | |||
| 56 | return voice_client | ||
| 57 | |||
| 58 | |||
| 59 | def setup(bot): | ||
| 60 | bot.add_cog(Voice(bot)) \ No newline at end of file | ||
