【问题标题】:TypeError: Cannot read property of 'execute' of undefined When trying to execute a command fileTypeError:尝试执行命令文件时无法读取未定义的“执行”属性
【发布时间】:2021-01-25 02:27:03
【问题描述】:

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

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

});

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

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

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

【问题讨论】:

  • 你的set 会被称为client.commands.set('declineAlly', command); 所以......在你的.get 中,使用declineAlly 而不是./commands/declineAlly
  • 现在错误是找不到模块

标签: javascript node.js discord discord.js


【解决方案1】:

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

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

让我们从顶部开始。您将命令设置到集合中的代码工作正常。因此,让我们找到问题的根源,client.on('message', message 部分。在下面的代码 sn-ps 中,我总是使用 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);

});

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 2021-10-02
    • 2021-06-03
    • 2021-07-03
    • 2021-04-30
    • 2017-10-14
    • 1970-01-01
    • 2021-04-01
    相关资源
    最近更新 更多