0
votes

I've been making my first discord bot recently and I've finally gotten audio to play through the bot using the code below. However, if a person in the same channel as the bot uses the same command again, the bot stops playing its audio and leaves the channel. Additionally, how would I make it so that the bot is not able to switch channels before it has finished playing its audio. (Note: the bot plays audio from urls containing mp3 files not with the YouTube plugin as I only want it to play specific things for a few private servers) Heres the code:

client.on('message', (message) => {
    if (message.content == '!play EXAMPLE') {
        var channel = message.member.voiceChannel;

        if (!channel) 
            message.channel.sendMessage ('You need to be in a voice channel to use this command.');

        if (!channel)
             return console.error("The channel does not exist!"); 

        channel.join().then(connection => {
            const dispatcher = connection.playArbitraryInput("URL TO AUDIO FILE");
            dispatcher.on("end", end => { 
                channel.leave(); 
            });
        });          
    }
});

Any help is greatly appreciated.

1
You should really format your code properly. This is hard to read, and you might find an error of your own once it's properly formatted. - Joshua M. Moore
Formatted, thanks to jtate. - Lg Cas
check if client has already joined channel - Frustrated programmer
What i think is going on... I cant verify it rn... Is that the bot is re-joining the channel(so nothing happens here) then "re-creating" dispatcher and overwriting the old dispatcher... firing dispatcher.on("end") - Frustrated programmer
I looked about on Reddit and they said to use guild.voiceConnection to detect whether or not the bot is in a channel already, this would be used as a decider on whether or not the bot would run the command(s) or not; the only thing I can't figure out is where or how to fit it into the existing code - Lg Cas

1 Answers

0
votes

I do not know if there is a method in the API to get the curent channel of the bot. However, one way to do this is to store the channel the bot is in a variable.

This simply sets a botChannel variable when joining a channel and unsets it when leaving it. So before you join the member channel, you can check if the channel is set.

I split the code to make it easier to visualize (atleast it is for me)

let botChannel = undefined;

const joinChannel = (channel) => {
    channel.join().then(connection => {
        botChannel = channel;
        playAudio(connection, 'AUDIO URL');
    });
}

const leaveChannel = () => {
    botChannel.leave();
    botChannel = undefined;
};

const playAudio = (connection, audioUrl) => {
    const dispatcher = connection.playArbitraryInput(audioUrl);
    dispatcher.on("end", end => {
        leaveChannel();
    });
};

client.on('message', (message) => {
    if (message.content == '!play EXAMPLE') {
        var channel = message.member.voiceChannel;

        if (!channel) {
            message.channel.sendMessage('You need to be in a voice channel to use this command.');
            return console.error("The channel does not exist!"); 
        }

        if (!botChannel) {
            joinChannel(channel);
        }     
    }
});