#readdir
1 messages · Page 1 of 1 (latest)
Js
Why's there a src directory then? Curious. No matter..
Its for my bot? the commands, handlers, main bot file. All are in the src. Events included
Okay. Have you tried just not having . or / there at all?
src/events/${dir}?
Where is the main index file for the project?
No it doesnt
The file upon which you invoke node.
in the src folder
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}
What's the error?
Error: ENOENT: no such file or directory, scandir 'events/client'
Then I don't trust that what you've claimed is entirely accurate before.
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
It is true look at the error
C:\Users\Therogamer\Documents\Coding-Projects\Discord-Bot\src <- Full Path
This claimed /app/src not.. whatever path that is.
Which already raises some red flags to me.
Yes because im pushing it to git then heroku because it is hosted in heroku
app is basically Discord-bot from my path
Does it work when running on the local machine?
Same error
I tried every combination of that path. I put path.resolve in it too and it gave the same error
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}`)
const event_files = fs.readdirSync(path.join( __dirname, `events/${dir}`)).filter(file => file.endsWith('.js')); ?
Yeah. Since I can't seem to figure out what your working directory even is, I'm skipping past that and going entirely relative.
Error: ENOENT: no such file or directory, scandir 'C:\Users\Therogamer\Documents\Coding-Projects\discord-bot\src\handlers\events\client'
Ah, you're inside a handler.
I think its trying to read from inside the handlers folder
That's fine, ../events/${dir}
Just that should i remove the path.join part?
Just the addition of ../
Ok
And if this fails then you're a filthy liar :p
.. not .
It is ..
Ah. Hm.
In the error its .
Idk why
const event_files = fs.readdirSync(path.join( __dirname, `../events/${dir}`)).filter(file => file.endsWith('.js'));
It's reading from the correct path, but your ready.js isn't exporting?
I think so
Since the error is no longer that there is no file, but that no module could be found.
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.
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.
modulePath would be?
The path of the file you're trying to load.
Oh, you've probably not got that far..bleh.
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
I assumed the earlier code was to add ready.js to your project.
So where is that specific piece of code?
You mean the event_handler?
You have this here.
I assume you then went on to use event_files.
I would like to see more of the code.
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));
}```
module.exports = (client, args, Discord) =>{
console.log('Bot Online.');
client.user.setActivity('Made by Therogamer. | *help');
}```
Just this for now
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)
Honestly I doubt you have any reason to pass the Discord object into the event, with it being global.
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
What's your bind command now?
It should be event.bind(null, Discord, client)
thats what i deleted to make ready.js work
And this should be
module.exports = (Discord, client)
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.
Now message.js and ready.js works but a command that contains embed doesnt work
Is its parameter list correct?
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
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.
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;
}
}
}```
Additionally, you're using new Discord.MessageEmbed() despite the fact you now have a global, MessageEmbed higher up.
Im blind can you tell me where it is?