I am currently making a Discord bot that assigns a role when you enter a specific voice channel and removes it when you leave. Here is the code:
client.on('voiceStateUpdate', (oldState, newState) => {
const testChannel = newState.guild.channels.cache.find(c => c.name === ' 1h de travail');
const role = newState.guild.roles.cache.find(r => r.name === 'test');
if (newState.channelID === testChannel.id) {
// Triggered when the user joined the channel we tested for
if (!newState.member.roles.cache.has(role))
newState.member.roles.add(role);
// Add the role to the user if they don't already have it
}
else {
console.log('detected');
if (oldState.voiceChannel !== undefined && newState.voiceChannel === undefined)
oldState.member.roles.remove(role);
}
});
My issue is that it doesn't actually remove the role. How can I make it do so?