Created
April 2, 2020 01:33
-
-
Save Apexal/2dc237eb0f01619023ed3e2eabf17108 to your computer and use it in GitHub Desktop.
Discord bot speech synthesis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Discord = require('discord.js'); | |
const client = new Discord.Client(); | |
const textToSpeech = require('@google-cloud/text-to-speech'); | |
const fs = require('fs'); | |
const util = require('util'); | |
const tts = new textToSpeech.TextToSpeechClient(); | |
const writeFile = util.promisify(fs.writeFile); | |
client.on('message', async message => { | |
if (!message.guild) return; | |
if (message.content.startsWith('.say ')) { | |
const text = message.content.replace('.say ', '') | |
// Only try to join the sender's voice channel if they are in one themselves | |
if (message.member.voice.channel) { | |
const connection = await message.member.voice.channel.join(); | |
const request = { | |
input: { text }, | |
voice: { languageCode: 'en-US', ssmlGender: 'NEUTRAL' }, | |
audioConfig: { audioEncoding: 'MP3' }, | |
}; | |
const [response] = await tts.synthesizeSpeech(request); | |
await writeFile('output.mp3', response.audioContent, 'binary'); | |
connection.play('output.mp3'); | |
} else { | |
message.reply('You need to join a voice channel first!'); | |
} | |
} | |
}); | |
client.login('secret token from discord here'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment