diff options
Diffstat (limited to 'cogs/voice.py')
| -rw-r--r-- | cogs/voice.py | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/cogs/voice.py b/cogs/voice.py index b6b504a..07015cc 100644 --- a/cogs/voice.py +++ b/cogs/voice.py | |||
| @@ -7,22 +7,27 @@ import discord | |||
| 7 | from discord.ext import commands | 7 | from discord.ext import commands |
| 8 | import logging | 8 | import logging |
| 9 | import asyncio | 9 | import asyncio |
| 10 | from dataclasses import dataclass, field | ||
| 11 | from typing import Any | ||
| 10 | 12 | ||
| 11 | logger = logging.getLogger(__name__) | 13 | logger = logging.getLogger(__name__) |
| 12 | 14 | ||
| 13 | _voice_manager = None | ||
| 14 | 15 | ||
| 15 | def get_voice_manager(bot): | 16 | _voice_managers: dict[int, "VoiceManager"] = {} |
| 16 | global _voice_manager | ||
| 17 | if _voice_manager is None: | ||
| 18 | _voice_manager = VoiceManager(bot) | ||
| 19 | return _voice_manager | ||
| 20 | 17 | ||
| 21 | 18 | ||
| 19 | def get_voice_manager(bot) -> "VoiceManager": | ||
| 20 | """Get or create VoiceManager for a specific bot instance.""" | ||
| 21 | bot_id = bot.user.id if bot.user else id(bot) | ||
| 22 | if bot_id not in _voice_managers: | ||
| 23 | _voice_managers[bot_id] = VoiceManager(bot) | ||
| 24 | return _voice_managers[bot_id] | ||
| 25 | |||
| 26 | |||
| 27 | @dataclass | ||
| 22 | class VoiceManager: | 28 | class VoiceManager: |
| 23 | def __init__(self, bot): | 29 | bot: Any # type: ignore[no-any-import] |
| 24 | self.bot = bot | 30 | _voice_lock: asyncio.Lock = field(default_factory=asyncio.Lock, init=False) |
| 25 | self._voice_lock = asyncio.Lock() | ||
| 26 | 31 | ||
| 27 | async def join_channel(self, channel): | 32 | async def join_channel(self, channel): |
| 28 | """Join a voice channel by channel object.""" | 33 | """Join a voice channel by channel object.""" |
| @@ -71,17 +76,17 @@ class VoiceManager: | |||
| 71 | 76 | ||
| 72 | async def get_voice_status(self, guild): | 77 | async def get_voice_status(self, guild): |
| 73 | """Get voice client status including connection and playback state. | 78 | """Get voice client status including connection and playback state. |
| 74 | 79 | ||
| 75 | Returns: | 80 | Returns: |
| 76 | tuple: (voice_client, is_connected, is_playing) or (None, False, False) if no client | 81 | tuple: (voice_client, is_connected, is_playing) or (None, False, False) if no client |
| 77 | """ | 82 | """ |
| 78 | voice_client = await self.get_voice_client(guild) | 83 | voice_client = await self.get_voice_client(guild) |
| 79 | if voice_client is None: | 84 | if voice_client is None: |
| 80 | return None, False, False | 85 | return None, False, False |
| 81 | 86 | ||
| 82 | is_connected = voice_client.is_connected() | 87 | is_connected = voice_client.is_connected() |
| 83 | is_playing = voice_client.is_playing() if is_connected else False | 88 | is_playing = voice_client.is_playing() if is_connected else False |
| 84 | 89 | ||
| 85 | return voice_client, is_connected, is_playing | 90 | return voice_client, is_connected, is_playing |
| 86 | 91 | ||
| 87 | async def play_audio(self, channel, audio_source, volume=1.0): | 92 | async def play_audio(self, channel, audio_source, volume=1.0): |
| @@ -178,14 +183,18 @@ class Voice(commands.Cog): | |||
| 178 | if not await self._validate_user_voice_state(ctx): | 183 | if not await self._validate_user_voice_state(ctx): |
| 179 | return None | 184 | return None |
| 180 | return await self._create_voice_client(ctx) | 185 | return await self._create_voice_client(ctx) |
| 181 | 186 | ||
| 182 | if voice_client.channel != ctx.author.voice.channel: | 187 | if voice_client.channel != ctx.author.voice.channel: |
| 183 | return await self._move_voice_client(voice_client, ctx) | 188 | return await self._move_voice_client(voice_client, ctx) |
| 184 | 189 | ||
| 185 | return await self._verify_connection(voice_client) | 190 | return await self._verify_connection(voice_client) |
| 186 | 191 | ||
| 187 | async def connect(self, ctx): | 192 | async def connect(self, ctx): |
| 188 | voice_client = await self.ensure_voice_client(ctx) | 193 | voice_client = await self.ensure_voice_client(ctx) |
| 189 | if not voice_client: | 194 | if not voice_client: |
| 190 | return None | 195 | return None |
| 191 | return voice_client \ No newline at end of file | 196 | return voice_client |
| 197 | |||
| 198 | |||
| 199 | def setup(bot): | ||
| 200 | bot.add_cog(Voice(bot)) \ No newline at end of file | ||
