NodeJS Discord.js v14 - TypeError:无法读取undefined的属性(阅读“commands”)

nc1teljy  于 2023-05-17  发布在  Node.js
关注(0)|答案(1)|浏览(111)

我最近一直在构建一个discord.js机器人来复习我的javascript,因为它已经有一段时间了。一直在遵循命令和事件重新加载斜杠命令的指南,但它出现了以下错误:

TypeError: Cannot read properties of undefined (reading 'commands')
    at loadCommands (/home/runner/tester/handlers/commandHandler.js:19:14)
    at Object.execute (/home/runner/tester/commands/owner/reload.js:33:11)
    at Client.<anonymous> (/home/runner/tester/index.js:58:17)
    at Client.emit (node:events:513:28)
    at Client.emit (node:domain:489:12)
    at InteractionCreateAction.handle (/home/runner/tester/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (/home/runner/tester/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (/home/runner/tester/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (/home/runner/tester/node_modules/discord.js/src/client/websocket/WebSocketShard.js:494:22)

至于它所引用的代码:

commandHandler.js:

function loadCommands(client) {
  const ascii = require("ascii-table");
  const fs = require("fs");
  const table = new ascii().setHeading("File Name", "Status");
  require("colors");

  let commandsArray = [];

  const commandsFolder = fs.readdirSync("./commands");
  for (const folder of commandsFolder) {
    const commandFiles = fs
      .readdirSync(`./commands/${folder}`)
      .filter((file) => file.endsWith(".js"));

    for (const file of commandFiles) {
      const commandFile = require(`../commands/${folder}/${file}`);

      const properties = { folder, ...commandFile };
      client.commands.set(commandFile.data.name, properties);

      commandsArray.push(commandFile.data.toJSON());

      table.addRow(file, "Loaded");
      continue;
    }
  }

  client.application.commands.set(commandsArray);

  return console.log(table.toString(), "\n[+]".green + " Loaded Commands");
}

module.exports = { loadCommands };

reload.js:

const { Client, SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const { loadCommands } = require("../../handlers/commandHandler");
const { loadEvents } = require("../../handlers/eventHandler");

module.exports = {
  data: new SlashCommandBuilder()
  .setName('reload')
  .setDescription('Reload your commands or events.')
  .addSubcommand(subcommand =>
  subcommand.setName('commands')
  .setDescription('Reload your commands.')
  )
    .addSubcommand(subcommand =>
      subcommand.setName('events')
      .setDescription('Reload your events.')
      ),

    async execute(interaction, client) {
      const { user } = interaction;
      if (user.id !== "217414221728710656") return interaction.reply({
        embeds: [new EmbedBuilder()
        .setColor('#2f3136')
        .setDescription('This command is only for the bot developer!')], ephemeral: true
      })

      const sub = interaction.options.getSubcommand()
      const embed = new EmbedBuilder()
      .setTitle('Developer')
      .setColor('#2f3136')

      switch (sub) {
        case "commands": {
          loadCommands(client)
          interaction.reply({ embeds: [embed.setDescription('Commands reloaded!')] })
          console.log(`${user} has reloaded the bot commands.`)
        }
        break;
        case "events": {
          loadEvents(client)
          interaction.reply({ embeds: [embed.setDescription('Events reloaded!')] })
          console.log(`${user} has reloaded the bot events.`)
        }
        break;
        }
    }
}

index.js

const { Client, Collection, Events, GatewayIntentBits, Partials, ActivityType } = require('discord.js');
require("colors");

const fs = require('node:fs');
const path = require('node:path');
const { token } = require('./config.json');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.commands = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require(filePath);
        if ('data' in command && 'execute' in command) {
            client.commands.set(command.data.name, command);
        } else {
            console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
        }
    }
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

const { loadCommands } = require("./handlers/commandHandler.js");
const { loadEvents } = require("./handlers/eventHandler.js");

client.once(Events.ClientReady, () => {
  console.log(`--------------------------------------------------------`)
  console.log("ඞ".red + " sus")
  console.log(`--------------------------------------------------------`)
                                            
  console.log(
    `Logged in as: ${client.user.tag.green} |`, `User ID: ${client.user.id.green}`,
  );
  client.user.setActivity("Duck ASMR", {type: ActivityType.Streaming, url: "https://www.twitch.tv/dashducks" });
  console.log(`--------------------------------------------------------`)
  console.log(`Current Servers In:`)
  console.log(``)
  client.guilds.cache.forEach((guild) => {
    console.log(`  - ${guild.name.blue}`);
  });
  console.log(`--------------------------------------------------------`)
});

client.on(Events.InteractionCreate, async interaction => {
  if (!interaction.isChatInputCommand()) return;
    const command = interaction.client.commands.get(interaction.commandName);

    if (!command) {
        console.error(`No command matching ${interaction.commandName} was found.`);
        return;
    }

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        if (interaction.replied || interaction.deferred) {
            await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
        } else {
            await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
        }
    }
});

module.expots = client;

client.login(token).then(() => {
  loadEvents(client);
  loadCommands(client);
})

const express = require('express')
const app = express();
const port = 2000;

app.get('/', (req, res) => {
  res.send('Hello World! ☀️');
})

  app.listen(port, () => {
  console.log(`- App webview active at http://localhost:${port}`)
})

尝试搜索有类似问题的用户,但找不到太多:(不太熟悉编码错误,所以我愿意接受阅读资源的建议,请帮助我学习!)
如果还需要什么就告诉我。先谢谢你了!

j8yoct9x

j8yoct9x1#

在index.js的第73行中,您正在为带有await command.execute(interaction);的命令调用execute,而没有将client作为第二个参数传递。这样做只是为了解决你的问题。

相关问题