Home > Software design >  Discord.js returns an undefined error when creating a Client
Discord.js returns an undefined error when creating a Client

Time:10-27

I followed the example on the official discord.js guide and it gave the code:

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

// Login to Discord with your client's token
client.login(token);

But upon running it gave this error:

D:\DiscCoin\node_modules\discord.js\src\managers\GuildManager.js:39
    if (!cacheWarningEmitted && this._cache.constructor.name !== 'Collection') {
                                                        ^

TypeError: Cannot read properties of undefined (reading 'name')
    at new GuildManager (D:\DiscCoin\node_modules\discord.js\src\managers\GuildManager.js:39:57)
    at new Client (D:\DiscCoin\node_modules\discord.js\src\client\Client.js:127:19)
    at Object.<anonymous> (D:\DiscCoin\index.js:6:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

I have not done anything besides follow the official guide. What could I be doing wrong?

CodePudding user response:

I have the same problem.

My error is

/Users/someone/Documents/code/Javascript code/Bot code/node_modules/discord.js/src/managers/GuildManager.js:34
    if (!cacheWarningEmitted && this._cache.constructor.name !== 'Collection') {
                                                        ^

TypeError: Cannot read properties of undefined (reading 'name')
    at new GuildManager (/Users/someone/Documents/code/Javascript code/Bot code/node_modules/discord.js/src/managers/GuildManager.js:34:57)
    at new Client (/Users/someone/Documents/code/Javascript code/Bot code/node_modules/discord.js/src/client/Client.js:127:19)
    at Object.<anonymous> (/Users/someone/Documents/code/Javascript code/Bot code/test.js:2:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

My code is

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
  }
});

client.login('Token');

Except token ^^^ is my bot token. Discord.js

CodePudding user response:

Try running this:

npm i @discordjs/collection

  • Related