#readdir

1 messages · Page 1 of 1 (latest)

fast bramble
#

Is this a JS or TS project?

gray sigil
#

Js

fast bramble
#

Why's there a src directory then? Curious. No matter..

gray sigil
#

Its for my bot? the commands, handlers, main bot file. All are in the src. Events included

fast bramble
#

Okay. Have you tried just not having . or / there at all?
src/events/${dir}?

gray sigil
#

no

#

I don't think it will work without but ill try

fast bramble
#

Where is the main index file for the project?

gray sigil
#

No it doesnt

fast bramble
#

The file upon which you invoke node.

gray sigil
fast bramble
#

Okay. So all file access is relative to the src directory.
Your path would thus be looking for...

src/src/events/${dir}

#

Drop the src, just put events/${dir}

gray sigil
#

I tried ./events too but it still didn't work

#

and .events

gray sigil
#

Didn't work

fast bramble
#

What's the error?

gray sigil
#

Error: ENOENT: no such file or directory, scandir 'events/client'

fast bramble
#

Then I don't trust that what you've claimed is entirely accurate before.

gray sigil
#

     at Object.readdirSync (node:fs:1390:3)

     at load_dir (/app/src/handlers/event_handler.js:7:32)

     at /app/src/handlers/event_handler.js:15:38

     at Array.forEach (<anonymous>)

     at module.exports (/app/src/handlers/event_handler.js:15:25)

     at /app/src/bot.js:19:37

     at Array.forEach (<anonymous>)

     at Object.<anonymous> (/app/src/bot.js:18:38)

     at Module._compile (node:internal/modules/cjs/loader:1101:14)

     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {

   errno: -2,

   syscall: 'scandir',

   code: 'ENOENT',

   path: 'events/client'
 }```
#

Full error

gray sigil
#

C:\Users\Therogamer\Documents\Coding-Projects\Discord-Bot\src <- Full Path

fast bramble
#

This claimed /app/src not.. whatever path that is.
Which already raises some red flags to me.

gray sigil
#

Yes because im pushing it to git then heroku because it is hosted in heroku

#

app is basically Discord-bot from my path

fast bramble
#

Does it work when running on the local machine?

gray sigil
#

Same error

#

I tried every combination of that path. I put path.resolve in it too and it gave the same error

fast bramble
#

Alrighty, test locally for the moment.

#

My suggestion for the moment is to try a path relative to your index file.

const path = require("path");

path.join( __dirname, `events/${dir}`)
gray sigil
#

const event_files = fs.readdirSync(path.join( __dirname, `events/${dir}`)).filter(file => file.endsWith('.js')); ?

fast bramble
#

Yeah. Since I can't seem to figure out what your working directory even is, I'm skipping past that and going entirely relative.

gray sigil
#

Error: ENOENT: no such file or directory, scandir 'C:\Users\Therogamer\Documents\Coding-Projects\discord-bot\src\handlers\events\client'

fast bramble
#

Ah, you're inside a handler.

gray sigil
#

I think its trying to read from inside the handlers folder

fast bramble
#

That's fine, ../events/${dir}

gray sigil
fast bramble
#

Just the addition of ../

gray sigil
#

Ok

fast bramble
#

And if this fails then you're a filthy liar :p

gray sigil
#

Error: Cannot find module './events/client/ready.js

#

Ok

fast bramble
#

.. not .

gray sigil
#

It is ..

fast bramble
#

Ah. Hm.

gray sigil
#

In the error its .

#

Idk why

#

const event_files = fs.readdirSync(path.join( __dirname, `../events/${dir}`)).filter(file => file.endsWith('.js'));

fast bramble
#

It's reading from the correct path, but your ready.js isn't exporting?

gray sigil
#

I think so

fast bramble
#

Since the error is no longer that there is no file, but that no module could be found.

gray sigil
#

Oop

#

found it

#

The second one didnt have .. before

#

Ok now it works

#

And another thing doesn't

#

setactivity doesn't work because its undefined. I don't know how to declare the client so it works. My attempts at declaring it didn't work

#

Well thanks for you help @fast bramble . Ill ask about the setactivity in the main channel.

fast bramble
#

Your issue is module handling. You want your event modules to return a function that accepts the Client as a parameter.

e.g.

module.exports = function(client) {
  // do stuff with client here
}

Then when you first require in the file, call the resultant module value and call it with your Client object.

const event = require(modulePath);
event(client);

It's just one example, there are other variations of this including returning a class or an object with an init function.

gray sigil
#

modulePath would be?

fast bramble
#

The path of the file you're trying to load.

#

Oh, you've probably not got that far..bleh.

gray sigil
#

Im at the first client event which is the ready.js

#

setactivity is undefined and im trying to figure out how to declare the client so it works

fast bramble
#

I assumed the earlier code was to add ready.js to your project.

#

So where is that specific piece of code?

gray sigil
#

You mean the event_handler?

fast bramble
gray sigil
#
const path = require('path');
const fs = require('fs');

module.exports = (client, Discord) =>{
    const load_dir = (dir) =>{
        const event_files = fs.readdirSync(path.join( __dirname, `../events/${dir}`)).filter(file => file.endsWith('.js'));

        for(const file of event_files){
            const event = require(`../events/${dir}/${file}`);
            const event_name = file.split('.')[0];
            client.on(event_name, event.bind(null, Discord, client));
        }
    }
    ['client', 'guild'].forEach(e => load_dir(e));
}```
fast bramble
#

Hm.

#

And the ready.js file?

gray sigil
#
module.exports = (client, args, Discord) =>{
    console.log('Bot Online.');
    client.user.setActivity('Made by Therogamer. | *help');
}```
Just this for now
fast bramble
#

So your issue here is that what you're passing in does not match what you're expecting.

#

event.bind(null, Discord, client)
module.exports=(client, args, Discord)

gray sigil
#

event.bind comes where?

#

oh i see

#

Im blind lmao

fast bramble
#

Honestly I doubt you have any reason to pass the Discord object into the event, with it being global.

gray sigil
#

ready.js works now but message.js doesn't

#
    const prefix = '*';
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const cnd = args.shift().toLowerCase();
    
    const command = client.commands.get(cnd);

    if(command) command.execute(client, message, args, Discord);
}```
this is it
#

Its in

fast bramble
#

What's your bind command now?

gray sigil
#

event/guild

#

event.bind(Discord, client))

#

i should add message?

fast bramble
#

It should be event.bind(null, Discord, client)

gray sigil
#

thats what i deleted to make ready.js work

fast bramble
gray sigil
#

the null

fast bramble
# gray sigil the null

The first parameter of bind is not passed into the argument list, but is what the this value is assigned to on call.

#

It's why I had it struck out in my previous code.

gray sigil
#

Now message.js and ready.js works but a command that contains embed doesnt work

fast bramble
#

Is its parameter list correct?

gray sigil
#
const Discord = require('discord.js');
const { MessageEmbed } = require('discord.js');
module.exports = {
    name: 'version',
    description: 'Tells the bots version!',
    execute(Discord, message, args, client) {
        const Version = new Discord.MessageEmbed()
           .setColor('#4A235A')
           .setTitle("Bot Version")
           .setDescription('The bot is currently on version: ' + bv);
           message.channel.send({embeds: [ Version ]})
    }
}```
TypeError: Discord.MessageEmbed is not a constructor
fast bramble
#

This doesn't follow the same structure as the previous code.
It returns an object instead of a function, so clearly you're handling this with a different piece of code.

#

I've not seen that code and so I'm unable to assist.

gray sigil
#

Yes sorry

#

Commands are handled by command_handler

#

module.exports = (client, Discord) =>{
    const command_files = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js'));

    for(const file of command_files){
        const command = require(`../commands/${file}`);
        if(command.name){
            client.commands.set(command.name, command);
        } else {
              continue;
        }
    }
}```
fast bramble
#

Additionally, you're using new Discord.MessageEmbed() despite the fact you now have a global, MessageEmbed higher up.

gray sigil
fast bramble
gray sigil
#

Oh i see but thats how i did every embed and it worked until now

#

I guess it won't work now

#

How do i need to do it now? I did it like that and it worked

#

Oh i figured it out