I'm working on a discord.js v13 bot that joins a voice channel then listen for user then transfer the voice to text then transfer the text to voice then send it back to the voice channel
When I join the voice channel with the bot the bot doesn't send voice I think it's a problem with my code so can someone solve it or tell me the problem in the code
Here's the code :
const { Client, Intents } = require('discord.js');
const { VoiceConnectionStatus, createAudioPlayer, createAudioResource, joinVoiceChannel } = require('@discordjs/voice');
const { getAudioUrl } = require('google-tts-api');
const Transcriber = require("discord-speech-to-text");
const transcriber = new Transcriber("api key");
const DiscordTTS = require("discord-tts");
const axios = require('axios');
//const AssemblyAI = require('assemblyai_nodejs');
const client = new Client({
intents:[
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_VOICE_STATES
]
});
//const assemblyClient = new AssemblyAI("49457b83b09243b494eccd");
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith('!listen')) return;
// Join the voice channel of the user who sent the command
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.reply('You need to join a voice channel first!');
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false,
});
const audioStream = connection.receiver.subscribe(message.author.id, { mode: 'pcm' });
const audioChunks = [];
audioStream.on('data', (chunk) => {
audioChunks.push(chunk);
});
audioStream.on('end', async () => {
const audioBuffer = Buffer.concat(audioChunks);
try {
// Send audio buffer to AssemblyAI for transcription
transcriber.listen(audioBuffer).then((data) => {
if (!data.transcript.text) return;
let texter = data.transcript.text;
let userer = data.user;
})
const transcript = response.text;
// Translate text to desired language using Google Translate API
const translationResponse = await axios.get(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=fr&dt=t&q=${encodeURI(texter)}`);
const translatedText = translationResponse.data[0][0][0];
// Convert translated text to speech using AssemblyAI
const audioURL = getAudioUrl(texter, {
lang: 'en',
slow: false,
host: 'https://translate.google.com',
timeout: 10000,
});
const resource = DiscordTTS.getVoiceStream("Hello world!");
// Create audio player and resource
const player = createAudioPlayer();
// const resource = createAudioResource(audioURL);
player.play(resource);
// Play the speech in the voice channel
connection.subscribe(player);
//player.on(VoiceConnectionStatus.Destroyed, () => {
// connection.destroy();
// });
} catch (error) {
console.error(error);
}
// });
// Stop audio stream after 5 seconds
// setTimeout(() => {
// audioStream.destroy();
// }, 5000);
})});
client.login('token');