TypeError:无法读取未定义不一致Bot js的属性'Execute' [英] TypeError: Cannot read property 'execute' of undefined Discord Bot js

查看:0
本文介绍了TypeError:无法读取未定义不一致Bot js的属性'Execute'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:在编译时,我收到未定义属性‘Execute’的错误。我试图做的是打开另一个文件夹中的文件并将其停靠在命令处理文档中,我不知道错误是否在另一个名为‘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("
"),
                    footer: { text: "Requested by " + message.author.tag, icon_url: message.author.displayAvatarURL }
                }
            })
        }

        function msToTime(ms) {...
        }

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

推荐答案

它说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,因此它会查找错误的函数,但找不到它。

这篇关于TypeError:无法读取未定义不一致Bot js的属性'Execute'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆