Compare commits

...

2 Commits

View File

@ -6,7 +6,7 @@ from discord.ext import commands
from discord.ext.commands import Context from discord.ext.commands import Context
import discord import discord
import DB import DB
from typing import Union from typing import Union, Optional
from loguru import logger from loguru import logger
from TTSSilero import TTSSileroCached from TTSSilero import TTSSileroCached
from TTSSilero import Speakers from TTSSilero import Speakers
@ -78,22 +78,30 @@ class TTSCore(commands.Cog, Observ.Observer):
try: try:
wav_file_like_object = self.tts.synthesize_text(message.content, speaker=speaker) wav_file_like_object = self.tts.synthesize_text(message.content, speaker=speaker)
sound_source = FFmpegPCMAudio(wav_file_like_object, pipe=True, stderr=subprocess.PIPE) sound_source = FFmpegPCMAudio(wav_file_like_object, pipe=True, stderr=subprocess.PIPE)
except Exception as synth_exception:
logger.opt(exception=True).warning(f'Exception on synthesize {message.content!r}: {synth_exception}')
await message.channel.send(f'Synthesize error')
DB.SynthesisErrors.create(speaker=speaker.value, text=message.content)
return
else:
try:
if voice_client.is_playing(): if voice_client.is_playing():
# Then we need to enqueue prepared sound for playing through self.tts_queues mechanism # Then we need to enqueue prepared sound for playing through self.tts_queues mechanism
self.tts_queues[message.guild.id].append(sound_source) self.tts_queues[message.guild.id].append(sound_source)
await message.channel.send(f"Enqueued for play, queue size: {len(self.tts_queues[message.guild.id])}") await message.channel.send(f"Enqueued for play, queue size: {len(self.tts_queues[message.guild.id])}")
return return
voice_client.play(sound_source, after=lambda e: self.queue_player(message)) voice_client.play(sound_source, after=lambda e: self.queue_player(message))
except Exception as synth_exception: except Exception as play_exception:
logger.opt(exception=True).warning(f'Exception on synthesize {message.content!r}: {synth_exception}') logger.opt(exception=True).warning(f'Exception on playing for: {message.guild.name}[#{message.channel.name}]: {message.author.display_name} / {play_exception}')
await message.channel.send(f'Internal error') await message.channel.send(f'Playing error')
DB.SynthesisErrors.create(speaker=speaker.value, text=message.content) return
def queue_player(self, message: discord.Message): def queue_player(self, message: discord.Message):
voice_client: Union[discord.VoiceClient, None] = message.guild.voice_client voice_client: Optional[discord.VoiceClient] = message.guild.voice_client
if voice_client is None: if voice_client is None:
# don't play anything and clear queue for whole guild # don't play anything and clear queue for whole guild
del self.tts_queues[message.guild.id] del self.tts_queues[message.guild.id]
@ -118,6 +126,8 @@ class TTSCore(commands.Cog, Observ.Observer):
if members[0].id == self.bot.user.id: if members[0].id == self.bot.user.id:
await before.channel.guild.voice_client.disconnect(force=False) await before.channel.guild.voice_client.disconnect(force=False)
# TODO: leave voice channel after being moved there alone
async def setup(bot): async def setup(bot):
await bot.add_cog(TTSCore(bot)) await bot.add_cog(TTSCore(bot))