#module resolve
1 messages · Page 1 of 1 (latest)
I'm pretty much out of ideas, can you show me your project layout like this
.
|-index.js
|-Structures
|-commands.js
|-Command.js
|-Client.js
is this above correct?
oh, is your Command.js one of those that look like this
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
from the discord js guide?
const Client = require("./Client");
const commands = require("../../Structures/Command.js")
/**
* @param {Discord.Message | Discord.Interaction} message
* @param {string[]} args
* @param {Client} client
*/
function RunFunction(message, args, client) {}
class Command {
/**
* @typedef {{name: string, description: string, run: RunFunction}} CommandOptions
* @param {CommandOptions} options
*/
constructor(options){
this.name = options.name;
this.description = options.description;
this.run = options.run;
}
}
module.exports = Command;
yes lol
it changed a bit since the last bot i worked on
its rlly outdated so im working on a new one
const Discord = require("discord.js");
const Client = require("./Client");
const commands = require("../../Structures/Command.js")
/**
* @param {Discord.Message | Discord.Interaction} message
* @param {string[]} args
* @param {Client} client
*/
function RunFunction(message, args, client) {}
class Command {
/**
* @typedef {{name: string, description: string, run: RunFunction}} CommandOptions
* @param {CommandOptions} options
*/
constructor(options){
this.name = options.name;
this.description = options.description;
this.run = options.run;
}
}
module.exports = Command;```
im stupid
ty for telling me that i didnt know
np, it's much nicer to look at
ofc :D
which file is it that I'm looking at?
command.js
i imported it like an idiot at that top
wait lemme remove it and see if it helps
wait the error changed
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module './Commands/Misc/hello.js'
Require stack:
- G:\Caffeine\src\Structures\Client.js
- G:\Caffeine\src\index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at G:\Caffeine\src\Structures\Client.js:32:55
at Array.map (<anonymous>)
at G:\Caffeine\src\Structures\Client.js:32:43
at Array.forEach (<anonymous>)
at Client.start (G:\Caffeine\src\Structures\Client.js:26:19)
at Object.<anonymous> (G:\Caffeine\src\index.js:13:8) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'G:\\Caffeine\\src\\Structures\\Client.js',
'G:\\Caffeine\\src\\index.js'
]
}```
fixed that issue xd
✅ Misc/hello
✅ Misc/ping
✅ undefined Event
G:\Caffeine\src\Structures\Client.js:49
this.on(event.event, event.run.bind(null, this));
^
TypeError: Cannot read properties of undefined (reading 'bind')
at G:\Caffeine\src\Structures\Client.js:49:48
at Array.forEach (<anonymous>)
at Client.start (G:\Caffeine\src\Structures\Client.js:43:14)
at Object.<anonymous> (G:\Caffeine\src\index.js:13:8)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47```
but an old issue popped up
hm
would have to see Client.js, what's this event object? it looks like you wanted your command
const Discord = require("discord.js");
const Command = require ("../Structures/Command");
const Event = require("./Event.js");
const config = require("../Data/config.json");
const intents = new Discord.Intents(32767);
const fs = require("fs");
class Client extends Discord.Client {
constructor() {
super({ intents });
/**
* @type {Discord.Collection<string, Command>}
*/
this.commands = new Discord.Collection();
this.prefix = config.prefix;
}
start(token) {
const directory = fs.readdirSync('./src/Commands')
directory.forEach(dir => {
const commandFiles = fs.readdirSync(`./src/Commands/${dir}`)
.filter(file => file.endsWith('.js'))
/**
* @type {Command[]}
*/
const commands = commandFiles.map(file => require(`../Commands/${dir}/${file}`));
commands.forEach(command => {
console.log(`✅ ${dir}/${command.name}`)
this.commands.set(command.name, command)
});
})
fs.readdirSync("./src/Events")
.filter(file => file.endsWith(".js"))
.forEach(file => {
/**
* @type {Event}
*/
const event = require(`../Events/${file}`);
console.log(`✅ ${event.event} Event`);
this.on(event.event, event.run.bind(null, this));
});
this.login(token);
}
}
module.exports = Client;```
I don't recommend extending Discord.Client, if for some reason they ever decide to add a start method, it just causes headaches for you
try logging the file at the .forEach(file, it's trying to require a file that doesn't exist
should i remove extends Discord.Client?
amd lemme change it
don't change that quite yet, let's fix your issue
okay so for .forEach what do you recommend me change
just log out the file, see what it's actually trying to require
im gonna sound stupid often as im still a novice but how do i do that xd
.forEach(file => {
/**
* @type {Event}
*/
console.log(file); // remove this after debugging
const event = require(`../Events/${file}`);
console.log(`✅ ${event.event} Event`);
this.on(event.event, event.run.bind(null, this));
});
nono, just the one dynamically importing events
✅ Misc/hello
✅ Misc/ping
messageCreate.js
✅ undefined Event
G:\Caffeine\src\Structures\Client.js:50
this.on(event.event, event.run.bind(null, this));
^
TypeError: Cannot read properties of undefined (reading 'bind')
at G:\Caffeine\src\Structures\Client.js:50:48
at Array.forEach (<anonymous>)
at Client.start (G:\Caffeine\src\Structures\Client.js:43:14)
at Object.<anonymous> (G:\Caffeine\src\index.js:13:8)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47```
does messageCreate.js export event and run?
const event = require("../Structures/Event.js");
module.exports = new Event("messageCreate", (client, message) => {
if(message.author.bot) return;
if (!message.content.startsWith(client.prefix)) return;
const args = message.content.substring(client.prefix.length).split(/ +/);
const command = client.commands.find(cmd => cmd.name == args[0]);
if (!command) return message.reply(`${args[0]} is not a valid command!`);
command.run(message, args, client);
});```
oh wow, I feel like I'm stepping into a whole new can of worms
it feels like you're maybe trying to do a little bit too much abstraction
you wrote new Event instead of new event
ooo lemme try
✅ Misc/hello
✅ Misc/ping
messageCreate.js
✅ messageCreate Event
ready.js
✅ undefined Event
G:\Caffeine\src\Structures\Client.js:50
this.on(event.event, event.run.bind(null, this));
^
TypeError: Cannot read properties of undefined (reading 'bind')
at G:\Caffeine\src\Structures\Client.js:50:48
at Array.forEach (<anonymous>)
at Client.start (G:\Caffeine\src\Structures\Client.js:43:14)
at Object.<anonymous> (G:\Caffeine\src\index.js:13:8)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47```
same thing in ready.js
IT WORKED OMG
lemme test commands
it worked omfg
ive been stuck on this for a while
thank you so much!
should we solve this though?
np, glad to have unblocked you
your constructor and other method don't really need to extend Discord.client, they can just be on their own
so just remove extends?
super({ intents });
^^^^^
SyntaxError: 'super' keyword unexpected here
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (G:\Caffeine\src\index.js:5:16)
at Module._compile (node:internal/modules/cjs/loader:1101:14)```
thats when i remove extends Discord.Client
yup, what I meant
it'll 100% be broken, get a refresher on oop in js and I think you can probably go with a service pattern
class UseService {
constructor(service1, service2, ...) {
this.service1 = service1;
this.service2 = service2;
...
}
dothing() {
this.service1.thing();
}
dothing2() {
this.service2.thing();
}
}
basically this is a gist of service pattern, your class constructor takes services as arguments, that methods will then use to do something.
is it bad that i dont understand that and i feel like i'd be in over my head if i attempted that and confuse myself completely
a little, most people writing discord bots don't have very strong cs fundamentals, it's hard to tell you where to go if I don't know where you're at. I try to help but I don't really know if sometimes things I'm saying is just going in one ear and out the other.
bah, it's to be expected, I suppose it's a start to programming, but I don't really like patterns recommended by the discord guide, but who am I but a humble developer writing a bot so I can kick my friends when they do stupid shit.
100%, im more than willing to understand, as much as i want to sit down an read a book on js and fully understand, i cant, my adhd just doesnt allow it. im taking classes in school to help but i dont have it until next semester. thats why talking to someone like you and having it explained helps alot more