NodeJS 命令.execute不是函数

bvjxkvbb  于 2022-12-22  发布在  Node.js
关注(0)|答案(1)|浏览(148)

我得到的错误:
类型错误:命令.execute不是对象上的函数。请在客户端执行(C:\用户\lachl\桌面\编码\不一致机器人\src\事件\客户端\交互创建.js:11:25)。在客户端执行(C:\用户\lachl\桌面\编码\不一致机器人\src\函数\处理程序\处理事件.js:15:63)。发出(节点:事件:513:28)
handleEvents.js文件:

const fs = require("fs");

module.exports = (client) => {
  client.handleEvents = async () => {
    const eventFolders = fs.readdirSync(`./src/events`);
    for (const folder of eventFolders) {
      const eventFiles = fs
        .readdirSync(`./src/events/${folder}`)
        .filter((file) => file.endsWith(".js"));
    switch (folder) {
        case 'client':
            for (const file of eventFiles) {
                const event = require(`../../events/${folder}/${file}`);
                if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
                else client.on(event.name, (...args) => event.execute(...args, client));
            }
            break;
    
        default:
            break;
    }
    
    }
  };
};

交互创建. js文件:

module.exports = {
  name: "interactionCreate",
  async execute(interaction, client) {
    if (interaction.isChatInputCommand()) {
      const { commands } = client;
      const { commandName } = interaction;
      const command = commands.get(commandName);
      if (!command) return;

      try {
          await command.execute(interaction, client);
      } catch (error) {
        console.error(error);
        await interaction.reply({
          content: `Something went wrong while executing this command...`,
          ephemeral: true,
        });
      }
    }
  },
};

handleCommands.js文件:

const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const fs = require("fs");

module.exports = (client) => {
  client.handleCommands = async () => {
    const commandFolders = fs.readdirSync(`./src/commands`);
    for (const folder of commandFolders) {
      const commandFiles = fs
        .readdirSync(`./src/commands/${folder}`)
        .filter((file) => file.endsWith(".js"));

      const { commands, commandArray } = client;
      for (const file of commandFiles) {
        const command = require(`../../commands/${folder}/${file}`);
        commands.set(command.data.name, command);
        commandArray.push(command.data.toJSON());
        console.log(
          `Command ${command.data.name} has passed through the handler`
        );
      }
    }

    const clientId = "1050985494537834606";
    const guildId = "1041260203112411197";
    const rest = new REST({ version: "9" }).setToken(process.env.token);
    try {
      console.log("Started refreshing application [/] commands");

      await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
        body: client.commandArray,
      });

      console.log("Successfully reloaded application [/] commands.");
    } catch (error) {
      console.error(error);
    }
  };
};

ping.js文件:

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
       .setName('ping')
       .setDescription("Ping Command!"),
    async exectute(interaction, client) {
        const message = await interaction.deferReply({
            fetchReply: true
        });
        
        const newMessage = `API LATENCY: ${client.ws.ping}\nClient Ping: ${message.createdTimeStamp - interaction.createdTimeStamp}`
        await interaction.editReply({
            content: newMessage
        });  
    
    }
}

当我运行代码时,机器人会上线,但当我尝试运行命令时,它不工作

相关问题