'this script, it's a code that I'm updating from V12 of discord.js to v13
I want to understand the reason for the error, so if it happens again, you can correct it, the following error appears whenever I use the !whitelist command (my english is very basic, sorry if there are too many spelling mistakes)
E:\Desktop\Bot\commands\whitelist.js:30
const whitelist = new Whitelist({
^
TypeError: Whitelist is not a constructor
This is the script where the command reports the error
const Whitelist = require("../classes/whitelist.class");
const config = require("../config/whitelist.config");
const moment = require("moment-timezone");
const usersCooldown = {};
const whitelists = {};
module.exports = {
run: async (client, message, args) => {
const userItsNotOnCooldown = (userId) => {
if (Array.isArray(usersCooldown[userId])) {
if (usersCooldown[userId].length >= config.maximumTries) {
const firstTryDate = usersCooldown[userId][0].date;
if (
Math.floor(Math.abs(new Date() - firstTryDate) / 1000 / 60) <=
config.cooldown
) {
return false;
}
delete usersCooldown[userId];
}
}
return true;
};
const userId = message.author.id;
if (typeof whitelists[userId] === "undefined") {
if (userItsNotOnCooldown(userId)) {
const whitelist = new Whitelist({
message,
client,
});
whitelist.on("finished", (whitelist) => {
delete whitelists[userId];
const data = {
whitelist,
date: new Date(),
};
// console.log(data) // @todo: log data into mongodb.
if (!data.passed) {
if (typeof usersCooldown[userId] === "undefined") {
usersCooldown[userId] = [data];
return;
}
usersCooldown[userId].push(data);
}
});
whitelists[userId] = whitelist;
} else {
message.reply(
`você atingiu o número máximo de tentativas, tente depois das: **${moment(
usersCooldown[userId][0].date
)
.add(config.cooldown, "minutes")
.tz("America/Sao_Paulo")
.format(`DD/MM/YYYY [-] HH:mm`)}**`
);
}
} else {
message.reply("você só pode fazer uma whitelist por vez!");
}
},
};
I will leave the main file if necessary
const Discord = require("discord.js");
const client = new Discord.Client({intents: 32767});
const leaveEvent = require('./events/leave.event');
const config = require('./config/whitelist.config');
client.on("ready", () => {
console.log(`BOT FUNCIONANDO NORMALMENTE NOME DO BOT : ${client.user.tag}!`)
})
client.on('messageCreate', message => {
if (message.author.bot) return;
if (message.channel.type == 'dm') return;
if (!message.content.toLowerCase().startsWith(config.prefix.toLowerCase())) return;
if (message.content.startsWith(`<@!${client.user.id}>`) || message.content.startsWith(`<@${client.user.id}>`)) return;
const args = message.content
.trim().slice(config.prefix.length)
.split(/ +/g);
const command = args.shift().toLowerCase();
try {
const commandFile = require(`./commands/${command}.js`)
commandFile.run(client, message, args);
} catch (err) {
console.error('Erro:' + err);
}
});
client.on("guildMemberRemove", (member) => {
leaveEvent(member)
});
client.login(config.discordClientId);
Solution 1:[1]
This issue doesn't have to do with updating from v12 to v13. .class
is probably not recognizable by require()
. Make sure the whitelist.class
file is a JavaScript file, and change its extension to .js
, then try require()
ing it.
The error is saying that Whitelist
doesn't refer to anything that JavaScript can read as a class constructor, which means that it's not a JavaScript class, or there's an issue with your importing/exporting.
Solution 2:[2]
You are not exporting a class from the whitelist.class.js
file. Please make sure that your file has a module.exports =
in it
It should look like this:
class Whitelist {
...
}
module.exports = Whitelist;
Or like this:
module.exports = class Whitelist {
...
};
Or like this:
module.exports = class {
...
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Wubzy |
Solution 2 | gear4 |