From a2b5b10625126b7e98643af9fe40569c9e004047 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 18 Apr 2026 00:24:50 +0200 Subject: refactor + preparing bot for public use Signed-off-by: salmonik --- cogs/voice.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'cogs/voice.py') 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 from discord.ext import commands import logging import asyncio +from dataclasses import dataclass, field +from typing import Any logger = logging.getLogger(__name__) -_voice_manager = None -def get_voice_manager(bot): - global _voice_manager - if _voice_manager is None: - _voice_manager = VoiceManager(bot) - return _voice_manager +_voice_managers: dict[int, "VoiceManager"] = {} +def get_voice_manager(bot) -> "VoiceManager": + """Get or create VoiceManager for a specific bot instance.""" + bot_id = bot.user.id if bot.user else id(bot) + if bot_id not in _voice_managers: + _voice_managers[bot_id] = VoiceManager(bot) + return _voice_managers[bot_id] + + +@dataclass class VoiceManager: - def __init__(self, bot): - self.bot = bot - self._voice_lock = asyncio.Lock() + bot: Any # type: ignore[no-any-import] + _voice_lock: asyncio.Lock = field(default_factory=asyncio.Lock, init=False) async def join_channel(self, channel): """Join a voice channel by channel object.""" @@ -71,17 +76,17 @@ class VoiceManager: async def get_voice_status(self, guild): """Get voice client status including connection and playback state. - + Returns: tuple: (voice_client, is_connected, is_playing) or (None, False, False) if no client """ voice_client = await self.get_voice_client(guild) if voice_client is None: return None, False, False - + is_connected = voice_client.is_connected() is_playing = voice_client.is_playing() if is_connected else False - + return voice_client, is_connected, is_playing async def play_audio(self, channel, audio_source, volume=1.0): @@ -178,14 +183,18 @@ class Voice(commands.Cog): if not await self._validate_user_voice_state(ctx): return None return await self._create_voice_client(ctx) - + if voice_client.channel != ctx.author.voice.channel: return await self._move_voice_client(voice_client, ctx) - + return await self._verify_connection(voice_client) async def connect(self, ctx): voice_client = await self.ensure_voice_client(ctx) if not voice_client: return None - return voice_client \ No newline at end of file + return voice_client + + +def setup(bot): + bot.add_cog(Voice(bot)) \ No newline at end of file -- cgit v1.2.3