【问题标题】:TypeError: Cannot read property 'execute' of undefined Discord Bot jsTypeError:无法读取未定义 Discord Bot js 的属性“执行”
【发布时间】:2020-10-15 10:45:45
【问题描述】:

我的问题如下:编译时,我收到未定义属性“执行”的错误。我要做的是打开另一个文件夹中的文件并将其停靠在 if 中,我在命令处理文档的指导下,我不知道错误是否在另一个名为“ping”的文件中.js'。我最近才开始,所以我不完全了解它。 主要代码如下:

const Discord = require('discord.js');
const { token, default_prefix } = require('./conf.json');
const client = new 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.once('ready', () => {
    console.log('Ready!');
});

client.on('message', async message => {
    if (!message.content.startsWith(default_prefix) || message.author.bot) return;

    const args = message.content.slice(default_prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping') {
        client.commands.get('ping').execute(message, args);
    }
});

client.login(token);

虽然“ping.js”代码是:

const Discord = require('discord.js');

module.exports = {
    description: "Get the latency of the bot.",
    usage: {},
    examples: {},
    aliases: [ "pong", "latency", "uptime" ],
    permissionRequired: 0,
    checkArgs: (args) => !args.length
  }

module.exports.run = async function (client, message, args, config, gdb, prefix, permissionLevel, db) {
    let botMsg = await message.channel.send("Pinging")
                    botMsg.edit({ 
                    embed: {
                    name: "ping",
                    title: "???? Ping",
                    color: 0x2ed32e,
                    description: [
                        "**Server**: `" + (message.createdAt - message.createdAt) + "ms`",
                        "**API**: `" + Math.round(client.ws.ping) + "ms`",
                        "**Uptime**: `" + msToTime(client.uptime) + "`"
                    ].join("\n"),
                    footer: { text: "Requested by " + message.author.tag, icon_url: message.author.displayAvatarURL }
                }
            })
        }

        function msToTime(ms) {...
        }

它可以工作,但如果我直接将它添加到主代码中,但我不希望这样。如果您有任何想法或知道解决方案,我将不胜感激。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    那是因为您在这一行将其命名为 run 而不是 execute

    module.exports.run = async function ()

    将其更改为执行,它应该可以正常工作,如果您想保留关键字run,而不是client.commands.get('ping').execute(message, args),请使用 client.commands.get('ping').run(message, args)

    另外我应该提到你有很多参数:

    execute function (client, message, args, config, gdb, prefix, permissionLevel, db) {
       //...
    }
    

    任何 after args 都将是未定义的,因为您只传递消息和 args,这里:

    client.commands.get('ping').execute(message, args)

    【讨论】:

      【解决方案2】:

      之所以说是execute is undefined,是因为您没有在ping.js 中定义execute

      您可以:

      • ping.js 中将module.exports.run 更改为module.exports.execute
      • 或在您的主文件中,将client.commands.get('ping').execute 更改为client.commands.get('ping').run

      原因是当调用command.execute() 时,您正试图调用命令模块中名为“execute”的函数。由于您将其命名为 run 而不是 execute,因此它会查找错误的函数并没有找到它。

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 2021-01-19
        • 2021-08-29
        • 1970-01-01
        • 2021-03-29
        • 2021-09-14
        • 2021-02-01
        • 1970-01-01
        • 2020-06-20
        相关资源
        最近更新 更多