I have an issue which I really don't know how to solve. When I try to implement the change prefix command in my code I get an error when I want to use it. First the code was all in 1 file and it worked fine, but I splitted the file so that the commands have each a different file. This is where the code didn't work anymore. I'm sure it's because I'm not good in javascript but please help me!
This is the main file:
const Discord = require("discord.js");
const config = require("./config.json");
const client = new Discord.Client();
const newUsers = [];
const fs = require("fs")
// Error en warning berichten.
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
// This loop reads the /events/ folder and attaches each event file to the appropriate event.
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
let eventFunction = require(`./events/${file}`);
let eventName = file.split(".")[0];
// super-secret recipe to call events with all their proper arguments *after* the `client` var.
client.on(eventName, (...args) => eventFunction.run(client, ...args));
});
});
client.on("message", message => {
if (message.author.bot) return;
if(message.content.indexOf(config.prefix) !== 0) return;
// This is the best way to define args. Trust me.
const args = message.content.split(/\s+/g);
const command = args.shift().slice(config.prefix.length).toLowerCase();
// The list of if/else is replaced with those simple 2 lines:
try {
let commandFile = require(`./commands/${command}.js`);
commandFile.run(client, message, args);
} catch (err) {
console.error(err);
}
});
exports.run = (client, message, args) => {
if(!args || args.size < 1) return message.reply("Must provide a command name to reload.");
// the path is relative to the *current folder*, so just ./filename.js
delete require.cache[require.resolve(`./${args[0]}.js`)];
message.reply(`The command ${args[0]} has been reloaded`);
};
client.login(config.token);
This is the prefix file:
exports.run = (client, message, args) => {
const modRole = message.guild.roles.find("name", "Discord Admin");
if (!modRole)
return console.log("The Discord Admin role does not exist.");
if (!message.member.roles.has(modRole.id))
return message.reply("You can't use this command.");
// Verandert de prefix van de command (vb. "!prefix +" ontvangt de "+" dus word het "+prefix").
let newPrefix = message.content.split(" ").slice(1, 2)[0];
// Verandert de configuratie in memory
config.prefix = newPrefix;
// Slaat het bestand op.
fs.writeFile("./config.json", JSON.stringify(config), (err) => console.error);
}
And in the config file is: "prefix":"!"
I really tried a lot of things but I keep this error in node.js:
ReferenceError: config is not defined at Object.exports.run (C:\Users\fleur\Desktop\botdiscord\commands\prefix.js:12:7)