简体   繁体   中英

Bot asks user what role they wish to join, bot adds them to role based on the reply

I am slowly learning javascript in my spare time and I haven't quite gotten to this yet. But a friend asked me to make a simple bot, one that private messages every new user that joins the server, asks them which color they would like their username to be and adds them to the role they reply with. The roles on the server have no permission significance. It is purely a color for the username (the discord server is a DeviantArt-based community chat).

In the below excerpt, it works just fine, until the user replies with a color "blue" and the reply of "Please choose a color or be sure you typed the color exactly as shown." is sent instead of adding the user to the right role and replying with what the color is. I am fairly certain that the issue with with this part:

user.addTo(server.roles.get("name", `${collected.first().content}`));
newmsg.channel.send(`Your name is now: **${collected.first().content}** Thank you and enjoy the server!`)

I am just unsure with what I am missing or how it should be structured to make it work. The colors listed do show up in lowercase in the roles area and the roles exist.

 bot.on('guildMemberAdd', member => { member.send("Welcome to RebornWings! Please select a color for your username! **Choices: orange-red, red, blue, blue-green, or purple** Please type exactly how they appear in the list of choices.") .then((newmsg) => { //Now newmsg is the message you sent newmsg.channel.awaitMessages(response => response.content, { max: 1, time: 300000, errors: ['time'], }).then((collected) => { user.addTo(server.roles.get("name", `${collected.first().content}`)) newmsg.channel.send(`Your name is now: **${collected.first().content}** Thank you and enjoy the server!`) }).catch(() => { newmsg.channel.send('Please choose a color or be sure you typed the color exactly as shown.'); }); }); }); bot.login(config.token); 

I think I am fairly close to having it from experimenting with things, just not sure what piece I am missing.

Thanks in advance for the help while I am slowly learning on my own.

The Catch Statement after the Role adding process is fired because your trying to not correctly get a Role and add it to an not existing variable.

It would be correct if you replace user with member and replace addTo with addRole.
Also get isn't used for this purpose. You use Collection#get with Snowflake IDs(The Roles of a Server are saved in a Collection).
For your purpose, you just have to replace get with find and it would work fine. But as Stock Overflaw already said I would first check if the user made a correct input. You NEVER trust the users input blind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM