TTS

i want to create a simple discord bot, that will read user messages in specific channel and bot will use TTS in voice channel to read user message. so basically if user joins - bot joins also and should play TTS with the message. but it doesnt work for me - bot joins after sending a message but its not playing TTS. any ideas? Code:
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, VoiceConnectionStatus } = require("@discordjs/voice");
const { Client, GatewayIntentBits } = require('discord.js');
const googleTTS = require('google-tts-api');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const token = 'yea';

const audioPlayer = createAudioPlayer();
let voiceConnection = null;

client.on('messageCreate', async (message) => {
if (message.channelId === '1168302709024239656') {
const userVoiceChannel = message.member.voice.channel;
if (userVoiceChannel) {
if (voiceConnection) {
voiceConnection.destroy();
}
voiceConnection = joinVoiceChannel({
channelId: userVoiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});

voiceConnection.on(VoiceConnectionStatus.Ready, async () => {
const text = message.content;
const audioURL = googleTTS.getAudioUrl(text, {
lang: 'pl',
slow: false,
});

const audioResource = createAudioResource(audioURL, {
inputType: StreamType.Arbitrary,
});

audioPlayer.play(audioResource);
});

audioPlayer.on('end', () => {
if (voiceConnection) {
voiceConnection.destroy();
}
});
}
}
});

client.login(token);
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, VoiceConnectionStatus } = require("@discordjs/voice");
const { Client, GatewayIntentBits } = require('discord.js');
const googleTTS = require('google-tts-api');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const token = 'yea';

const audioPlayer = createAudioPlayer();
let voiceConnection = null;

client.on('messageCreate', async (message) => {
if (message.channelId === '1168302709024239656') {
const userVoiceChannel = message.member.voice.channel;
if (userVoiceChannel) {
if (voiceConnection) {
voiceConnection.destroy();
}
voiceConnection = joinVoiceChannel({
channelId: userVoiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});

voiceConnection.on(VoiceConnectionStatus.Ready, async () => {
const text = message.content;
const audioURL = googleTTS.getAudioUrl(text, {
lang: 'pl',
slow: false,
});

const audioResource = createAudioResource(audioURL, {
inputType: StreamType.Arbitrary,
});

audioPlayer.play(audioResource);
});

audioPlayer.on('end', () => {
if (voiceConnection) {
voiceConnection.destroy();
}
});
}
}
});

client.login(token);
1 Reply
d.js toolkit
d.js toolkit13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Want results from more Discord servers?
Add your server