Please describe the problem you are having in as much detail as possible:
In my bot's only guild with 500 members, random Presences throw an error on the Presence.member getter.
My bot keeps throwing the following error:
TypeError: Cannot read property 'get' of undefined
at Presence.get member [as member] (/app/node_modules/discord.js/src/structures/Presence.js:35:31)
at Client.dClient.on (/app/src/colorroles.js:90:32)
at emitTwo (events.js:131:20)
at Client.emit (events.js:214:7)
at PresenceUpdateHandler.handle (/app/node_modules/discord.js/src/client/websocket/packets/handlers/PresenceUpdate.js:44:16)
at WebSocketPacketManager.handle (/app/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:97:65)
at WebSocketConnection.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:322:35)
at WebSocketConnection.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:285:10)
at WebSocket.onMessage (/app/node_modules/discord.js/node_modules/ws/lib/event-target.js:120:16)
at emitOne (events.js:116:13)
and this is the code described at the second call from the top:
dClient.on('presenceUpdate', async (oldPresence, newPresence) => {
colorRoles.update(newPresence.member); // this line
});
Upon further investigation I found that some of the guild's Presences throw the error, whether it's from each GuildMember.presence of Guild.members, fromGuild.presences, or oldPresence/newPresence from presenceUpdate event.
I tested all the guild's members' presences by evaluating this code:
let foo = new String();
message.guild.members.forEach(member => {
try {
foo += `${member.displayName} ${member.presence.member}\n`;
} catch(e) {
foo += `${member.displayName} ${e.message}\n`;
}
});
message.channel.send(foo, {split:true});
This produced a list of members and their .presence.member or the error. Some random members clustered at the top end were valid—I counted 66 (out of 500)—while all the others threw Cannot read property 'get' of undefined
I also tested all of the guild's .presences with this code:
let foo = new String();
message.guild.presences.forEach(presence => {
try {
foo += `${presence.member.displayName}\n`;
} catch(e) {
foo += `${e.message}\n`;
}
});
message.channel.send(foo, {split:true})
This also produced a list showing 66 valid presence.members (all at the top) followed by 71 errored presence.members.
But here's the strange thing. I tried to reproduce it with a separate bot in the same guild:
bot = new (require('discord.js')).Client({
//fetchAllMembers: true
});
bot.login( 'token');
setTimeout(()=>{
bot.guilds.first().members.forEach(member => {
try {
console.log(member.displayName, member.presence.member);
} catch(e) {
console.error(member.displayName, e.message);
}
});
}, 1000*60*5);
And this does _not_ throw errors. Instead, member.presence.member is just _always_ undefined.
I've tried different variations of this, including running the code once 'ready', running the code 5 minutes after ready, with and without fetchAllMembers: true. And before I tested it with presenceUpdate event because that was where the original issue was. Presence.member.presence is just _always_ undefined.
I looked at the code and determined that the error is caused when the Presence.guild is undefined. But that's as far as I went.
I'm puzzled by this; It appears to be a Discord.js bug that I cannot get around, but when I reproduce it, I get a different problem. 😕
Further details:
github:hydrabolt/discord.js#7864653df726ab08e2f71313b69a8e5dd0b6d7e1v8.11.3Linux 1824621d-b4f0-4442-850e-c34e1a941184 4.4.0-1025-aws #26-Ubuntu SMP Fri Jul 13 08:33:50 UTC 2018 x86_64 x86_64 x86_64 GNU/LinuxI have observed this as well. upon inspecting the presence object for issue presences . Member is not a GuildMember but a TypeError object
Can I just confirm that you're both not using user bots?
@amishshah correct, not a user bot.
Can either of you try to reproduce your bug with that pr?
@amishshah Not a user bot
@bdistin Looks like it's working! 😄 The test script returns the member instead of undefined. I will deploy with your branch and see if it fixes the TypeError.
Edit: It works!! No more errors! 😄
looks like this fixes it