Home > front end >  TypeError: client.voice.createBroadcast is not a function
TypeError: client.voice.createBroadcast is not a function

Time:10-25

This is my code:

const secret = require('./secret.json'); //file with your bot credentials/token/etc
const discord = require('discord.js');
const discordTTS = require('discord-tts');
const client = new discord.Client();
client.login(secret.token);

client.on('ready', () => {
    console.log('Online');
});

client.on('message', msg => {
    if(msg.content === 'say test 123'){
        const broadcast = client.voice.createBroadcast();
        const channelId = msg.member.voice.channelID;
        const channel = client.channels.cache.get(channelId);
        channel.join().then(connection => {
            broadcast.play(discordTTS.getVoiceStream('test 123'));
            const dispatcher = connection.play(broadcast);
        });
    }
});

Output comes out with an error:

TypeError: client.voice.createBroadcast is not a function

I am using Node:17.0.0 and Discord.js:13.1.0

I am not sure why I am getting this error.

CodePudding user response:

Discord.js v13 no longer supports voice. The new way to join a VC and play audio is with the @discordjs/voice library.

const { joinVoiceChannel, createAudioPlayer } = require("@discordjs/voice")
const player = createAudioPlayer()
joinVoiceChannel({
  channelId: msg.member.voice.channel.id,
  guildId: msg.guild.id,
  adapterCreator: msg.guild.voiceAdapterCreator
}).subscribe(player) //join VC and subscribe to the audio player
player.play(audioResource) //make sure "audioResource" is a valid audio resource

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:

const { createAudioResource } = require("@discordjs/voice")
const audioResource = createAudioResource("path/to/local/file.mp3")
  • Related