Problem with found user in specific guild

I cant find and add role user in specific guild. Here is my code:
const main_guild = interaction.client.guilds.cache.get(main_guild_id);
const clubs_guild = interaction.client.guilds.cache.get(clubs_guild_id);

if (!main_guild || !clubs_guild) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć gildii!", ephemeral: true });

const main_guild_member = main_guild.members.cache.find(member => member.id === interaction.user.id);
const clubs_guild_member = clubs_guild.members.cache.find(member => member.id === interaction.user.id);

if (!main_guild_member || !clubs_guild_member) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć użytkownika!", ephemeral: true });

const main_guild_role = main_guild.roles.cache.find(role => role.name === "• " + club_name + " | Kluby");
const club_guild_role = clubs_guild.roles.cache.find(role => role.name === club_name + " | Kluby");
const club_guild_rank_role = clubs_guild.roles.cache.find(role => role.name === "✧ " + club_rank + " | IFL");

if (!main_guild_role || !club_guild_role || !club_guild_rank_role) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć roli!", ephemeral: true });

main_guild_member.roles.add(main_guild_role);
clubs_guild_member.roles.add(club_guild_role);
clubs_guild_member.roles.add(club_guild_rank_role);
const main_guild = interaction.client.guilds.cache.get(main_guild_id);
const clubs_guild = interaction.client.guilds.cache.get(clubs_guild_id);

if (!main_guild || !clubs_guild) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć gildii!", ephemeral: true });

const main_guild_member = main_guild.members.cache.find(member => member.id === interaction.user.id);
const clubs_guild_member = clubs_guild.members.cache.find(member => member.id === interaction.user.id);

if (!main_guild_member || !clubs_guild_member) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć użytkownika!", ephemeral: true });

const main_guild_role = main_guild.roles.cache.find(role => role.name === "• " + club_name + " | Kluby");
const club_guild_role = clubs_guild.roles.cache.find(role => role.name === club_name + " | Kluby");
const club_guild_rank_role = clubs_guild.roles.cache.find(role => role.name === "✧ " + club_rank + " | IFL");

if (!main_guild_role || !club_guild_role || !club_guild_rank_role) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć roli!", ephemeral: true });

main_guild_member.roles.add(main_guild_role);
clubs_guild_member.roles.add(club_guild_role);
clubs_guild_member.roles.add(club_guild_rank_role);
19 Replies
d.js toolkit
d.js toolkit4mo 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 OP
mallusrgreat
mallusrgreat4mo ago
error?
n4a
n4a4mo ago
no errors only returning intreraction message cant find user in specific guild guilds id is right and user is in both guilds
mallusrgreat
mallusrgreat4mo ago
idk which message says that
n4a
n4a4mo ago
(!main_guild_member || !clubs_guild_member) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć użytkownika!", ephemeral: true });
(!main_guild_member || !clubs_guild_member) return interaction.reply({ content: "\`[ ❌ ]\` Nie udało się znaleźć użytkownika!", ephemeral: true });
Kevinnnn
Kevinnnn4mo ago
The cache will often not be populated with all the members, which is why fetching the member in that specific guild is better
n4a
n4a4mo ago
i changed this and now i have error cant add role
TypeError: Cannot read properties of undefined (reading 'add')
TypeError: Cannot read properties of undefined (reading 'add')
Kevinnnn
Kevinnnn4mo ago
what did you change?
n4a
n4a4mo ago
cache to fetch
mallusrgreat
mallusrgreat4mo ago
which line
n4a
n4a4mo ago
main_guild_member.roles.add(main_guild_role);
clubs_guild_member.roles.add(club_guild_role);
clubs_guild_member.roles.add(club_guild_rank_role);
main_guild_member.roles.add(main_guild_role);
clubs_guild_member.roles.add(club_guild_role);
clubs_guild_member.roles.add(club_guild_rank_role);
mallusrgreat
mallusrgreat4mo ago
all of them? they give the same error?
Kevinnnn
Kevinnnn4mo ago
what did you change? can you send the updated code?
n4a
n4a4mo ago
yes
const main_guild_member = main_guild.members.fetch(member => member.id === interaction.user.id);
const clubs_guild_member = clubs_guild.members.fetch(member => member.id === interaction.user.id);
const main_guild_member = main_guild.members.fetch(member => member.id === interaction.user.id);
const clubs_guild_member = clubs_guild.members.fetch(member => member.id === interaction.user.id);
Kevinnnn
Kevinnnn4mo ago
thats- not how fetch works
mallusrgreat
mallusrgreat4mo ago
u have to await it
d.js docs
d.js docs4mo ago
Documentation suggestion for @n4a: :method: GuildMemberManager#fetch() Fetches member(s) from a guild.
Kevinnnn
Kevinnnn4mo ago
And yes, you need to use the await keyword, fetch() returns a promise that you need to resolve first before using it
n4a
n4a4mo ago
ty guys now its works