how to add a role from a user ID and a role ID

I would like to assign role using user ID and role ID, but in my research I could only find the following example.
let role= member.guild.roles.cache.find(role => role.name === "role name");
member.roles.add(role);
let role= member.guild.roles.cache.find(role => role.name === "role name");
member.roles.add(role);
Sorry for the rudimentary question.
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2y ago
method GuildMemberManager#addRole() Adds a role to a member.
kokastar
kokastar2y ago
Sorry for repeating myself. What code should I write in this case?
client.on('voiceStateUpdate', (oldState, newState) => {
if(oldState.channel===null){
//The "userID" variable is the userID and the "role" variable is the roleID.
//I want to grant it here.
}
})
client.on('voiceStateUpdate', (oldState, newState) => {
if(oldState.channel===null){
//The "userID" variable is the userID and the "role" variable is the roleID.
//I want to grant it here.
}
})
I would like to know how to get "<Guild>.members"
d.js docs
d.js docs2y ago
property VoiceState#guild The guild of this voice state
kokastar
kokastar2y ago
Is newState.guild.member correct? If so, it is undefined. newState.guild.members.addRole(role) I tried as follows. However, I got the following error
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[50035]: Invalid Form Body
role_id[NUMBER_TYPE_COERCE]: Value "null" is not snowflake.
user_id[NUMBER_TYPE_COERCE]: Value "null" is not snowflake.
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[50035]: Invalid Form Body
role_id[NUMBER_TYPE_COERCE]: Value "null" is not snowflake.
user_id[NUMBER_TYPE_COERCE]: Value "null" is not snowflake.
Sorry for repeating myself. I read it but did not understand it. If possible, I would like an explanation with specific examples. I was able to do it successfully. After comparing what I was taught and reading the documentation, I think I have some idea of how to read it. Thank you.