给消息作者一个角色-Discord.js

时间:2020-10-23 19:43:04

标签: javascript node.js discord discord.js bots

我开始制作命令,消息作者在输入命令后应获得该角色。但是我总是收到“无法读取未定义'add'的属性”的错误。

代码:

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 个答案:

答案 0 :(得分:1)

  1. 要使用.add(),您需要GuildMember而不是用户
  2. 要添加角色,您应该传递Role而不是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;