#Discord Bot when start

113 messages · Page 1 of 1 (latest)

fallen quest
#

node:fs:1508
const result = binding.readdir(
^

Error: ENOTDIR: not a directory, scandir 'C:\Users\jeanb\Desktop\Bot Discord\Code\commands\ping.js'
at Object.readdirSync (node:fs:1508:26)
at Object.<anonymous> (C:\Users\jeanb\Desktop\Bot Discord\Code\index.js:13:26)
at Module._compile (node:internal/modules/cjs/loader:1369:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
at Module.load (node:internal/modules/cjs/loader:1206:32)
at Module._load (node:internal/modules/cjs/loader:1022:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49 {
errno: -4052,
code: 'ENOTDIR',
syscall: 'scandir',
path: 'C:\Users\jeanb\Desktop\Bot Discord\Code\commands\ping.js'
}

Node.js v20.12.0

jaunty cloudBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
fallen quest
#

Discord Bot when start

coral ore
#

a file is indeed not a directory

obsidian prismBOT
fallen quest
#

Hi i follow this

#

wdym by file what i need to do ?

coral ore
#

your code is reading ping.js as a folder, and the guide assumes you have folder categories

fallen quest
#

so i need to create a folder ping ?

#

sorry im bad i just starting the js

coral ore
#

you should have a general understanding of javascript before making a discord bot

fallen quest
#

ohh i find thanks

fallen quest
#

Now i have this error
Started refreshing application (/) commands.
ReferenceError: commands is not defined
at C:\Users\jeanb\Desktop\Bot Discord\Code\index.js:36:12
at Object.<anonymous> (C:\Users\jeanb\Desktop\Bot Discord\Code\index.js:44:3)
at Module._compile (node:internal/modules/cjs/loader:1369:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
at Module.load (node:internal/modules/cjs/loader:1206:32)
at Module._load (node:internal/modules/cjs/loader:1022:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

#
const fs = require('fs');
const path = require('path');
const { REST } = require('@discordjs/rest');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { Routes } = require('discord-api-types/v9');

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 rest = new REST().setToken(process.env.TOKEN);

(async () => {
    try {
        console.log(`Started refreshing application (/) commands.`);

        const data = await rest.put(
            Routes.applicationGuildCommands(process.env.CLIENT, process.env.GUILD),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        
        console.error(error);
    }
})();

client.login(process.env.TOKEN);
#

i follow the doc

coral ore
#

that is not from the guide i sent

#

not sure where it says to register commands and set the collection in the same file

obsidian prismBOT
#

guide Creating Your Bot: Registering slash commands
read more

fallen quest
#

and i put into

fallen quest
#

When i do node deploy-commands.js i have this error

[WARNING] The command at C:\Users\jeanb\Desktop\Bot Discord\Code\commands\utility\help.js is missing a required "data" or "execute" property.
Started refreshing 1 application (/) commands.
Error: Expected token to be set for this request, but none was present
    at _REST.resolveRequest (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1340:15)    at _REST.queueRequest (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1300:46)
    at _REST.request (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1266:33)
    at _REST.put (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1249:17)
    at C:\Users\jeanb\Desktop\Bot Discord\Code\deploy-commands.js:35:27
    at Object.<anonymous> (C:\Users\jeanb\Desktop\Bot Discord\Code\deploy-commands.js:45:3)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
#

this is the code of deploy-commands.js

const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// Grab all the command folders from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
    // Grab all the command files from the commands directory you created earlier
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require(filePath);
        if ('data' in command && 'execute' in command) {
            commands.push(command.data.toJSON());
        } else {
            console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
        }
    }
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.TOKEN);

// and deploy your commands!
(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        // The put method is used to fully refresh all commands in the guild with the current set
        const data = await rest.put(
            Routes.applicationCommands(process.env.CLIENT, process.env.GUILD),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        // And of course, make sure you catch and log any errors!
        console.error(error);
    }
})();
coral ore
#

did you put your bot token in a .env file

fallen quest
#

yeah

#

TOKEN=
GUILD=
CLIENT=
all

#

with an .env.local

coral ore
#

remove .local

fallen quest
#

i have these two

#

oh ok

coral ore
#

did you install dotenv and initialize it with config()

fallen quest
#

i inistall with npm install dotenv but i don't initialize it with config

#

what's this "initialize it with config"?

obsidian prismBOT
#

guide Creating Your Bot: Configuration files - Using environment variables > Using dotenv
read more

fallen quest
#

oh yeah i config yaeh

#

"start": "node --env-file=.env.local index.js"

#

i just use npm start run

coral ore
#

it should be npm run start*

fallen quest
#

yeah npm start run typing error

coral ore
#

no it’s npm run start

#

not start run

fallen quest
#

yeah run start re typing error

#

sorry

#

but i still have this error

[WARNING] The command at C:\Users\jeanb\Desktop\Bot Discord\Code\commands\utility\help.js is missing a required "data" or "execute" property.
Started refreshing 1 application (/) commands.
Error: Expected token to be set for this request, but none was present
    at _REST.resolveRequest (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1340:15)
    at _REST.queueRequest (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1300:46)
    at _REST.request (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1266:33)
    at _REST.put (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1249:17)
    at C:\Users\jeanb\Desktop\Bot Discord\Code\deploy-commands.js:36:27
    at Object.<anonymous> (C:\Users\jeanb\Desktop\Bot Discord\Code\deploy-commands.js:46:3)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
coral ore
#

i hope you’re not trying to register commands on every restart

#

please make sure you have a token set in your .env

log process.env.TOKEN in your deploy-commands.js file to verify that a token is present

fallen quest
#

no i just follow the doc step by step i add interactionCreate.js & read.js and it work

fallen quest
coral ore
#

that error is pretty self explanatory

#

above i wasn’t aware of what the actual error was

#

i was assuming it had to do with the token, not a missing property

fallen quest
#

execute is good

coral ore
#

missing a required “data” or “execute” property

fallen quest
#

oh i need to use both?

coral ore
#

that’s up to you, but your code checks for that and will log if either one isn’t present

fallen quest
#

when i use only data i have the same error i'll use both

coral ore
#

yes because your code checks for both

#

you don’t have to have that check

#

though the guide does recommend it based on following everything to have a organized project

#

it’s not obligatory

fallen quest
#

and how can i use both ?

#

i don't know how

coral ore
#

if you followed the guide, you would see how data is used

obsidian prismBOT
fallen quest
fallen quest
#

yeah i have this in index.js

coral ore
#

okay, but your command is still missing the data property

#

and the usage is shown from the link i sent

#

please read the guide slowly

#

you seem to be reading it faster than actually processing what is there

fallen quest
#

yeah i read slowly but i don't know how can i use execute and data both

#

in the command help.js

coral ore
#

the usage is shown from the link i sent

fallen quest
#

but i don't findi read all slowly

fallen quest
#

yeah but i don't find how to use execute and data both

coral ore
#

that link quite literally shows you

#

this example from the guide uses both data and execute together


module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),
    async execute(interaction) {
        await interaction.reply('Pong!');
    },
};
fallen quest
#

i do everythigs as on the link

coral ore
#

but if you have the same error, you didn’t

fallen quest
#

i did just i can't reaload command idk why

coral ore
#

do you get the same error

fallen quest
#
[WARNING] The command at C:\Users\jeanb\Desktop\Bot Discord\Code\commands\utility\help.js is missing a required "data" or "execute" property.
Started refreshing 1 application (/) commands.
Error: Expected token to be set for this request, but none was present
    at _REST.resolveRequest (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1340:15)    at _REST.queueRequest (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1300:46)
    at _REST.request (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1266:33)
    at _REST.put (C:\Users\jeanb\Desktop\Bot Discord\Code\node_modules\@discordjs\rest\dist\index.js:1249:17)
    at C:\Users\jeanb\Desktop\Bot Discord\Code\deploy-commands.js:36:27
    at Object.<anonymous> (C:\Users\jeanb\Desktop\Bot Discord\Code\deploy-commands.js:46:3)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)```
#

yeah idk why

coral ore
#

please show your help command

fallen quest
#
const { ActionRowBuilder, StringSelectMenuBuilder, EmbedBuilder } = require('discord.js');
const fs = require('fs');

module.exports = {
    name: 'help',
    description: 'Displays a help menu with command categories.',
    async execute (interaction) {
        const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
        const categories = [];

        for (const file of commandFiles) {
            const category = file.split('.')[0];
            categories.push(category);
        }

        const selectMenuOptions = categories.map(category => ({
            label: category,
            value: category.toLowerCase()
        }));

        const selectMenu = new StringSelectMenuBuilder()
            .setCustomId('help-menu')
            .setPlaceholder('Select a category')
            .addOptions(selectMenuOptions);

        const row = new ActionRowBuilder().addComponents(selectMenu);

        const initialEmbed = new EmbedBuilder ()
            .setColor('#0099ff')
            .setTitle('Help Menu')
            .setDescription('Select a category to view commands.');

        await interaction.reply({ embeds: [initialEmbed], components: [row], ephemeral: true });
    },
};
coral ore
#

your code doesn’t match what the guide has

#

still missing the data property

#

so you didn’t follow anything i sent

fallen quest
#

i want to create category on the select menu automaticly with the folder name

coral ore
#

one issue at a time

#

if you’re not able to follow the guide, i’m not sure how to help

fallen quest
coral ore
#

so add the execute function?

fallen quest
#

but i don't know how to add execute and data at the same time

coral ore
#

i’m not going to repeat myself i’m sorry

#

i already showed you

#

numerous times

#

if this is too hard, i suggest learning a bit more javascript before starting to make a discord bot with discord.js.

you can look at #resources for a list of links to help you. the guide also has some listed as well

#

i’m sorry i can’t help you anymore than i have already

fallen quest
#

my bad

coral ore
#

feel free to use a translator and translate the guide into the language of your choosing

#

it’s okay, just hard to help someone that isn’t really doing anything after suggestions made

#

not trying to come off as harsh in any way