TypeError:无法读取未定义的“执行"属性尝试执行命令文件时

2022-12-31前端开发问题
43

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

问题描述

我正在用一个简单的命令处理程序制作一个不和谐的机器人.我以前从未使用过命令处理程序,所以我对这类函数和类似的东西不太了解.我收到一个错误,说执行未定义,我不知道如何解决.代码:

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:无法读取未定义的“执行"属性尝试执行命令文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13