#a little file handling

49 messages · Page 1 of 1 (latest)

torn valve
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

orchid marsh
#

C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\app.js:17
client.commands.set(command.data.name, command);
^

TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\app.js:17:42)
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:82:12)
at node:internal/main/run_main_module:23:47

Node.js v19.0.1

#

my ping command

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('ping')
    .setDescription('Responds with pong!'),
  async execute(interaction) {
    await interaction.reply('Pong! My current ping is '+interaction.client.ws.ping+"ms");
  },
};
hollow nacelle
#

Do you have another file in your commands folder? Seems like your ping file isn’t the issue

orchid marsh
#

its like commands/info/ping.js

hollow nacelle
#

No other files in any other folder in commands/?

orchid marsh
#

Nope

orchid marsh
#

Error: Cannot find module 'commands\info\help.js'
Require stack:

  • C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\commands\info\help.js
  • C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\app.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.execute (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\commands\info\help.js:26:25)
    at Client.<anonymous> (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\app.js:46:23)
    at Client.emit (node:events:513:28)
    at InteractionCreateAction.handle (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
    'C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\commands\info\help.js',
    'C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\app.js'
    ]
#

i made some more files i repair the file for ping

orchid marsh
orchid marsh
#

should i send the code to my help command?

brazen arrow
#

yes

orchid marsh
#
const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilder } = require('discord.js');
const { readdirSync, statSync } = require('fs');
const { join } = require('path');

const isDirectory = source => statSync(source).isDirectory();
const getDirectories = source => readdirSync(source).map(name => join(source, name)).filter(isDirectory);

module.exports = {
  data: new SlashCommandBuilder()
    .setName('help')
    .setDescription('Displays a list of available commands.'),

  async execute(interaction) {
    // Recursively read all command files from the `commands` folder and its subdirectories.
    const commandFolders = getDirectories('commands');
    const commandMap = new Map();

    for (const folder of commandFolders) {
      const commandFiles = readdirSync(folder).filter(file => file.endsWith('.js'));

      for (const file of commandFiles) {
        const command = require(join(folder, file));

        if (command.data.category) {
          // If the command has a category, add it to the map.
          if (commandMap.has(command.data.category)) {
            commandMap.get(command.data.category).push(command);
          } else {
            commandMap.set(command.data.category, [command]);
          }
        }
      }
    }

    // Create an embedded message with a list of available commands grouped by category.
    const helpEmbed = new EmbedBuilder()
      .setColor('#0099ff')
      .setTitle('Command List');

    for (const [category, commands] of commandMap.entries()) {
      const commandList = commands.map(c => `/${c.data.name} - ${c.data.description}`).join('\n');
      helpEmbed.addFields({name:category, fields:commandList});
    }

    // Send the embedded message with the list of available commands.
    await interaction.reply({ embeds: [helpEmbed], ephemeral: true });
  },
};
#

this is my help.js file in info folder

orchid marsh
#

did u figure anything out yet?

orchid marsh
#

uhhhhh idts i can ping cuz against rules T-T

hollow nacelle
#

Your paths are relative to the cwd (which works fine for fs). But require needs paths relative to the current file (which is in another directory). So it tries to find the file commands/info/commands/info/help.js to require it. Which doesn’t exist

#

So either use absolute paths or add ../../ to the beginning when passing to require

orchid marsh
#

oh okay thank you so much

#

i made the line 16
const commandFolders=getDirectories("../../commands") but im getting this

Error: ENOENT: no such file or directory, scandir '../../commands'
    at readdirSync (node:fs:1445:3)
    at getDirectories (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\commands\info\help.js:7:34)
    at Object.execute (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\commands\info\help.js:16:28)
    at Client.<anonymous> (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\app.js:46:23)
    at Client.emit (node:events:513:28)
    at InteractionCreateAction.handle (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22)
    at WebSocketShard.onMessage (C:\Users\Admin\OneDrive\Documents\Cyte bot\djbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10) {
  errno: -4058,
  syscall: 'scandir',
  code: 'ENOENT',
  path: '../../commands'
}


hollow nacelle
#

Yes, for fs functions (like readdir) your paths were correct. Don’t change them there, only in require

orchid marsh
#

oh okay

#

im unable to make it relative can u plz help if possible?

hollow nacelle
#

In your join you have in your require you can just add another ../../ as first parameter

orchid marsh
#

im new to this can you please do the change in code and tell

hollow nacelle
#

How did you get your hands on such elaborate code and also be new at thisThonk

orchid marsh
#

little from chat gpt rest most of the part was from my old code

#

that was discord v12

hollow nacelle
#

Yeah, throw the ChatGPT code in the garbage immediately…

orchid marsh
#

ikr 💀

mortal hillBOT
hollow nacelle
#

Start there

orchid marsh
#

i did see the docs ngl

#

kind of complicated for me

#

i did most of the bot stuff

#

from there

#

this help command is the difficult part

hollow nacelle
#

You don’t even need a help command for slashcommands. They already show their help themselves in the UI

orchid marsh
#

i still prefered one so that i dont forget what all there lol

hollow nacelle
#

Type a / and you see what‘s there, what do you mean?

orchid marsh
#

ahhh if people have alot of bots in there server it becomes a little problem

#

anyways thanks for your time man appriciate it

hollow nacelle
#

Huh? Type a slash and select the bot from the menu

orchid marsh
#

thanks for the help

hollow nacelle
#

Having 20 /help commands won‘t make that easier btw😉