How can I make this work – client already declared

Advertisements

CODE:

require("dotenv").config();
const { token } = process.env;
const {client, collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");

const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new collection();
client.commandArray = [];

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync('./src/functions/${folder}')
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles)
    require(`./functions/${folder}/${file}`)(client);
}

client.handleEvents();
client.handleCommands();
client.login(token);

ERROR after running npm run test:

**const client = new Client({ intents: GatewayIntentBits.Guilds });
      ^

SyntaxError: Identifier 'client' has already been declared**
    at Object.compileFunction (node:vm:360:18)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

I have tried deleting line 3 as Client is already handled at line 6 but that throws a different error also.

Can someone please advise?

I have tried deleting line 3 as Client is already handled at line 6 but that throws a different error also.

Tried removing "client" from line 3

>Solution :

You need to import Client not client

try like this

require("dotenv").config();
const { token } = process.env;
const {Client, collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");

const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new collection();
client.commandArray = [];

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync('./src/functions/${folder}')
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles)
    require(`./functions/${folder}/${file}`)(client);
}

client.handleEvents();
client.handleCommands();
client.login(token);


Leave a Reply Cancel reply