Home > OS >  Cannot read properties of undefined (reading 'on') client.on("ready", () =>
Cannot read properties of undefined (reading 'on') client.on("ready", () =>

Time:09-01

I am coding a Discord bot and found an issue I cannot fix.

checking if the bot was online to send a message in the console and a rich presence

client.on("ready", () => {
  console.log(`${client.user.username} is in the house!`);
  client.user.setPresence({
    status: 'online',
    game: {
      name: 'Cool kid the movie 2',
      type: 'WATCHING'
    }
  });
});

All the code

const { MessageHandler } = require("discord-message-handler"); // npm install discord-message-handler //
const handler = new MessageHandler();
const discord = require("discord.js"); // npm i discord.js //
const { Options } = require("discord.js");
const { GatewayIntentBits } = require("discord.js");
var logger = require("winston"); // npm install winston //
const { client, Collection } = require("discord.js");
const { config } = require("dotenv");
const Client = {
  disableEveryone: true,
};

// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console(), {
  colorize: true,
});
logger.level = "debug";

// Initialize Discord Bot
var bot = new discord.Client({
  autorun: true,
});

client.on("ready", () => {
  console.log(`${client.user.username} is in the house!`);
  client.user.setPresence({
    status: "online",
    game: {
      name: "Cool kid the movie 2",
      type: "WATCHING",
    },
  });
});

client.login(process.env.TOKEN);

CodePudding user response:

you have to mention client in the function you created and also you defined your Discord Bot as bot so code will be

bot.on("ready", (client) => {
  console.log(`${client.user.username} is in the house!`);
  client.user.setPresence({
    status: 'online',
    game: {
      name: 'Cool kid the movie 2',
      type: 'WATCHING'
    }
  });
});

CodePudding user response:

here is an example code

const {
  Client,
  Partials,
  Collection,
  PermissionFlagsBits,
  EmbedBuilder,
} = require("discord.js");
const { config } = require("");// enter your path
const { user, Message, GuildMember} =
  Partials;

const client = new Client({
  intents: 131071,
  Partials: [
    user,
    Message,
    GuildMember,
    PermissionFlagsBits
  ],
  allowedMentions: { parse: ["everyone", "roles", "users"] }
});

client.login(//token path).catch((err) => console.log(err));

client.on("ready", (client) => {
  client.user.setPresence({
    status: "online",
    game: {
      name: 'Cool kid the movie 2',
      type: 'WATCHING'
    }
  })
});


  • Related