adding support for multiple lanagues in tts

This commit is contained in:
Luke Robles 2018-04-13 16:14:18 -07:00
parent 0289483038
commit f414c3a0b3
2 changed files with 33 additions and 11 deletions

View File

@ -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(

30
app/tts.py Normal file
View File

@ -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