diff --git a/bot.py b/bot.py index 5db9963..413133a 100644 --- a/bot.py +++ b/bot.py @@ -795,11 +795,12 @@ async def quote_random_slash(ctx: SlashContext, author: Optional[str] = None, tt if tts: voice: discord.VoiceState = ctx.author.voice if voice.channel is not None: - async def after_play(e: discord.DiscordException, vp: discord.VoiceProtocol): - await vp.disconnect(force=True) + source, tts_destroyer = text_to_speech(author + " sagte: " + quote) + vp: discord.VoiceClient = await connect_and_play(voice.channel, source) + while vp.is_playing() and vp.channel == voice.channel: + await asyncio.sleep(0.5) - source, tts_destroyer = text_to_speech(quote) - await connect_and_play(voice.channel, source, after_play=after_play) + await vp.disconnect() tts_destroyer() diff --git a/lib/utils.py b/lib/utils.py index 9437c9f..66c68e2 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -53,8 +53,8 @@ def text_to_speech(text: str, lang: str = "de") -> (discord.AudioSource, Callabl async def connect_and_play( channel: discord.VoiceChannel, source: discord.AudioSource, - after_play: Optional[Callable[[discord.DiscordException, discord.VoiceProtocol], Coroutine[Any, Any, Any]]] = None -) -> Optional[discord.VoiceProtocol]: + after_play: Optional[Callable[[discord.DiscordException, discord.VoiceProtocol], Any]] = None +) -> Optional[discord.VoiceClient]: # noinspection PyTypeChecker client: discord.VoiceClient = await channel.connect() after_callback: Optional[Callable[[discord.DiscordException], Any]] @@ -62,8 +62,7 @@ async def connect_and_play( after_callback = None else: def callback(exc: discord.DiscordException) -> Any: - loop = asyncio.get_event_loop() - return loop.run_until_complete(after_play(exc, client)) + after_play(exc, client) after_callback = callback