Last active
September 23, 2021 14:49
-
-
Save equalent/f356081177677d0dfdc46070b9462e15 to your computer and use it in GitHub Desktop.
Discord TTS Bot (CLI)
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 { Client, Intents } = require('discord.js'); | |
const { joinVoiceChannel, createAudioResource, createAudioPlayer, VoiceConnectionStatus, entersState, AudioPlayerStatus } = require("@discordjs/voice"); | |
const { token } = require('./config.json'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
prompt: 'TTS> ' | |
}); | |
const util = require('util'); | |
const fs = require('fs'); | |
const guildId = "xxx"; | |
const channelId = "xxx"; | |
const tts = require('@google-cloud/text-to-speech'); | |
const ttsClient = new tts.TextToSpeechClient(); | |
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES] }); | |
let gender = 'MALE'; | |
let language = 'ru-RU'; | |
client.once('ready', async () => { | |
const guild = await (await client.guilds.fetch({ id: guildId })).get(guildId).fetch(); | |
const channel = (await guild.channels.fetch(channelId)); | |
let connection = joinVoiceChannel({ | |
channelId: channel.id, | |
guildId: guild.id, | |
adapterCreator: channel.guild.voiceAdapterCreator, | |
selfDeaf: false, | |
selfMute: false, | |
debug: true | |
}); | |
connection = await entersState(connection, VoiceConnectionStatus.Ready, 10000); | |
console.log('Ready!'); | |
const player = createAudioPlayer(); | |
connection.subscribe(player); | |
rl.prompt(); | |
rl.on('line', async text => { | |
if(text.trim() == '/m') { | |
gender = 'MALE'; | |
console.log('Set gender to MALE!'); | |
} else if(text.trim() == '/f') { | |
gender = 'FEMALE'; | |
console.log('Set gender to FEMALE!'); | |
} else if(text.trim() == '/ru') { | |
language = 'ru-RU'; | |
console.log('Set language to ru-RU!'); | |
} else if(text.trim() == '/en') { | |
language = 'en-US'; | |
console.log('Set language to en-US!'); | |
} else if(text.trim() == '/ua') { | |
language = 'uk-UA'; | |
console.log('Set language to uk-UA!'); | |
} else if(text.trim() == '/enuk') { | |
language = 'en-UK'; | |
console.log('Set language to en-UK!'); | |
} else if(text.trim()[0] == '/') | |
{ | |
console.log('Unknown command!'); | |
} else { | |
console.log(`Synthesizing ${text}...`); | |
const ttsRequest = { | |
input: { text: text }, | |
voice: { languageCode: language, ssmlGender: gender }, | |
audioConfig: { audioEncoding: 'OGG_OPUS' }, | |
}; | |
const [response] = await ttsClient.synthesizeSpeech(ttsRequest); | |
const writeFile = util.promisify(fs.writeFile); | |
await writeFile('ttstmp.ogg', response.audioContent, 'binary'); | |
const resource = createAudioResource('ttstmp.ogg'); | |
player.play(resource); | |
} | |
rl.prompt(); | |
}); | |
}); | |
rl.on('SIGINT', ()=>{ | |
client.destroy(); | |
process.exit(0); | |
}); | |
client.login(token); |
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
{ | |
"token": "xxx (Discord Bot Token)" | |
} |
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
{ | |
"name": "discord-shit", | |
"version": "1.0.0", | |
"main": "main.js", | |
"license": "MIT", | |
"private": "true", | |
"dependencies": { | |
"@discordjs/opus": "^0.6.0", | |
"@discordjs/voice": "^0.6.0", | |
"@google-cloud/text-to-speech": "^3.3.1", | |
"discord.js": "^13.1.0", | |
"ffmpeg-static": "^4.4.0", | |
"libsodium-wrappers": "^0.7.9" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment