猿问

TypeError:无法读取未定义的 Discord,js 的属性“执行”

我有问题,我的代码正在显示


类型错误:无法读取暂停命令未定义的属性“执行”。所有其他命令都工作正常。


读取文件。


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 => {

    if (!msg.content.startsWith(prefix) || msg.author.bot) {

        return;

    }

    const args = msg.content.slice(prefix.length).trim().split(/ +/);

    const command = args.shift().toLowerCase();


    if (command === 'ping') {

        client.commands.get('ping').execute(msg, args);

    } else if (command === 'userinfo') {

        client.commands.get('userinfo').execute(msg, args);

    } else if (command === 'delete') {

        const amount = parseInt(args[0]) + 1;

        if (isNaN(amount)) {

            return msg.reply('Enter a valid number.');

        } else if (amount <= 1 || amount > 100) {

            return msg.reply('Enter a number between 1 and 99.');

        } else {

            msg.channel.bulkDelete(amount, true).catch(err => {

                console.error(err);

                msg.channel.send('There was an error trying to delete messages');

            });

            msg.channel.send(`Deleted ${args} messages.`);

        }

    } else if (command === 'p' || command === 'play') {

        client.commands.get('play').execute(msg, args);

    } else if (command === 'pause') {

        client.commands.get('pause').execute(msg);

    }

});


如果我复制代码并粘贴相同的代码,Client.on它可以正常工作,但在使用 with 时显示错误module.exports。有什么办法可以解决这个问题吗?


MMTTMM
浏览 101回答 3
3回答

吃鸡游戏

查看您的 pause.js 导出 -execute不是对象的属性。尝试这个:module.exports = {&nbsp; &nbsp; title: 'pause',&nbsp; &nbsp; description: "Pause the current song.",&nbsp; &nbsp; execute: function (message) {&nbsp; &nbsp; &nbsp; &nbsp;const queue = message.client.queue.get(message.guild.id);&nbsp; &nbsp; &nbsp; &nbsp;if(!queue) return message.reply("There is nothing playing").catch(console.error);&nbsp; &nbsp; &nbsp; &nbsp;if(queue.playing){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;queue.playing = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;queue.connection.dispatcher.pause(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return queue.textChannel.send(`⏸ Paused.`).catch(console.error);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }};创建一个名为的属性execute并将其值分配给您拥有的功能。

料青山看我应如是

您正在使用以下代码添加命令:client.commands = new Discord.Collection();const commandFIles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));for(const file of commandFIles){&nbsp; &nbsp; const command = require(`./commands/${file}`);&nbsp; &nbsp; client.commands.set(command.name, command);}所以命令名称是文件 export .name。在您的 expore 文件中,您没有名称属性。相反,你写了标题。尝试这个:module.exports = {&nbsp; &nbsp; name: 'pause',&nbsp; &nbsp; description: "Pause the current song.",&nbsp; &nbsp; execute(message){&nbsp; &nbsp; &nbsp; &nbsp;const queue = message.client.queue.get(message.guild.id);&nbsp; &nbsp; &nbsp; &nbsp;if(!queue) return message.reply("There is nothing playing").catch(console.error);&nbsp; &nbsp; &nbsp; &nbsp;if(queue.playing){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;queue.playing = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;queue.connection.dispatcher.pause(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return queue.textChannel.send(`⏸ Paused.`).catch(console.error);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }};

繁星coding

所以你设置命令按名称执行但在你的导出中没有名称,你在事件端的.get中调用它,这就是它返回未定义的原因,希望它有用
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答