我收到错误Typerror无法读取未定义的属性'Execute' [英] I am getting an error Typerror Cannot read property 'execute' of undefined

查看:16
本文介绍了我收到错误Typerror无法读取未定义的属性'Execute'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要代码我正在尝试制作一个不和谐的机器人,我收到了这个错误,Typerror无法读取‘Execute’的属性未定义,我有几乎所有的解决方案,但它仍然有一些错误,如果有人解决了它,我将不胜感激。我正在试着做一个简单的迪斯科机器人,但代码不仅不能工作,请帮助。


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('yes its 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();


 client.on('guildMemberAdd', member =>{
       const channel = member.guild.channels.cache.find(channel=> channel.name === "hlo");
       if(!channel) return;

       channel.send(`Welcome to our server , ${member}, please read the rules!`);

    });
    

    if(command === 'ping'){
        message.channel.send('pong!');
    }
    if (command == 'kick'){
       client.commands.get('kick').execute(message, args);
    }
    if (command === 'ban'){
        client.commands.get('ban').execute(message, args);
        }
    
}); ```

My ban code 
```module.exports = {
    name: 'ban',
    description: "Uses ban hammer",
    execute(messsage, args){

    
     if (command === "ban"){
        const userBan = message.mentions.users.first();

        if(userBan){
            var member = message.guild.member(userBan);

            if(member) {
                member.ban({
                    reason: 'you broke rules buddy.'
                }).then(() => {
                    message.reply(`${userBan.tag} was banned from the server.`)
                })

            } else{
                message.reply('that user is not in the server.');
            }
        }else {
            message.reply('you need to state a user to ban')
        }
}
}

我的踢球代码

module.exports = {
    name: 'kick',
    description: 'kick people',
    execute(messsage, args){

    
     if (command === "kick"){
        const userKick = message.mentions.users.first();

        if(userBan){
            var member = message.guild.member(userKick);

            if(member) {
                member.kick({
                    reason: 'you broke rules buddy.'
                }).then(() => {
                    message.reply(`${userKick.tag} was kicked from the server.`)
                })

            } else{
                message.reply('that user is not in the server.');
            }
        }else {
            message.reply('you need to state a user to kick')
        }
}
}```

推荐答案

若要使此功能正常工作,您需要做两件事。

首先,您需要将on('guildMemberAdd')on('message')事件分开。现在他们搞得一团糟,那是行不通的。

因此您的索引文件应该是

// your command reader above this
client.once('ready', () => {
    console.log('yes its online');
});

client.on('guildMemberAdd', member => {
    // your code
}

client.on('message', message => {
    // here we will work in part two
}

其次,让我们看看您的命令阅读器。在我看来,您的那部分代码很不错。请确保commands文件夹与index文件位于同一目录中。我想这就是您的问题所在。

注意:我在这里说的是索引文件。我的意思是,您在其中包含client.login()函数的文件。它可能是您的bot.js或类似的内容。

您的文件夹结构应该如下所示

-- Your bot folder
    - index.js
    - package.json
    - package-lock.json
    -- commands (this is a folder)
        - kick.js
        - ban.js

注意:只是为了确保,这里有一个命令阅读器,可以完全使用上面的文件结构。

// Read all files in the commands folder and that ends in .js
const commands = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
// Loop over the commands, and add all of them to a collection
// If there's no name found, prevent it from returning an error
for (let file of commands) {
    const command = require(`./commands/${file}`);
    // Check if the command has both a name and a description
    if (command.name && command.description) {
        client.commands.set(command.name, command);
    } else {
        console.log("A file is missing something");
        continue;
    }
    
    // check if there is an alias and if that alias is an array
    if (command.aliases && Array.isArray(command.aliases))
        command.aliases.forEach(alias => client.aliases.set(alias, command.name));
};

您的client.on('message'事件中的命令处理程序对我有效。所以我想您的文件夹结构有问题。

话虽如此,我还是建议您使用一种稍微不同的方法来处理命令。目前您需要手动将命令添加到if链中。这并不是很有效率。

理想情况下,您希望自动执行该操作。你已经有了你的论点和你的命令词。您现在需要做的就是检查该命令是否存在,如果存在,则执行它。

// check if there is a message after the prefix
if (command.length === 0) return;
// look for the specified command in the collection of commands
let cmd = client.commands.get(command);
// if there is no command we return with an error message
if (!cmd) return message.reply(``${prefix + command}` doesn't exist!`);
// finally run the command
cmd.execute(message, args);

您的client.on('message'事件现在应该看起来有点像这样。

client.on('message', message => {
    // check if the author is a bot
    if (message.author.bot) return;
    // check if the message comes through a DM
    if (message.guild === null) return;
    // check if the message starts with the prefix
    if (!message.content.startsWith(prefix)) return;
    // slice off the prefix and convert the rest of the message into an array
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    // convert all arguments to lowercase
    const command = args.shift().toLowerCase();
    // check if there is a message after the prefix
    if (command.length === 0) return;
    // look for the specified command in the collection of commands
    let cmd = client.commands.get(command);
    // if there is no command we return with an error message
    if (!cmd) return message.reply(``${prefix + command}` doesn't exist!`);
    // finally run the command
    cmd.execute(message, args);
});
注意:我还注意到您的命令有一些不一致之处。但我假设,一旦您真正了解了命令,您将能够自己弄清楚😉

这篇关于我收到错误Typerror无法读取未定义的属性'Execute'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆