#a little file handling
49 messages · Page 1 of 1 (latest)
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");
},
};
Do you have another file in your commands folder? Seems like your ping file isn’t the issue
umm i dont
its like commands/info/ping.js
No other files in any other folder in commands/?
Nope
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
show your folder structure
should i send the code to my help command?
yes
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
did u figure anything out yet?
uhhhhh idts i can ping cuz against rules T-T
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
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'
}
Yes, for fs functions (like readdir) your paths were correct. Don’t change them there, only in require
In your join you have in your require you can just add another ../../ as first parameter
im new to this can you please do the change in code and tell
#rules 2&5
How did you get your hands on such elaborate code and also be new at this
little from chat gpt rest most of the part was from my old code
that was discord v12
Yeah, throw the ChatGPT code in the garbage immediately…
ikr 💀
Start there
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
You don’t even need a help command for slashcommands. They already show their help themselves in the UI
i still prefered one so that i dont forget what all there lol
Type a / and you see what‘s there, what do you mean?
ahhh if people have alot of bots in there server it becomes a little problem
anyways thanks for your time man appriciate it
Huh? Type a slash and select the bot from the menu
thanks for the help
Having 20 /help commands won‘t make that easier btw😉
Home: Introduction