'trying to give role for voter top.gg

i'm trying to give top.gg voters role in my server but there's so many thing happened got me confused and i can't find any solution for it.

full source code : https://sourceb.in/dSlb2nhjPH

here's my code :

app.post('/vote', webhook.listener(vote => {

    client.users.fetch(vote.user).then(user => {
        console.log(`Found ${user.tag}.`);
        let role = client.guilds.cache.get('928290243843604500').roles.cache.get("942061236475002952")
        user.roles.add(role).catch(e => { console.log('Something went wrong, i cant add roles') })
        
    }).catch(error => {
        console.log("Couldn't find the user.");
    });

}))

so after someone vote the console log showing this:

Found ' Ab#2000.
Couldn't find the user.

which mean there's something wrong it showing the user found and showing his tag but didn't give the role to the user then just showing Couldn't find the user.

i hope someone could help with this i have been trying to setup custom vote system for a while



Solution 1:[1]

You can't add a role to a User. You need to fetch the User by ID as a GuildMember in a particular guild, then you can add role to that member. Like-

const guild = client.guilds.cache.get("guildID")
const role = guild.roles.cache.get("roleID")
const member = guild.members.cache.get(vote.user)
if (!member) return;
member.roles.add(role).then(() => ...).catch(err => ...)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glitter