无法读取未定义的属性“执行” - 不和谐

时间:2021-01-11 14:03:37

标签: javascript discord

我正在编写一个不和谐的机器人。我遇到问题的命令是 temprole 和 kick。 ping 命令似乎工作正常。

main.js

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Bot is online!');
});

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command == 'ping') {
        client.commands.get('ping').execute(message, args);
    } else if (command == 'temprole') {
        client.commands.get('temprole').execute(message, args);
    }
});

client.login('token')

temprole.js

module.exports = {
     name: 'youtube',
     description: "give the member a temprole",
     execute(message, args) {

         if (message.member.roles.cache.has('798180573998612512')) {

         } else {
             message.channel.send('You dont have access to this command!')
         }
     }
 }

kick.js

module.exports = {
    name: 'kick',
    description: "This command kicks a member!",
    execute(messages, args) {
        const member = message.mentions.users.first();
        if (member) {
            const memberTarger = message.guild.members.cache.get(member.id);
            memberTarger.kick();
            message.channel, send('User has been kicked');
        } else {
            message.channel.send('You coundt kick that member');
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在您的 temprole.js 中,“name”属性与 temprole 不匹配,因此命令集合会将命令名称设置为“youtube”。然后您尝试从集合中获取“temprole”命令,但没有。解决方法是将命令名称的属性更改为“temprole”。