From f414c3a0b38d70565dc4f8df9150989154af5ebf Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Fri, 13 Apr 2018 16:14:18 -0700 Subject: [PATCH] adding support for multiple lanagues in tts --- app/dragon-bot.py | 14 +++----------- app/tts.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 app/tts.py diff --git a/app/dragon-bot.py b/app/dragon-bot.py index 0140c03b..9b2a2ade 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -7,7 +7,6 @@ then imported into the main bot import os import requests -from gtts import gTTS import core_utils import decide @@ -24,6 +23,7 @@ import questions import role_check import set_avatar import stock +import tts import wallpaper import weather @@ -313,19 +313,11 @@ async def on_message(message): ) if message.content.startswith('!tts'): - myobj = gTTS( - text=' '.join(message.content.split()[1:]), - lang='en', - slow=False - ) - file_path = '/tmp/memes.mp3' - myobj.save(file_path) - await client.send_file( message.channel, - file_path, + tts.text_to_speech(message.content), ) - os.remove(file_path) + os.remove('/tmp/memes.mp3') if message.content.startswith('!wallpaper'): await client.send_message( diff --git a/app/tts.py b/app/tts.py new file mode 100644 index 00000000..c8e4e821 --- /dev/null +++ b/app/tts.py @@ -0,0 +1,30 @@ +from gtts import gTTS + +def text_to_speech(input): + """ + text_to_speech(input) + receives the message from discord and returns an MP3 of it. + If you pass in a language after a semicolon, it will return it read in + that language. eg: what are you doing; ja, will return a file read by a japanese + robot + + List of supported languages here: + https://github.com/pndurette/gTTS#supported-languages + """ + file_path = '/tmp/memes.mp3' + message = ' '.join(input.split()[1:]) + + language = 'en' + # check for another language denoted at the end of the message + if ';' in message: + print(message.split(';')) + language = message.split(';')[1].strip() + message = message.split(';')[0] + + gTTS( + text=message, + lang=language, + slow=False + ).save(file_path) + + return file_path