I am having a tough time understanding the discord.js documentation. I am looking to have my bot disconnect a user based on their userID.
My code would ideally look something like this:
function disconnectUser(userID){
userID.disconnect();
return;
}
The code I have tried is varitions of:
function disconnectUser(userID){
userID.VoiceState.disconnect();
return;
}
And I get the error:
TypeError: Cannot read properties of undefined (reading 'disconnect')
Any help would be greatly appreciated :)
You simply need to fetch the member and the call the voiceState#disconnect() method on them like so:
async function disconnect(userID){
let user = await client.users.fetch(userID)
user.voice.disconnect();
}
For calling it you simply need to pass a snowflake ( user ID) as the function parameter like so:
disconnect(1234567890123456);
The voiceState#disconnect() method was called voiceState#kick() in v12 therefore you'd have to make the suitable changes if on v12.
Like so:
async function disconnect(userID){
let user = await client.users.fetch(userID)
user.voice.kick();
}
Voice is not an attribute of "users", it is only a member of "guild.members". "guild.members" is created when a user joins a VC and is accessed via a message or an interaction. In my case using slash commands I got the guild by passing in the interaction to the disconnectUser() function. The working disconnect function works as below:
async function disconnectUser(userID, interaction){
let user = await interaction.guild.members.fetch(userID);
user.voice.disconnect();
}
Where interaction is essentially the same as message.