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.
channel
– Frustrated programmerdispatcher.on("end")
– Frustrated programmer