Javascript TypeError:无法读取';执行';尝试执行命令文件时未定义的

Javascript TypeError:无法读取';执行';尝试执行命令文件时未定义的,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我正在用一个简单的命令处理程序制作一个discord机器人。我以前从未使用过命令处理程序,所以我对这类函数和类似的东西不太了解。我得到一个错误,说execute是未定义的,我不知道如何修复它。代码: module.exports={Discord:'Discord.JS'} module.exports={client:'Discord.client'} 常数fs=要求('fs'); client.commands=new Discord.Collection(); const commandF

我正在用一个简单的命令处理程序制作一个discord机器人。我以前从未使用过命令处理程序,所以我对这类函数和类似的东西不太了解。我得到一个错误,说execute是未定义的,我不知道如何修复它。代码:

module.exports={Discord:'Discord.JS'}
module.exports={client:'Discord.client'}
常数fs=要求('fs');
client.commands=new Discord.Collection();
const commandFiles=fs.readdirSync('./commands/').filter(file=>file.endsWith('.js'));
for(命令文件的常量文件){
const command=require(`./commands/${file}`);
client.commands.set(command.name,command);
};
client.on('message',msg=>{
让消息=“”;
if(msg.channel==msg.author.dmChannel){
返回
}否则{
client.commands.get('./commands/allyaapplication').execute(message,msg);
};
if(msg.content.toLowerCase().startsWith(“!accept”)){
client.commands.get('./commands/acceptly').execute(message,msg);
};
if(msg.content.toLowerCase().startsWith(“!谢绝”)){
client.commands.get('./commands/declinaly').execute(message,msg);
};
});
这是用于读取以下内容的脚本的代码:

module.exports={
名称:'十二行',
描述:'拒绝盟友',
执行(消息,消息){
代码的其余部分
}
} 

如果有人知道我如何修复这个错误,那就太好了,因为我是命令处理程序的新手。

要回答您的问题,您的代码不起作用,因为您需要像这样调用它
client.commands.get('declinally').execute(message,msg)client.commands.get('./commands/allyaapplication').execute(message,msg)else
,这意味着您的代码甚至没有达到定义命令的程度。此外,您总是向命令传递一个空字符串。您在这里所做的(这本身并没有错)是必须手动将每个命令添加到处理程序中。这不是很有效

让我们来解决这个问题

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

一开始你应该做两件事。首先检查频道是否是DM,如果是,返回

if (message.guild === null) {
    return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:"); //example sentence
}
并检查发送此消息的用户是否是机器人。这样其他机器人就不能使用您的

if (message.author.bot) return;
接下来要做的是设置一个前缀值,在您的情况下是
,并检查消息是否以该前缀开头

const prefix = '!';
if (!message.content.startsWith(prefix)) {
    return;
}
现在我们已经检查了消息是否实际上是一个命令,我们可以切掉前缀并将消息的其余部分转换为一个数组,我们将调用
args

const args = message.content.slice(prefix.length).trim().split(/ +/g);
接下来,我们需要获取该数组的第一个元素,删除它并将其存储为命令值

const cmd = args.shift().toLowerCase();
接下来,我们需要检查消息是否确实有一些参数。这很重要,如果消息只是一个简单的
,则不会执行此代码的其余部分

if (cmd.length === 0) return;
之后,是从我们的命令集合中获取命令的时候了

let command = client.commands.get(cmd);
完成后,我们需要检查该命令是否确实存在。如果不存在,则返回一个错误

if (!command) return message.reply(`\`${prefix + cmd}\` doesn't exist!`);
一旦我们确认该命令存在,就是执行该命令的时候了

command.execute(message, args);
现在,您已经完成了。命令处理程序已经完成。但是我们还没有完成。在您可以使用命令之前,我们需要在那里更改一些内容

  • 首先,从现在开始,您将使用命令名调用命令,而不是使用代码中的其他内容
  • 其次,您需要确保命令的名称完全是小写的。这是因为我们在处理程序中将命令转换为小写
最后,我们应该稍微更改一下命令,以便更易于阅读

module.exports = {
    name: 'declineally',
    description: 'Declines allies.',
    execute: (message, msg) => {
        // the rest of your code
    }
} 
所有这些之后,您的
client.on('message'
事件应该看起来有点像这样:

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);

});

你的
set
将被称为
client.commands.set('declinaly',command);
因此…在你的.get中,使用
declinaly
not
/commands/declinaly
现在找不到错误