我正在用一个简单的命令处理程序制作一个不和谐的机器人.我以前从未使用过命令处理程序,所以我对这类函数和类似的东西不太了解.我收到一个错误,说执行未定义,我不知道如何解决.代码:
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:无法读取未定义的“执行"属性尝试执行命令文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!