Adding role to a user

I have a function that should add a role to a user.
async function assignRole(userId, roleId) {
try {
const guild = client.guilds.cache.get('922553636494639134');
const member = guild.members.cache.get(userId);
const role = guild.roles.cache.get(roleId);

if (member && role) {
await member.roles.add(role);
console.log(`Role ${role.name} added to user ${member.user}`);
} else {
console.error('Error assigning role: Member or role not found.');
}
} catch (error) {
console.error('Error assigning role:', error.message);
}
}
async function assignRole(userId, roleId) {
try {
const guild = client.guilds.cache.get('922553636494639134');
const member = guild.members.cache.get(userId);
const role = guild.roles.cache.get(roleId);

if (member && role) {
await member.roles.add(role);
console.log(`Role ${role.name} added to user ${member.user}`);
} else {
console.error('Error assigning role: Member or role not found.');
}
} catch (error) {
console.error('Error assigning role:', error.message);
}
}
I am calling it using this line:
await assignRole(participant.user_id, roleId);
await assignRole(participant.user_id, roleId);
And I am also import client from the index.js file which module.exports the client. But When running that function I get this error: Error assigning role: Cannot read properties of undefined (reading 'cache') (node:41004) Warning: Accessing non-existent property 'guilds' of module exports inside circular dependency (Use node --trace-warnings ... to show where the warning was created)
4 Replies
d.js toolkit
d.js toolkit3mo 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
Acemavie
Acemavie3mo ago
What do you mean by, What is the context you call that function in? It's a function for a function which is for a command
Acemavie
Acemavie3mo ago
Pastebin
const { createClient } = require('@supabase/supabase-js');const { s...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Acemavie
Acemavie3mo ago
from a command file
const { SlashCommandBuilder } = require('discord.js');
const chooseParticipants = require('../../utils/chooseParticipants');

module.exports = {
data: new SlashCommandBuilder()
.setName('choose')
.setDescription('Choose participants for an event (raffle)')
.addStringOption(option =>
option.setName('id')
.setDescription('ID of the event')
.setRequired(true))
.addIntegerOption(option =>
option.setName('amount')
.setDescription('Number of participants to choose')
.setRequired(true)),

async execute(interaction) {
const eventId = interaction.options.getString('id');
const amount = interaction.options.getInteger('amount');

// Choose participants
const result = await chooseParticipants(eventId, amount);

if (result.success) {
const chosenParticipants = result.chosenParticipants.map(participant => participant.minecraftUsername);
const message = `Chosen participants: ${chosenParticipants.join(', ')}`;
await interaction.reply(message);
} else {
await interaction.reply({ content: result.message, ephemeral: true });
}
}
};
const { SlashCommandBuilder } = require('discord.js');
const chooseParticipants = require('../../utils/chooseParticipants');

module.exports = {
data: new SlashCommandBuilder()
.setName('choose')
.setDescription('Choose participants for an event (raffle)')
.addStringOption(option =>
option.setName('id')
.setDescription('ID of the event')
.setRequired(true))
.addIntegerOption(option =>
option.setName('amount')
.setDescription('Number of participants to choose')
.setRequired(true)),

async execute(interaction) {
const eventId = interaction.options.getString('id');
const amount = interaction.options.getInteger('amount');

// Choose participants
const result = await chooseParticipants(eventId, amount);

if (result.success) {
const chosenParticipants = result.chosenParticipants.map(participant => participant.minecraftUsername);
const message = `Chosen participants: ${chosenParticipants.join(', ')}`;
await interaction.reply(message);
} else {
await interaction.reply({ content: result.message, ephemeral: true });
}
}
};
I tried putting the assignRole function in the index.js file and importing that but then I get the error: Error choosing participants: assignRole is not a function Error assigning role: Cannot read properties of undefined (reading 'cache') (node:1452) Warning: Accessing non-existent property 'guilds' of module exports inside circular dependency (Use node --trace-warnings ... to show where the warning was created) Oh, I got it working I just needed to pass the client to the assignRole function