Some of my code:
const fs = require(‘fs’);
const Discord = require(‘discord.js’);
// create a new Discord client
const config = require(’./config.json’);
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync(’./commands’).filter(file => file.endsWith(’.js’));
for (const file of commandFiles) {
const command = require(./commands/${file}
);
client.commands.set(command.name, command);
}
Could you tell me what to do?
I tried making it a file but it doesn’t let me do just “commands”.
It doesn’t let me make a file without .____
Try using __dirname + "/commands"
.
Where? @khalby786 oooooooooooooooooooooookk.
Wow just today I started having this EXACT same issue with my ./commands folder and readdirsync on discord.js. I haven’t been able to fix it and I’ve been trying for hours. Manage to find anything?
Do u have a folder named commands
?
If yes try fs.readdir(..)
instead of fs.reaadirSync(..)
.
fs.readdir
and fs.readdirSync
has no difference except that the former needs a callback function passed which might make things more complicated.
@khalby786 So what should I do
P.s. I made the file commands.js
? @khalby786
Could you tell me what to do?
try something like this
fs.readdir("./commands/", (err, files) => { //make sure you have a commands folder
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return; //ignore non js files
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
client.commands.set(commandName, props);
});
});
Try doing ../commands
, generally works for me.
@Joe-Biondani using ../commands
will look for a folder named commands
in the parent directory and not the same directory.
@DaRealMiniToon try this:
const fs = require(‘fs’);
const Discord = require(‘discord.js’);
// create a new Discord client
const config = require(’./config.json’);
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync(__dirname + ’/commands’).filter(file => file.endsWith(’.js’));
for (const file of commandFiles) {
const command = require(__dirname + `/commands/${file}`);
client.commands.set(command.name, command);
}
thanks ghostoblivion it works
but turns out my program is really dumb since I’m not that good at discord.js
Syntax error: await is only valid in an async function.
My code
const { Users, CurrencyShop } = require(’./dbObjects’);
const { Op } = require(‘sequelize’);
const currency = new Discord.Collection();
const storedBalances = await Users.findAll();
storedBalances.forEach(b => currency.set(b.user_id, b));
Reflect.defineProperty(currency, ‘add’, {
value: async function add(id, amount) {
const user = currency.get(id);
if (user) {
user.balance += Number(amount);
return user.save();
}
const newUser = await Users.create({ user_id: id, balance: amount });
currency.set(id, newUser);
return newUser;
},
});
Reflect.defineProperty(currency, ‘getBalance’, {
value: function getBalance(id) {
const user = currency.get(id);
return user ? user.balance : 0;
},
});
fs.readdir("./commands/", (err, files) => { //make sure you have a commands folder
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return; //ignore non js files
let props = require(./commands/${file}
);
let commandName = file.split(".")[0];
client.commands.set(commandName, props);
});
});
// when the client is ready, run this code
sorry someone else got it ;-;
maybe you can beat him to this :
Syntax error: await is only valid in an async function.
My code
const { Users, CurrencyShop } = require(’./dbObjects’);
const { Op } = require(‘sequelize’);
const currency = new Discord.Collection();
const storedBalances = await Users.findAll();
storedBalances.forEach(b => currency.set(b.user_id, b));
Reflect.defineProperty(currency, ‘add’, {
value: async function add(id, amount) {
const user = currency.get(id);
if (user) {
user.balance += Number(amount);
return user.save();
}
const newUser = await Users.create({ user_id: id, balance: amount });
currency.set(id, newUser);
return newUser;
},
});
Reflect.defineProperty(currency, ‘getBalance’, {
value: function getBalance(id) {
const user = currency.get(id);
return user ? user.balance : 0;
},
});
fs.readdir("./commands/", (err, files) => { //make sure you have a commands folder
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return; //ignore non js files
let props = require( ./commands/${file}
);
let commandName = file.split(".")[0];
client.commands.set(commandName, props);
});
});
// when the client is ready, run this code
First, I formatted your code for better readability:
const { Users, CurrencyShop } = require("./dbObjects");
const { Op } = require("sequelize");
const currency = new Discord.Collection();
const storedBalances = await Users.findAll();
storedBalances.forEach(b => currency.set(b.user_id, b));
Reflect.defineProperty(currency, "add", {
value: async function add(id, amount) {
const user = currency.get(id);
if (user) {
user.balance += Number(amount);
return user.save();
}
const newUser = await Users.create({ user_id: id, balance: amount });
currency.set(id, newUser);
return newUser;
}
});
Reflect.defineProperty(currency, "getBalance", {
value: function getBalance(id) {
const user = currency.get(id);
return user ? user.balance : 0;
}
});
fs.readdir("./commands/", (err, files) => {
//make sure you have a commands folder
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return; //ignore non js files
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
client.commands.set(commandName, props);
});
});
// when the client is ready, run this code
Now the problem lies on line 4, where you are attempting to use await
outside an async
function. Try removing the await
keyword from await Users.findAll()
on line 4.
alright i did that , removed all the “await”'s out of my program that dont have async function
but now , this happens.
throw new Error(Please install ${moduleName} package manually
);
^
Error: Please install sqlite3 package manually
at ConnectionManager._loadDialectModule (c:\Users\Aaron\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:88:15)
at new ConnectionManager (c:\Users\Aaron\node_modules\sequelize\lib\dialects\sqlite\connection-manager.js:22:21)
at new SqliteDialect (c:\Users\Aaron\node_modules\sequelize\lib\dialects\sqlite\index.js:14:30)
at new Sequelize (c:\Users\Aaron\node_modules\sequelize\lib\sequelize.js:324:20)
at Object. (c:\Users\Aaron\Desktop\Discord Bot\node_modules\dbObjects.js:3:19)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
at Object. (c:\Users\Aaron\Desktop\Discord Bot\node_modules\index.js:6:33)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
But , when I tried installing that software on node.js command prompt , a bunch of errors pop up!
@Ghostoblivion @khalby786
npm i sqlite3
@DaRealMiniToon ^^^^
@khalby786 A bunch of errors still show up.