I have created a discord bot that follows users around vc's and joins them, how do I disconnect the audio stream when they leave? Code is as follows:
client.on('voiceStateUpdate', (oldMember, newMember) => {
console.log(newMember.channelID)
let newUserChannel = newMember.channelID;
const channel = client.channels.cache.get(newUserChannel);
channel.join().then(connection => {
const dispatcher = connection.play('audio.mp3',{ volume: 1 });
dispatcher.on('start', () => {
console.log('audio.mp3 is now playing!');
});
dispatcher.on('finish', () => {
console.log('audio.mp3 has finished playing!');
connection.disconnect();
});
// Always remember to handle errors appropriately!
}).catch(e => {
console.error(e);
});
}
);
Just add an if statement to your code, which checks if there is still one Discord user in the channel (except the bot). If not, disconnect:
if (oldMember.channel.members.size === 1) {
connection.disconnect();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.