skip to Main Content

ReferenceError: Discord is not defined
at Object.<anonymous> (/home/runner/testing-a-bot/index.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1159:14)

I was attempting to create a discord bot, this error has popped up.
All of the code was in the below youtube video

(https://www.youtube.com/watch?v=8V7iTweKKM8)

My code for this:

const {Client, GatewayIntentBits, EmbedBuilder, PermissionsBitField, Permissions} =       require('discord.js');
const client = new Discord.Client();

client.on("ready", () => {
console.log("It's ready")
})

client.login(assume the token is here)

I ran the code, this popped up.
Just attempting to get the bot online and run basic code.

This problem isn’t a duplicate, as far as I can tell the old posts don’t work due to the age.

2

Answers


  1. You imported the Client as destructured member from discord.js, so when you want to use it, you can do it directly;

    const {Client, GatewayIntentBits, EmbedBuilder, PermissionsBitField, Permissions} = require('discord.js');
    const client = new Client();
    

    If you want your code to work; you should import the whole discord.js library as Discord;

    const Discord = require('discord.js');
    const client = new Discord.Client();
    

    Follow-up

    Discord bots need intents, you need to declare them when initializing a new client;

    const { Client, GatewayIntentBits } = require('discord.js');
    
    const client = new Client({
        intents: [
            GatewayIntentBits.Guilds,
            GatewayIntentBits.GuildMessages,
            GatewayIntentBits.MessageContent,
            GatewayIntentBits.GuildMembers,
            // Add more intents as needed
        ],
    });
    

    You can check this out in the official documentation.

    Login or Signup to reply.
  2. For the error message you are receiving

    TypeError [ClientMissingIntents]: Valid intents must be provided for the Client

    It means that you need a valid Intents to be able to Create a new client instance. For example:

    // Create a new client instance
    const client = new Client({ intents: [GatewayIntentBits.Guilds] });
    

    Check out these docs:
    https://discordjs.guide/creating-your-bot/main-file.html#running-your-application

    For the list of intents:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search