diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/tts.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/cogs/tts.py b/cogs/tts.py new file mode 100644 index 0000000..41dc366 --- /dev/null +++ b/cogs/tts.py | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | import discord | ||
| 2 | from discord.ext import commands | ||
| 3 | from gtts import gTTS | ||
| 4 | import os | ||
| 5 | import datetime | ||
| 6 | |||
| 7 | class TTS(commands.Cog): | ||
| 8 | def __init__(self, bot): | ||
| 9 | self.bot = bot | ||
| 10 | |||
| 11 | @commands.slash_command(name="tts", description="Generuje TTS dla danego tekstu") | ||
| 12 | async def tts(self, ctx, | ||
| 13 | text: str = discord.Option(str, "Tekst do przeczytania", required=True), | ||
| 14 | output: str = discord.Option(str, "Wybierz sposób odtwarzania", choices=["file", "vc"], required=True) | ||
| 15 | ): | ||
| 16 | tts = gTTS(text=text, lang='pl') | ||
| 17 | tts.save('tts.mp3') | ||
| 18 | if output == "file": | ||
| 19 | await ctx.respond(file=discord.File('tts.mp3')) | ||
| 20 | os.remove('tts.mp3') | ||
| 21 | elif output == "vc": | ||
| 22 | # Check if user is in a voice channel | ||
| 23 | if not (ctx.author.voice and ctx.author.voice.channel): | ||
| 24 | await ctx.respond("Musisz być na kanale głosowym, aby odtworzyć TTS!", ephemeral=True) | ||
| 25 | os.remove('tts.mp3') | ||
| 26 | return | ||
| 27 | channel = ctx.author.voice.channel | ||
| 28 | # Connect to voice channel if not already connected | ||
| 29 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) | ||
| 30 | if not voice_client or not voice_client.is_connected(): | ||
| 31 | voice_client = await channel.connect() | ||
| 32 | elif voice_client.channel != channel: | ||
| 33 | await voice_client.move_to(channel) | ||
| 34 | # Play the TTS audio | ||
| 35 | if not voice_client.is_playing(): | ||
| 36 | audio_source = discord.FFmpegPCMAudio('tts.mp3', options='-filter:a "volume=6"') | ||
| 37 | voice_client.play(audio_source) | ||
| 38 | # Wait for playback to finish | ||
| 39 | while voice_client.is_playing(): | ||
| 40 | await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) | ||
| 41 | os.remove('tts.mp3') | ||
| 42 | else: | ||
| 43 | await ctx.respond("Nieznany sposób odtwarzania.", ephemeral=True) | ||
| 44 | os.remove('tts.mp3') | ||
| 45 | |||
| 46 | def setup(bot): | ||
| 47 | bot.add_cog(TTS(bot)) \ No newline at end of file | ||
