Reputation: 31
So this discord bot is meant to simply join the voice channel of whoever inputs the command, play an audio file, and leave once done.
The bot successfully joins the voice channel, begins to play the audio, and just before it finishes, it leaves. It isn't the bot leaving before the audio file is done, since it still cut off at the same point after removing the bot's ability to leave. This is my code:
const Discord = require("discord.js");
const client = new Discord.Client();
var isReady = true;
client.on("ready", () => {
console.log("I am ready!");
});
client.on("message", (message) => {
if (isReady && message.content.startsWith("!gtab")) {
isReady = false;
var voiceChannel = message.member.voiceChannel;
voiceChannel.join().then(connection => {
const dispatcher = connection.playFile('./getthatassbanned.mp3', {});
dispatcher.on("end", end => {
message.channel.send("Get that ass banned.");
voiceChannel.leave();
isReady = true;
});
}).catch(err => console.log(err));
}
});
I've been slamming my head against this issue for many hours now, and I just cannot seem to pin down why it's happening. Any help is appreciated!
Upvotes: 3
Views: 2441
Reputation: 380
The problem may just be the connection. Try putting voiceChannel.leave()
inside of a setTimeout
function.
Example:
setTimeout(function(){
voiceChannel.leave()
}, 2000)
Upvotes: 1