简体   繁体   中英

Give a Role to the message author - Discord.js

I started making a command, where the message author should get the role after entering the command. But I always get the error for "Cannot read property of 'add' of undefined".

Code:

case 'ticket':
                if(!message.channel.id === "757221433335218216") return;
                if (message.channel.id === "757221433335218216"){
                    var randomRole = Math.random()
                    const person = message.author
                   var roleCreator = message.guild.roles.create({
                    data: {
                        name: randomRole.toLocaleString(),
                        color: "#ff0000",
                        permissions: 0
                    }
                }).then(role => {
                    person.roles.add(roleCreator)
                });
                break;
  1. To use .add() you will need GuildMember not a User
  2. To add the role, you should pass the Role instead of the roleCreator.
case 'ticket':
                if(!message.channel.id === "757221433335218216") return;
                if (message.channel.id === "757221433335218216"){
                    var randomRole = Math.random()
                    const person = message.member //<< Here, get the author as an GuildMember instead of getting author as an User
                   var roleCreator = message.guild.roles.create({
                    data: {
                        name: randomRole.toLocaleString(),
                        color: "#ff0000",
                        permissions: 0
                    }
                }).then(role => {
                    person.roles.add(role) // <<Here, passing role instead of roleceator
                });
                break;

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