This is my code:
JavaScript
x
22
22
1
const secret = require('./secret.json'); //file with your bot credentials/token/etc
2
const discord = require('discord.js');
3
const discordTTS = require('discord-tts');
4
const client = new discord.Client();
5
client.login(secret.token);
6
7
client.on('ready', () => {
8
console.log('Online');
9
});
10
11
client.on('message', msg => {
12
if(msg.content === 'say test 123'){
13
const broadcast = client.voice.createBroadcast();
14
const channelId = msg.member.voice.channelID;
15
const channel = client.channels.cache.get(channelId);
16
channel.join().then(connection => {
17
broadcast.play(discordTTS.getVoiceStream('test 123'));
18
const dispatcher = connection.play(broadcast);
19
});
20
}
21
});
22
Output comes out with an error:
JavaScript
1
2
1
TypeError: client.voice.createBroadcast is not a function
2
I am using Node:17.0.0 and Discord.js:13.1.0
I am not sure why I am getting this error.
Advertisement
Answer
Discord.js v13 no longer supports voice. The new way to join a VC and play audio is with the @discordjs/voice library.
JavaScript
1
9
1
const { joinVoiceChannel, createAudioPlayer } = require("@discordjs/voice")
2
const player = createAudioPlayer()
3
joinVoiceChannel({
4
channelId: msg.member.voice.channel.id,
5
guildId: msg.guild.id,
6
adapterCreator: msg.guild.voiceAdapterCreator
7
}).subscribe(player) //join VC and subscribe to the audio player
8
player.play(audioResource) //make sure "audioResource" is a valid audio resource
9
Unfortunately, discord-tts
might be deprecated. You can record your own user client speaking the message and save it to an audio file. Then you can create an audio resource like this:
JavaScript
1
3
1
const { createAudioResource } = require("@discordjs/voice")
2
const audioResource = createAudioResource("path/to/local/file.mp3")
3