TypeError:尝试执行命令文件时,无法读取未定义的'execute'的属性 [英] TypeError: Cannot read property of 'execute' of undefined When trying to execute a command file

查看:31
本文介绍了TypeError:尝试执行命令文件时,无法读取未定义的'execute'的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用一个简单的命令处理程序制作一个不和谐的机器人.我以前从未使用过命令处理程序,因此我对这类功能以及类似的东西并不了解很多.我收到一条错误消息,指出execute是未定义的,我不知道如何解决.代码:

I am making a discord bot with a simple command handler. I have never used a command handler before so I don't really know a lot about these sorts of functions and things like that. I get the an error saying that execute is undefined, which I do not know how to fix. Code:

module.exports = {Discord : 'Discord.JS'}
module.exports = {client : 'Discord.Client'}

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.on('message', msg => {
    let message = '';
    if (msg.channel === msg.author.dmChannel) {
        return
    } else {
            client.commands.get('./commands/allyApplication').execute(message, msg);
    };

    if (msg.content.toLowerCase().startsWith('!accept')) {
        client.commands.get('./commands/acceptAlly').execute(message, msg);
    };

    if (msg.content.toLowerCase().startsWith('!decline')) {
        client.commands.get('./commands/declineAlly').execute(message, msg);
    };

});

这是读取此内容的脚本的代码:

This is the code for scripts that read this:

module.exports = {
    name: 'declineAlly',
    description: 'Declines allies.',
    execute(message, msg) {
    REST OF CODE
    }
} 

如果有人知道我如何解决此错误,那将是很棒的,因为我是命令处理程序的新手.

If anyone knows how I could possibly fix this error, it would be great since I am new to command handlers.

推荐答案

要回答您的问题,您的代码不起作用,因为您需要像这样 client.commands.get('declineAlly').execute(message,msg); ,并且您始终会运行 client.commands.get('./commands/allyApplication').execute(message,msg); ,因为 else ,这意味着您的代码甚至还没有达到定义命令的地步.此外,您总是将空字符串传递给命令.您在这里所做的事情本身并没有错,那就是您必须手动将每个命令添加到处理程序中.那不是很有效.

To answer your question, your code doesn't work because for one you need to call it like this client.commands.get('declineAlly').execute(message, msg); and you always run client.commands.get('./commands/allyApplication').execute(message, msg); because of the else, meaning that your code doesn't even get to the point where you define your commands. Additionally you always pass an empty string to the command. What you also have done here, which is in itself not wrong, is that you have to manually add each command to the handler. Thats not really efficient.

所以让我们解决这个问题.

So lets fix that.

让我们从顶部开始.将命令设置到集合中的代码可以正常工作.因此,让我们找到问题的根源,即 client.on('message',message '部分.在以下代码段中,我始终使用 message 而不是味精.

Lets start at the top. Your code to set the commands into the collection works fine. So lets get to the root of your issue, the client.on('message', message part. in the following code snippets I always use message instead of msg.

开始时,您应该做两件事.首先检查该频道是否为DM,如果是则返回.

At the start you should do two things. First check if the channel is a DM and if so return.

if (message.guild === null) {
    return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:"); //example sentence
}

并检查发送此消息的用户是否是机器人.这样其他机器人就无法使用您的机器人.

And check if the user who sends this message is a bot. That way other bots can't use yours.

if (message.author.bot) return;

接下来应该做的是设置一个前缀值(在您的情况下为),并检查消息是否以所述前缀开头.

What you should do next is set a prefix value, in your case that would be ! and check if a message starts with said prefix.

const prefix = '!';
if (!message.content.startsWith(prefix)) {
    return;
}

现在,我们已经检查了消息是否实际上是一条命令,我们可以切掉前缀并将消息的其余部分转换成我们称为 args 的数组.

Now that we have checked if the message is actually a command we can slice off the prefix and convert the rest of the message into an array that we will call args.

const args = message.content.slice(prefix.length).trim().split(/ +/g);

接下来,我们需要获取该数组的第一个元素,将其删除并将其存储为命令值.我们还将其转换为小写.

Next we need to take the first element of that array, remove it and store it as our command value. We also convert it to lowercase.

const cmd = args.shift().toLowerCase();

接下来,我们需要检查消息中是否确实包含一些参数.这很重要,因此,如果消息只是简单的.

Next we need to check if the message actually has some arguments. Thats important so the rest of this code doesn't get executed if the message is just a simple !.

if (cmd.length === 0) return;

之后,该从我们的命令集中获取命令了.

After that it's time to get the command from our command collection.

let command = client.commands.get(cmd);

完成后,我们需要检查命令是否确实存在.如果没有,则返回错误.

Once that is done we need to check if the command actually exists. If it does not then we return with an error.

if (!command) return message.reply(`\`${prefix + cmd}\` doesn't exist!`);

一旦我们确认该命令存在,就该执行该命令了.

Once we have confirmed that the command exists it's time to execute that command.

command.execute(message, args);

就在那里.命令处理程序完成.我们还没有完成.在使用这些命令之前,我们需要在此处进行一些更改.

And there you have it. The command handler is finished. We are still not done yet though. Before you can use the commands we need to change something there.

  • 首先,从这一点开始,您将使用命令名称而不是代码中的其他名称来调用命令.
  • 第二,您需要确保命令名称完全小写.那是因为我们在处理程序中将命令转换为小写字母.

最后,我们应该稍微更改一下命令,以便于阅读.

Lastly we should change the command a little bit so it becomes easier to read.

module.exports = {
    name: 'declineally',
    description: 'Declines allies.',
    execute: (message, msg) => {
        // the rest of your code
    }
} 

所有这些之后,您的 client.on('message'事件应该看起来像这样:

After all of this, your client.on('message' event should look a litte something like this:

client.on('message', message => {
    // check if the message comes through a DM
    if (message.guild === null) {
        return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:");
    }
    // check if the author is a bot
    if (message.author.bot) return;
    
    // set a prefix and check if the message starts with it
    const 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 cmd = args.shift().toLowerCase();

    // check if there is a message after the prefix
    if (cmd.length === 0) return;

    // look for the specified command in the collection of commands
    let command = client.commands.get(cmd);

    // if there is no command we return with an error message
    if (!command) return message.reply(`\`${prefix + cmd}\` doesn't exist!`);

    // finally run the command
    command.execute(message, args);

});

这篇关于TypeError:尝试执行命令文件时,无法读取未定义的'execute'的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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