diff --git a/DB.py b/DB.py index 43c228f..7ca85ec 100644 --- a/DB.py +++ b/DB.py @@ -1,3 +1,5 @@ +from datetime import datetime + import peewee database = peewee.SqliteDatabase('voice_cache.sqlite') @@ -18,5 +20,16 @@ class Speaker(BaseModel): speaker = peewee.CharField() +class SynthesisErrors(peewee.Model): + speaker = peewee.CharField() + text = peewee.TextField() + timestamp = peewee.DateTimeField(default=datetime.now) + + class Meta: + database = database + primary_key = peewee.CompositeKey('speaker', 'text') + + Prefix.create_table() Speaker.create_table() +SynthesisErrors.create_table() diff --git a/cogs/TTSCommands.py b/cogs/TTSCommands.py index a23b43b..e2c2733 100644 --- a/cogs/TTSCommands.py +++ b/cogs/TTSCommands.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +import subprocess + from discord.ext import commands from discord.ext.commands import Context import discord @@ -62,10 +64,24 @@ class TTSCommands(commands.Cog, Observ.Observer): speaker: Speakers = await self._get_speaker(message.guild.id) - wav_file_like_object = self.tts.synthesize_text(message.content, speaker=speaker) - sound_source = FFmpegPCMAudio(wav_file_like_object, pipe=True) + # check if message will fail on synthesis + if DB.SynthesisErrors.select()\ + .where(DB.SynthesisErrors.speaker == speaker.value)\ + .where(DB.SynthesisErrors.text == message.content)\ + .count() == 1: + # Then we will not try to synthesis it + await message.channel.send(f"I will not synthesis this message due to TTS engine limitations") + return - voice_client.play(sound_source) + try: + wav_file_like_object = self.tts.synthesize_text(message.content, speaker=speaker) + sound_source = FFmpegPCMAudio(wav_file_like_object, pipe=True, stderr=subprocess.PIPE) + voice_client.play(sound_source) + + except Exception as synth_exception: + logger.warning(f'Exception on synthesize {message.content!r}: {synth_exception}', exc_info=synth_exception) + await message.channel.send(f'Internal error') + DB.SynthesisErrors.create(speaker=speaker.value, text=message.content) @commands.command('getAllSpeakers') async def get_speakers(self, ctx: Context):