Add role to user

Hey guys, im trying to add role to user but it didn't work
const guild = client.guilds.cache.get("1078542061235609640");

const member = guild.members.find((m) => m.id === interaction.user.id);

member.roles.cache.add("1082536706143703060")
const guild = client.guilds.cache.get("1078542061235609640");

const member = guild.members.find((m) => m.id === interaction.user.id);

member.roles.cache.add("1082536706143703060")
But it return D:\Project\Discord\Bots\GoodLuck\GoodLuck\command\BlackJack.js:121 const member = guild.members.find((m) => m.id === interaction.user.id); ^ TypeError: guild.members.find is not a function at BlackJack.interactionCreate (D:\Project\Discord\Bots\GoodLuck\GoodLuck\command\BlackJack.js:121:36) at Object.execute (D:\Project\Discord\Bots\GoodLuck\GoodLuck\Events\interactionCreate.js:14:22) at Client.execute (D:\Project\Discord\Bots\GoodLuck\GoodLuck\Handler\eventHandler.js:13:44) at Client.emit (node:events:523:35) at InteractionCreateAction.handle (D:\Project\Discord\Bots\GoodLuck\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12) at module.exports [as INTERACTION_CREATE] (D:\Project\Discord\Bots\GoodLuck\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (D:\Project\Discord\Bots\GoodLuck\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31) at WebSocketShard.onPacket (D:\Project\Discord\Bots\GoodLuck\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22) at WebSocketShard.onMessage (D:\Project\Discord\Bots\GoodLuck\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10) at callListener (D:\Project\Discord\Bots\GoodLuck\node_modules\ws\lib\event-target.js:290:14) Node.js v20.3.1 How can I add role to user?
26 Replies
d.js toolkit
d.js toolkit16mo 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!
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
Thank your response, <GuildMember>.roles.add(…) this add parameter is role's id ?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
It works perfectly just local parameter, but when I use it on express server it return this DiscordAPIError[50035]: Invalid Form Body user_id[NUMBER_TYPE_COERCE]: Value "favicon.ico" is not snowflake. at handleErrors (D:\Project\Starly\web\node_modules\@discordjs\rest\dist\index.js:687:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async SequentialHandler.runRequest (D:\Project\Starly\web\node_modules\@discordjs\rest\dist\index.js:1072:23) at async SequentialHandler.queueRequest (D:\Project\Starly\web\node_modules\@discordjs\rest\dist\index.js:913:14) at async _REST.request (D:\Project\Starly\web\node_modules\@discordjs\rest\dist\index.js:1218:22) at async GuildMemberManager._fetchSingle (D:\Project\Starly\web\node_modules\discord.js\src\managers\GuildMemberManager.js:221:18) { requestBody: { files: undefined, json: undefined }, rawError: { message: 'Invalid Form Body', code: 50035, errors: { user_id: { _errors: [ { code: 'NUMBER_TYPE_COERCE', message: 'Value "favicon.ico" is not snowflake.' } ] } } }, code: 50035, status: 400, method: 'GET', url: 'https://discord.com/api/v10/guilds/1141562591940968481/members/favicon.ico' } Node.js v20.3.1 [nodemon] app crashed - waiting for file changes before starting...
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
app.get('/:id', (req, res) => {
const id = req.params.id
const ip = req.clientIp;
const memberId = '273686048121290763'
const guildId = '1141562591940968481'
const roleId = '1142067076564009060'

const guild = client.guilds.cache.get(guildId);
const member = guild.members.fetch(id);

member.then(function(test){
test.roles.add(roleId)
})

res.send("test")
})
app.get('/:id', (req, res) => {
const id = req.params.id
const ip = req.clientIp;
const memberId = '273686048121290763'
const guildId = '1141562591940968481'
const roleId = '1142067076564009060'

const guild = client.guilds.cache.get(guildId);
const member = guild.members.fetch(id);

member.then(function(test){
test.roles.add(roleId)
})

res.send("test")
})
oh req.params.id is this a favicon.ico?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
I just edit it to this
app.get('/:id', async(req, res) => {
const id = req.params.id
const ip = req.clientIp;

res.send("test")

const memberId = '273686048121290763'
const guildId = '1141562591940968481'
const roleId = '1142067076564009060'

const guild = client.guilds.cache.get(guildId);
const member = await guild.members.fetch(id);

console.log(member)
})
app.get('/:id', async(req, res) => {
const id = req.params.id
const ip = req.clientIp;

res.send("test")

const memberId = '273686048121290763'
const guildId = '1141562591940968481'
const roleId = '1142067076564009060'

const guild = client.guilds.cache.get(guildId);
const member = await guild.members.fetch(id);

console.log(member)
})
but it still return error ..
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
I think this value const id = req.params.id is problem
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
Yeah It just return below pic
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
Yeah! that is a user Id
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
I was a idiot ....
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
WHen I was tried to print
console.log(new String(id))
console.log(new String(id))
it return
계정 버림
계정 버림OP16mo ago
lmao
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
Next Step: Getting params Without favicon.ico
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
Ohhhh I wonder why the params value returning soo stealthy The "favicon.ico"
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
계정 버림
계정 버림OP16mo ago
Thank uu then it won't return favicon anymore? Thank u ill try it later Thank uuuuu it work perfectly !!
Want results from more Discord servers?
Add your server