Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

routes - Discord.js Add Role with Express | Cannot read property 'add' of undefined

app.get('/whitelist/*', function(req, res) {
     var fullUrl = req.originalUrl.replace('/whitelist/', '');
     const userjs = bot.users.cache.get(fullUrl);
     userjs.roles.add('791498271922585614');
});

I want to give a role to a user via Express using the code above, but it doesn't work. I receive the following error:

TypeError: Cannot read property 'add' of undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to give a user a role, you'll need to get his GuildMember object.

To do that, you would have to get the Guild object of the guild you're referring to. You could simply do that using:

const guildObject = bot.guilds.cache.get('guild id here');

And finally, you could get the GuildMember object by typing:

const memberObject = guildObject.member(userjs); // AKA the user object you already have

Once that is done with, you can simply add the role using:

memberObject.roles.add('791498271922585614');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...