Roles not adding correctly

// Member is a GuildMember, passed from the interaction's interaction.member. i.e.:
const member = interaction.member;
// Example of rolesToAdd:
const rolesToAdd = new Map([
['server id #1', new Set(['role id #1', ...])],
['server id #2', new Set(['role id #1', ...])],
]);
for await (const [guildId, roleIds] of Object.entries(rolesToAdd)) {
if (!roleIds.size) {
container.logger.debug(`Skipped adding roles to user with id ${member.id} in guild with id ${guildId} as they already have them`);
continue;
}
const guild = await container.client.guilds.fetch(guildId);
if (!guild) {
container.logger.warn(`Couldn't add roles to a user while syncing: guild with id ${guildId} not found.`);
continue;
}
const guildMember = await guild.members.fetch(member.id);
if (!guildMember) {
container.logger.warn(`Couldn't add roles to a user while syncing: user with id ${member.id} not found in guild with id ${guildId}.`);
continue;
}
await guildMember.roles.add([...roleIds], `[Sync] Adding staff role`)
.catch((error) => {
container.logger.warn(`Failed to add roles to user with id ${member.id} in guild with id ${guildId}:`, error);
});
}
// Member is a GuildMember, passed from the interaction's interaction.member. i.e.:
const member = interaction.member;
// Example of rolesToAdd:
const rolesToAdd = new Map([
['server id #1', new Set(['role id #1', ...])],
['server id #2', new Set(['role id #1', ...])],
]);
for await (const [guildId, roleIds] of Object.entries(rolesToAdd)) {
if (!roleIds.size) {
container.logger.debug(`Skipped adding roles to user with id ${member.id} in guild with id ${guildId} as they already have them`);
continue;
}
const guild = await container.client.guilds.fetch(guildId);
if (!guild) {
container.logger.warn(`Couldn't add roles to a user while syncing: guild with id ${guildId} not found.`);
continue;
}
const guildMember = await guild.members.fetch(member.id);
if (!guildMember) {
container.logger.warn(`Couldn't add roles to a user while syncing: user with id ${member.id} not found in guild with id ${guildId}.`);
continue;
}
await guildMember.roles.add([...roleIds], `[Sync] Adding staff role`)
.catch((error) => {
container.logger.warn(`Failed to add roles to user with id ${member.id} in guild with id ${guildId}:`, error);
});
}
Hi, I have this strange problem. If I run the command /sync in server #1, the roles are added correctly in server #1, however they are not added to server #2. The converse is also true. Could anyone help me out? No errors. Using discord.js 14.15.3-dev.1715429013-7816ec2e6.
2 Replies
d.js toolkit
d.js toolkit6mo 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! - Marked as resolved by staff
☆♡ Nova Zin ☆♡
Did you mean to make two separate posts about the same thing?
Want results from more Discord servers?
Add your server