So when i run my discord bot by using "npm run test" the error seems to be in the following line of code
const button = require(
../../components/${folder}/${file});
... And here's the issue:
PS C:\Users\itstr\Documents\discord bot> npm run test
> discord-bot@1.0.0 test
> node .
Command: button has passed through the handler
Command: embed has passed through the handler
Command: modal has passed through the handler
Command: ping has passed through the handler
Started refreshing application (/) commands.
C:\Users\itstr\Documents\discord bot\node_modules\discord.js\src\client\Client.js:492
throw new DiscordjsTypeError(ErrorCodes.ClientMissingIntents);
^
TypeError [ClientMissingIntents]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Users\itstr\Documents\discord bot\node_modules\discord.js\src\client\Client.js:492:13)
at new Client (C:\Users\itstr\Documents\discord bot\node_modules\discord.js\src\client\Client.js:78:10)
at Object.<anonymous> (C:\Users\itstr\Documents\discord bot\src\components\buttons\sub-yt.js:3:16)
at Module._compile (node:internal/modules/cjs/loader:1257:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1311:10)
at Module.load (node:internal/modules/cjs/loader:1115:32)
at Module._load (node:internal/modules/cjs/loader:962:12)
at Module.require (node:internal/modules/cjs/loader:1139:19)
at require (node:internal/modules/helpers:121:18)
at client.handleComponents (C:\Users\itstr\Documents\discord bot\src\functions\handler\handleComponents.js:16:28) {
code: 'ClientMissingIntents'
}
Node.js v20.3.1
And here's the code:
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { readdirSync } = require('fs');
module.exports = (client) => {
client.handleComponents = async () => {
const componentsFolder = readdirSync(`./src/components`);
for (const folder of componentsFolder) {
const componentsFiles = readdirSync(`./src/components/${folder}`).filter(file => file.endsWith('.js'));
const { buttons, modals } = client;
switch (folder) {
case "buttons":
for (const file of componentsFiles) {
const button = require(`../../components/${folder}/${file}`);
buttons.set(button.data.name, button);
}
break;
case "modals":
for (const file of componentsFiles) {
const modal = require(`../../components/${folder}/${file}`);
modals.set(modal.data.name, modal);
}
break;
default:
break;
}
}
}
};
And by the way i tried doing:
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS, // Required for basic bot functionality
Intents.FLAGS.GUILD_MESSAGES, // Required for message events
// Add more intents as per your bot's requirements
]
});
// Rest of your code...
client.login('YOUR_BOT_TOKEN');
And it didin't work, it only resulted with more errors.
I expect that i would learn of my past mistakes and that the community fixes it as soon as possible...