Is there a way to get a log of added or removed roles for a member?

I have used this code for a long time to achieve this until suddenly I encountered an error when a member is not in the cache. When the guildMemberUpdate event occurs, it logs that all roles the member already had have been added again.
export default async (client, oldMember, newMember) => {
// Get roles of the oldMember from the cache (not ideal but necessary if no other option)
const oldRolesIds = oldMember.roles.cache.map((role) => role.id);

// Get roles of the newMember from the cache (not ideal but necessary if no other option)
const newRolesIds = newMember.roles.cache.map((role) => role.id);

// Compare the length of old and new roles
if (oldRolesIds.length !== newRolesIds.length) {
// Log the differences in roles if there is a change
await sendLogs(client, `\`\`\`\nOld roles: [ ${oldRolesIds.join(", ")} ]\nNew roles: [ ${newRolesIds.join(", ")} ]\n\`\`\``);
}
} catch (error) {
// Log any errors that occur during processing
console.error(`Error processing guildMemberUpdate: ${error}`);
}
};
export default async (client, oldMember, newMember) => {
// Get roles of the oldMember from the cache (not ideal but necessary if no other option)
const oldRolesIds = oldMember.roles.cache.map((role) => role.id);

// Get roles of the newMember from the cache (not ideal but necessary if no other option)
const newRolesIds = newMember.roles.cache.map((role) => role.id);

// Compare the length of old and new roles
if (oldRolesIds.length !== newRolesIds.length) {
// Log the differences in roles if there is a change
await sendLogs(client, `\`\`\`\nOld roles: [ ${oldRolesIds.join(", ")} ]\nNew roles: [ ${newRolesIds.join(", ")} ]\n\`\`\``);
}
} catch (error) {
// Log any errors that occur during processing
console.error(`Error processing guildMemberUpdate: ${error}`);
}
};
Here is an example of the logs with this code, showing this issue:
Old roles: [ 418586876899885056 ]
New roles: [ 807991856868229200, 807994083204792380, 807991313542676520, 874292095840563280, 807991901932224552, 873953249055621172, 807991887055028266, 418586876899885056 ]
Old roles: [ 418586876899885056 ]
New roles: [ 807991856868229200, 807994083204792380, 807991313542676520, 874292095840563280, 807991901932224552, 873953249055621172, 807991887055028266, 418586876899885056 ]
where 418586876899885056 is the ID of the server (guild).
5 Replies
d.js toolkit
d.js toolkit•3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
🤖 RoboticRobot 🤖
You can fetch it from audit log
d.js docs
d.js docs•3mo ago
documentation suggestion for @XtoManuel: :method: Guild#fetchAuditLogs() @14.15.3 Fetches audit logs for this guild.
// Output audit log entries
guild.fetchAuditLogs()
.then(audit => console.log(audit.entries.first()))
.catch(console.error);
// Output audit log entries
guild.fetchAuditLogs()
.then(audit => console.log(audit.entries.first()))
.catch(console.error);
XtoManuel
XtoManuelOP•3mo ago
Discord.js version: 14.14.1 I have tried using Audit Logs, but the roles I want to log are primarily roles that are assigned automatically by Discord, such as the server Booster role, and roles from streaming or video platform subscriptions (Twitch/YouTube). These roles are not recorded in the Audit Logs.
d.js docs
d.js docs•3mo ago
:method: Guild#fetchAuditLogs() @14.14.1 Fetches audit logs for this guild.
// Output audit log entries
guild.fetchAuditLogs()
.then(audit => console.log(audit.entries.first()))
.catch(console.error);
// Output audit log entries
guild.fetchAuditLogs()
.then(audit => console.log(audit.entries.first()))
.catch(console.error);
Want results from more Discord servers?
Add your server