Discord.js bot joining voice channel but wont run the remaining code after joining

Tags: , , ,



I have a discord bot that I’m trying to get to join a voice channel and have it repeat a sound file, so far I have got it to join but after it joins, none of the code in the arrow function is run

let channel = client.channels.cache.get('723620872572895243')

channel.join(connection => {
    console.log("Starting")
    mp3("speech.mp3", function (err, duration) {
        if (err) return console.log(err);
        console.log("File duration:" + duration * 1000 + "ms")
        repeat(connection, duration)
    })
}).catch(console.error)

This is the code I’m trying to run but it joins the channel and nothing after the arrow function is run

Here is the repeat() function in case it is needed

function repeat(connection, duration) {
const dispatcher = connection.play("speech.mp3")
let play = setInterval(function () {
    const dispatcher = connection.play("speech.mp3")
    console.log("Playing")
}, duration * 1000 + 2000)
module.exports.interval = play
}

Answer

VoiceChannel#join takes no parameters. You haven’t formed your arrow function correctly, which is why none of your code is working, you need to have the .then() after the .join() like this:

let channel = client.channels.cache.get('723620872572895243')

channel.join().then(connection => {
    console.log("Starting")
    mp3("speech.mp3", function (err, duration) {
        if (err) return console.log(err);
        console.log("File duration:" + duration * 1000 + "ms")
        repeat(connection, duration)
    });
}).catch(console.error)

You can see more about the VoiceChannel#join method here



Source: stackoverflow