Discord.js v12角色增加了提到的用户问题

时间:2020-06-22 19:43:23

标签: discord.js roles

因此,我正在尝试制作一个类似$roletested @user的命令,该命令应该为用户提到特定的角色。我收到一个错误消息:_“无法读取未定义的属性“角色” ...请帮助我,这是我的代码:D

    if (message.content.startsWith(prefix + "roletested")) {
        let testedUser = message.mentions.members.first()
        if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
        if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌');
            declineMsg.delete({timeout: 5000});
            let testedRole = message.guild.roles.cache.find(role => role.id === "724676382776492113");
            testedUser.roles.add(testedRole);
            message.channel.send("Added the role `TESTED` to user.")
        })}})

2 个答案:

答案 0 :(得分:1)

您的代码已固定

  if (message.content.startsWith(prefix + "roletested")) {
    if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))

    let testedRole = message.guild.roles.cache.get('724676382776492113');
    let testedUser = message.mentions.members.first();
    if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌')
    declineMsg.delete({timeout: 5000});
    });
    
    testedUser.roles.add(testedRole);
    message.channel.send("Added the role `TESTED` to user.")
  }

答案 1 :(得分:0)

我修复了您的代码:

  • 我整理了一些东西(代码是一样的,我只是改变了一些东西)
  • 您不能使用message.guild.roles.cache.find(...),所以我将其更改为message.guild.role.cache.get('Role ID')

来源:Link

代码:

    if (message.content.startsWith(prefix + 'roletested')) {
        const testedUser = message.mentions.members.first();
        if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
        if (!testedUser) {
            message.channel.send('You have to mention the person you want to assign the role to!').then(declineMsg => {
                message.react('❌');
                declineMsg.delete({ timeout: 5000 });
                return;
            });
        }
        const testedRole = message.guild.roles.cache.get('395671618912780289');
        testedUser.roles.add(testedRole);
        message.channel.send('Added the role `TESTED` to user.');
    }

我在我的机器人上测试了此代码,它按预期工作。