discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.47k stars 3.97k forks source link

voiceStateUpdate not updating to latest <NewMember>.voiceChannelID #2021

Closed reecebenson closed 7 years ago

reecebenson commented 7 years ago

Please describe the problem you are having in as much detail as possible: When moving users from channel to channel and omitting the voiceStateUpdate event, the <NewMember> variable doesn't actually grab the latest voiceChannelID of the user.

To demonstrate this a little further, imagine I have two voice channels:

I have my bot (or a user) join the voice channel Music. The log would be shown like so (in accordance to the reproducible code sample below):

newMbr: undefined
guildMbr: 67890

As you can see by the log, the newMbr variable doesn't have a voice channel set whereas the guildMbr variable does. When moving the joined user from Music to General, the log shows like so:

newMbr: 67890
guildMbr: 12345

Include a reproducible code sample here, if possible:

<Client>.on('voiceStateUpdate' (oldMbr, newMbr) => {
    console.log("newMbr: " + newMbr.voiceChannelID);

    var guild = newMbr.guild;
    console.log("guildMbr: " + guild.members.get(newMbr.id).voiceChannel.id);
});

Workaround (temporary fix)

<Client>.on('voiceStateUpdate' (oldMbr, newMbr) => {
    newMbr = (newMbr.guild.members.get(newMbr.id));
    // do stuff
    console.log(newMbr.voiceChannel.id); // correct updated version
});

Further details:

aemino commented 7 years ago

The event fires before actually updating the member in the guild member cache. The output you posted earlier is as expected. In the first scenario, the guild member joins a voice channel; thus the old member has no voice channel ID. The second scenario also makes sense; the member is switching from channel to channel. I have no idea why in your code you're fetching the guild member from the cache when you already have access to the guild member object.

reecebenson commented 7 years ago

You state that in the first scenario, the guild member joins the channel thus the old member having no voice channel ID but I'm clearly stating that I am not using the old member variable at all. The <NewMember> variable should hold the members current and correct voice channel ID whereas <OldMember> should hold the previous values.

Joining a Voice Channel: <OldMember>.voiceChannelID; // undefined (expected) <NewMember>.voiceChannelID; // undefined (previous & incorrect) <NewMember>.guild.members.get(<NewMember>.id).voiceChannel.id; // 12345 (current & correct)

However both variables old and new remain undefined until the next voice state update - and those values are then outdated.

vladfrangu commented 7 years ago

Hey. I just tested this on latest master (Actual Commit hash 97823bc3761bea0bfba34fc2f8b85765084d9d0d) and it seems to work as intended. Try updating Discord.js and see if you can still reproduce it

reecebenson commented 7 years ago

Can confirm on latest commit, values work as intended. Thank you @KingDGrizzle :)