VoiceState channelId is null

When leaving a voice channel, both the oldState.channelId and newState.channelId are null. djs v14.3.0, node 18.8.0 Bot has all the required intents enabled, and works fine when joining a channel (join to create voice bot). I also noticed that when connecting to a channel, both the old and newState have the same channelId
2 Replies
d.js docs
d.js docs2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
treble/luna
treble/luna2y ago
full code:
const { ChannelType } = require("discord.js");

module.exports = {
name: "voiceStateUpdate",
async execute(oldState, newState){
const channel = newState.channelId;
const {client, member} = newState;
if(channel != null && channel === "1008070945937838090"){
const chan = await newState.guild.channels.create({
name: `${newState.member.displayName.slice(0,15)}'s Channel`,
type: ChannelType.GuildVoice,
parent: "825397075763134466",
/* permissionOverwrites: [
{
id: newState.member.id,
allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.Connect, PermissionFlagsBits.Speak]
},
{
id: "832927620542627860",
deny: [PermissionFlagsBits.ViewChannel]
},
{
id: newState.guild.roles.everyone,
deny: [PermissionFlagsBits.ViewChannel]
},
],*/
})
newState.member.voice.setChannel(chan);
newState.client.voiceChannels.set(member.id,chan.id)
return;
}
console.log(oldState.member.voice.channel.id) //null when leaving
console.log(newState.member.voice.channel.id) // null when leaving
if(!member.voice.channel && client.voiceChannels.has(member.id) && oldState.member.voice.channel.id === client.voiceChannels.get(member.id)){
if(oldState.channelId === client.voiceChannels.get(member.id) && newState.channelId != client.voiceChannels.get(member.id)){
member.voice.channel.delete();
}
}
}
}
const { ChannelType } = require("discord.js");

module.exports = {
name: "voiceStateUpdate",
async execute(oldState, newState){
const channel = newState.channelId;
const {client, member} = newState;
if(channel != null && channel === "1008070945937838090"){
const chan = await newState.guild.channels.create({
name: `${newState.member.displayName.slice(0,15)}'s Channel`,
type: ChannelType.GuildVoice,
parent: "825397075763134466",
/* permissionOverwrites: [
{
id: newState.member.id,
allow: [PermissionFlagsBits.ViewChannel, PermissionFlagsBits.Connect, PermissionFlagsBits.Speak]
},
{
id: "832927620542627860",
deny: [PermissionFlagsBits.ViewChannel]
},
{
id: newState.guild.roles.everyone,
deny: [PermissionFlagsBits.ViewChannel]
},
],*/
})
newState.member.voice.setChannel(chan);
newState.client.voiceChannels.set(member.id,chan.id)
return;
}
console.log(oldState.member.voice.channel.id) //null when leaving
console.log(newState.member.voice.channel.id) // null when leaving
if(!member.voice.channel && client.voiceChannels.has(member.id) && oldState.member.voice.channel.id === client.voiceChannels.get(member.id)){
if(oldState.channelId === client.voiceChannels.get(member.id) && newState.channelId != client.voiceChannels.get(member.id)){
member.voice.channel.delete();
}
}
}
}
no, the event fires only one when joining a voice channel. It does fire again once it moves to the channel which was created when doing that, it does indeed work (maybe i made a typing error somewhere before). But, it keeps throwing an error at the member.voice.channel.id, because 'voice' apparently is null in both cases ahh that explains. i honestly dont know and just realised i can do it via the channelId instead. i tried it because initially the channelId property didnt work (probably misspelled) and i tried it via there. (my issues can be very stupid/simple sometimes but i only realise that afterwards)