【解決方法】私のJSコードには何が欠けていますか?

[ad_1]

Bot.js コード:

require("dotenv").config();
const { token } = process.env;
const { Client, Collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");

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

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync(`./src/functions/${folder}`).filter((file) => file.endsWith(".js"));
  for (const file of functionFiles)
    require(`./functions/${folder}/${file}`)(client);
}

client.handleEvents();
client.handleCommands();
client.login(token);

handleEvents.js

const { Events } = require("discord.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(../../src/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;
      }
    }
  };
};

エラー:

> hyperbot@1.0.0 test
> node .

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module '.src/functions/handlers/handleCommands.js'
Require stack:
- C:\Users\Admin\Desktop\HyperBot\src\bot.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (C:\Users\Admin\Desktop\HyperBot\src\bot.js:15:5)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\Admin\\Desktop\\HyperBot\\src\\bot.js' ]
}

Node.js v18.12.1

私が試したこと:

から変更してみました

const event = require(../../src/Events/${folder}/${file});

const event = require(src/Events/${folder}/${file});

解決策 1

./src/functions の大文字と小文字を区別する文字の変更

[ad_2]

コメント

タイトルとURLをコピーしました