Programming — Discord Bot TTS

Aaron Santa Cruz
2 min readDec 2, 2020

--

~tts hello there!

Source

Environment: Python3.7 on Raspberry Pi 3b+

To get started you need to run the following commands:

sudo pip3 install gtts && sudo pip3 install mutagen && sudo pip3 install discord.py[voice]

The first step is to import the packages you just installed.

Python Imports

The first function of the code gets the length of the mp3 file that is saved once you run the ~tts command.

Get the length of the MP3

Next, we need to create a discord bot command. I called it “tts”. Anything following the ~tts command is stored in the variable “text”. I also reference the gTTs that we will need to use in order to convert text to speech.

The command

The next step was to get the text and save it to the current directory.

Saving the tts audio

After we got that, we need to join the voice channel the user is currently in and play the file.

Playing the audio

The last step is to leave the voice chat once the audio is don playing. We do that by calling the audio_len() function and pass it the path to the file. We get back the length, and wait to disconnect until the audio is done playing.

The finished command

After that, you have yourself a fully functioning text to speech discord bot!

--

--