TypeError: Cannot read properties of undefined (reading 'execute') command handler
TypeError: Cannot read properties of undefined (reading 'execute') command handler
const discord = require('discord.js')
const client = new discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
const prefix = '&';
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('ready', () => {
console.log("================");
console.log("|Bot is ready|");
console.log("================");
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ip'){
client.commands.get('ip').execute(message, args);
} else if (command == 'creator'){
client.commands.get('creator').execute(message, args);
} else if (command == 'rulespost'){
client.commands.get('rulespost').execute(message, args, discord);
} else if(command == 'test'){
client.commands.get('test').execute(message, args);
}
})
然后我得到一个错误 'rulespost' 说 TypeError: Cannot read properties of undefined (reading 'execute')
rulespost.js 内是
module.exports = {
name: 'RulesPost',
description: "Posts the rules of the server",
execute(message, args, discord) {
const newEmbed = Discord.MessageEmbed()
.setColor('#000000')
.setTitle('Official Rules for ALL Platforms')
.setDescription('...')
.addFields(
{value: 'TEXT'}
)
message.channel.send(newEmbed)
}
}
并且当使用 &rulespost 命令时,机器人死了,没有其他事情发生。
所有其他命令都可以正常工作,没有任何问题,但尝试使用嵌入命令会完全杀死机器人。
请注意,这是错误的,但由于某些原因已被标记,因此无法将其删除。
execute 函数的语法不正确。解释器认为您正在调用一个函数,然后将更多项传递给该对象。这可以通过添加 function 关键字或通过将 execute 分配给匿名函数来解决,如下所示:
module.exports = {
name: 'RulesPost',
description: "Posts the rules of the server",
// Execute is the key for the anon. function
execute: function(message, args, discord) {
const newEmbed = Discord.MessageEmbed()
.setColor('#000000')
.setTitle('Official Rules for ALL Platforms')
.setDescription('...')
.addFields(
{value: 'TEXT'}
)
message.channel.send(newEmbed)
}
}
这将创建您要导出的执行函数。
const discord = require('discord.js')
const client = new discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
const prefix = '&';
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('ready', () => {
console.log("================");
console.log("|Bot is ready|");
console.log("================");
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ip'){
client.commands.get('ip').execute(message, args);
} else if (command == 'creator'){
client.commands.get('creator').execute(message, args);
} else if (command == 'rulespost'){
client.commands.get('rulespost').execute(message, args, discord);
} else if(command == 'test'){
client.commands.get('test').execute(message, args);
}
})
然后我得到一个错误 'rulespost' 说 TypeError: Cannot read properties of undefined (reading 'execute')
rulespost.js 内是
module.exports = {
name: 'RulesPost',
description: "Posts the rules of the server",
execute(message, args, discord) {
const newEmbed = Discord.MessageEmbed()
.setColor('#000000')
.setTitle('Official Rules for ALL Platforms')
.setDescription('...')
.addFields(
{value: 'TEXT'}
)
message.channel.send(newEmbed)
}
}
并且当使用 &rulespost 命令时,机器人死了,没有其他事情发生。 所有其他命令都可以正常工作,没有任何问题,但尝试使用嵌入命令会完全杀死机器人。
请注意,这是错误的,但由于某些原因已被标记,因此无法将其删除。
execute 函数的语法不正确。解释器认为您正在调用一个函数,然后将更多项传递给该对象。这可以通过添加 function 关键字或通过将 execute 分配给匿名函数来解决,如下所示:
module.exports = {
name: 'RulesPost',
description: "Posts the rules of the server",
// Execute is the key for the anon. function
execute: function(message, args, discord) {
const newEmbed = Discord.MessageEmbed()
.setColor('#000000')
.setTitle('Official Rules for ALL Platforms')
.setDescription('...')
.addFields(
{value: 'TEXT'}
)
message.channel.send(newEmbed)
}
}
这将创建您要导出的执行函数。