Problem with GuildMemberUpdate

For some reason, it happens that I add the highest (or no) role and it shows that all the roles that the participant already has have been added. How can this be fixed?
const oldRoles = Array.from(oldMember.roles.cache.values())
.filter(role => role.guild.id === oldMember.guild.id);

const newRoles = Array.from(newMember.roles.cache.values())
.filter(role => role.guild.id === newMember.guild.id);

const addedRoles = newRoles.filter(role => !oldRoles.some(oldRole => oldRole.id === role.id));
const removedRoles = oldRoles.filter(role => !newRoles.some(newRole => newRole.id === role.id));

...
if (addedRoles.length > 0) {
embedGuildMemberUpdate.fields.push({
name: 'Добавлены роли',
value: addedRoles.map(role => `${role.name} (${roleMention(role.id)})`).join(' '),
inline: addedRoles.length <= 4,
})
}

if (removedRoles.length > 0) {
embedGuildMemberUpdate.fields.push({
name: 'Сняты роли',
value: removedRoles.map(role => `${role.name} (${roleMention(role.id)})`).join(' '),
inline: removedRoles.length <= 4,
})
}
const oldRoles = Array.from(oldMember.roles.cache.values())
.filter(role => role.guild.id === oldMember.guild.id);

const newRoles = Array.from(newMember.roles.cache.values())
.filter(role => role.guild.id === newMember.guild.id);

const addedRoles = newRoles.filter(role => !oldRoles.some(oldRole => oldRole.id === role.id));
const removedRoles = oldRoles.filter(role => !newRoles.some(newRole => newRole.id === role.id));

...
if (addedRoles.length > 0) {
embedGuildMemberUpdate.fields.push({
name: 'Добавлены роли',
value: addedRoles.map(role => `${role.name} (${roleMention(role.id)})`).join(' '),
inline: addedRoles.length <= 4,
})
}

if (removedRoles.length > 0) {
embedGuildMemberUpdate.fields.push({
name: 'Сняты роли',
value: removedRoles.map(role => `${role.name} (${roleMention(role.id)})`).join(' '),
inline: removedRoles.length <= 4,
})
}
8 Replies
Syjalo
Syjalo13mo ago
Just use
const addedRoles = newMember.roles.cache.subtract(oldMember.roles.cache);
const removedRoles = oldMember.roles.cache.subtract(newMember.roles.cache);
const addedRoles = newMember.roles.cache.subtract(oldMember.roles.cache);
const removedRoles = oldMember.roles.cache.subtract(newMember.roles.cache);
Btw you don't need to convert a collection to an array. Collection has methods of array, and even more. If you have GuildMember partial the oldMember may be a partial structure and may not has roles.
d.js docs
d.js docs13mo ago
guide Popular Topics: Partial Structures read more
vlad vollar
vlad vollar13mo ago
I am now displaying the value of addedRoles in the console and 3 roles are initially displayed and then 1.
Syjalo
Syjalo13mo ago
Do you have GuildMember partial in your client options?
vlad vollar
vlad vollar13mo ago
Syjalo
Syjalo13mo ago
As you can read the guide you can't expect other properties to exist besides the id in partial structures. The guild member should be cached to not be partial in events, but I don't think you want to cache every single member.
vlad vollar
vlad vollar13mo ago
What do you suggest I do?
Syjalo
Syjalo13mo ago
Ignore partial members or fetch all members