类型错误:无法读取未定义的属性“角色” - 输入被错误读取

时间:2021-07-28 03:18:37

标签: node.js discord.js

不确定如何获取目标的 id,因为它一直将其读取为未定义。我的论点可能不正确,但我很确定这不是问题所在。如果有帮助,我的踢球命令是 !o kick @user kick reason

const { message } = require("discord.js")

module.exports = {
    name: 'kick',
    description: 'This command kicks a member',
        execute(message, args){
            if (message.member.hasPermission(["KICK_MEMBERS"])) {
            const target = message.mentions.members.first();
            if(target){
                let [target, ...reasonKick] = args;
                [...reasonKick].join(' ');
                message.guild.members.cache.find(target => target.id)
                const memberTarget = message.guild.members.cache.get(target.id);
                if (message.guild.me.hasPermission("KICK_MEMBERS")) {

                if (message.author.id === memberTarget) {
                    message.reply('why would you want to kick yourself?');
                }
                else if (message.client.user.id === memberTarget) {
                    message.channel.send('Nice try, but you can\'t kick me.')
                }
                else if (message.member.roles.highest.position > message.guild.members.cache.get(memberTarget).roles.highest.position) {
                    memberTarget.kick()
                    message.channel.send(`**${target.tag}** has been kicked. Reason: **${reasonKick}**.`);
                    }
                else {
                    message.reply(`I don\'t have the required permission to kick **${target.tag}**`);
                        }
                    } else {
                        message.reply('I don\'t have the required permission to kick members');
                    }
            } else{
                message.reply('you must mention the member you want to kick.')
                  .catch;
            }
        } else {
            message.reply("you do not have the permission to do that.")
              .catch;
            }
    },
};
// - someCollection.find('property', value);
// + someCollection.find(element => element.property === value)

1 个答案:

答案 0 :(得分:1)

提及已返回成员对象,无需尝试根据提及的 id 获取成员。

memberTarget 替换所有的 target