From 960b66a5b88e6ca162a1df09a21c281538106274 Mon Sep 17 00:00:00 2001 From: BogdanWDK Date: Wed, 30 Sep 2020 06:54:18 +0100 Subject: [PATCH 1/3] Language Support Added Supports Language as [p]tts --- tts/tts.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tts/tts.py b/tts/tts.py index 235d585..8584f5a 100644 --- a/tts/tts.py +++ b/tts/tts.py @@ -28,12 +28,12 @@ class TTS(Cog): return @commands.command(aliases=["t2s", "text2"]) - async def tts(self, ctx: commands.Context, *, text: str): - """ - Send Text to speech messages as an mp3 + async def tts(self, ctx: commands.Context, *, lang: str, text: str): """ + Send Text to speech messages as an mp3 + """ mp3_fp = io.BytesIO() - tts = gTTS(text, lang="en") + tts = gTTS(text, lang=lang) tts.write_to_fp(mp3_fp) mp3_fp.seek(0) await ctx.send(file=discord.File(mp3_fp, "text.mp3")) From 1ddcd980789c819f6a1db5ac08423376b1b5be6f Mon Sep 17 00:00:00 2001 From: bobloy Date: Thu, 15 Apr 2021 11:12:47 -0400 Subject: [PATCH 2/3] Implement languages --- tts/tts.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/tts/tts.py b/tts/tts.py index 8584f5a..fe86407 100644 --- a/tts/tts.py +++ b/tts/tts.py @@ -1,11 +1,40 @@ import io +import logging +from typing import Optional, TYPE_CHECKING import discord +import pycountry +from discord.ext.commands import BadArgument, Converter from gtts import gTTS +from gtts.lang import _fallback_deprecated_lang, tts_langs from redbot.core import Config, commands from redbot.core.bot import Red from redbot.core.commands import Cog +log = logging.getLogger("red.fox_v3.tts") + +langs = [lang.iso639_1_code for lang in pycountry.languages if hasattr(lang, "iso639_1_code")] + +print(langs) + +if TYPE_CHECKING: + ISO639Converter = str +else: + + class ISO639Converter(Converter): + async def convert(self, ctx, argument) -> str: + lang = _fallback_deprecated_lang(argument) + + try: + langs = tts_langs() + if lang not in langs: + raise BadArgument("Language not supported: %s" % lang) + except RuntimeError as e: + log.debug(str(e), exc_info=True) + log.warning(str(e)) + + return lang + class TTS(Cog): """ @@ -18,7 +47,7 @@ class TTS(Cog): self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True) default_global = {} - default_guild = {} + default_guild = {"language": "en"} self.config.register_global(**default_global) self.config.register_guild(**default_guild) @@ -27,11 +56,27 @@ class TTS(Cog): """Nothing to delete""" return + @commands.mod() + @commands.command() + async def ttslang(self, ctx: commands.Context, lang: ISO639Converter): + """ + Sets the default language for TTS in this guild. + + Default is `en` for English + """ + await self.config.guild(ctx.guild).language.set(lang) + await ctx.send(f"Default tts language set to {lang}") + @commands.command(aliases=["t2s", "text2"]) - async def tts(self, ctx: commands.Context, *, lang: str, text: str): + async def tts( + self, ctx: commands.Context, lang: Optional[ISO639Converter] = None, *, text: str + ): + """ + Send Text to speech messages as an mp3 """ - Send Text to speech messages as an mp3 - """ + if lang is None: + lang = await self.config.guild(ctx.guild).language() + mp3_fp = io.BytesIO() tts = gTTS(text, lang=lang) tts.write_to_fp(mp3_fp) From 506a79c6d6294c0353173e04f6b9c7bcb7d54bae Mon Sep 17 00:00:00 2001 From: bobloy Date: Thu, 15 Apr 2021 11:14:00 -0400 Subject: [PATCH 3/3] Removing debugging print --- tts/tts.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tts/tts.py b/tts/tts.py index fe86407..02d7267 100644 --- a/tts/tts.py +++ b/tts/tts.py @@ -15,8 +15,6 @@ log = logging.getLogger("red.fox_v3.tts") langs = [lang.iso639_1_code for lang in pycountry.languages if hasattr(lang, "iso639_1_code")] -print(langs) - if TYPE_CHECKING: ISO639Converter = str else: