'Discord bot isn't working using mac and discord.js getting several errors

so I followed a tutorial and got this code. If i try to run it via terminal it gives me this error: I installed all the npm things and node and stuff so that's all fine, but I don't understand what I need to edit for it to work I used this github: https://github.com/discordjs/discord.js Thanks for your reaction

Error:

ReferenceError: CLIENT_ID is not defined
    at /Users/mark/Desktop/Robert Wielinga/index.js:17:50
    at Object.<anonymous> (/Users/mark/Desktop/Robert Wielinga/index.js:23:3)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47
/Users/mark/Desktop/Robert Wielinga/index.js:25
client.login('--- token ---');
^

ReferenceError: Cannot access 'client' before initialization
    at Object.<anonymous> (/Users/mark/Desktop/Robert Wielinga/index.js:25:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47
mark@Air-van-Mark Robert Wielinga % node index.js
Started refreshing application (/) commands.
ReferenceError: GUILD_ID is not defined
    at /Users/mark/Desktop/Robert Wielinga/index.js:17:70
    at Object.<anonymous> (/Users/mark/Desktop/Robert Wielinga/index.js:23:3)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47
/Users/mark/Desktop/Robert Wielinga/index.js:28
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
                                                        ^

TypeError: Cannot read properties of undefined (reading 'Guilds')
    at Object.<anonymous> (/Users/mark/Desktop/Robert Wielinga/index.js:28:57)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47
mark@Air-van-Mark Robert Wielinga % node index.js
Started refreshing application (/) commands.
ReferenceError: GUILD_ID is not defined
    at /Users/mark/Desktop/Robert Wielinga/index.js:17:70
    at Object.<anonymous> (/Users/mark/Desktop/Robert Wielinga/index.js:23:3)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47
/Users/mark/Desktop/Robert Wielinga/index.js:28
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
                                                        ^

TypeError: Cannot read properties of undefined (reading 'Guilds')
    at Object.<anonymous> (/Users/mark/Desktop/Robert Wielinga/index.js:28:57)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47
mark@Air-van-Mark Robert Wielinga % 

Code:

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

const commands = [
    {
        name: 'ping',
        description: 'Replies with Pong!',
    },
];

const rest = new REST({ version: '9' }).setToken('token');

(async () => {
    try {
        console.log('Started refreshing application (/) commands.');

        await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: commands });

        console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
        console.error(error);
    }
})();



const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.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('my token');


Solution 1:[1]

It's simple! There are a few errors with different explanations for each:

  • ReferenceError: CLIENT_ID is not defined: This error is caused because there is nothing with the name of CLIENT_ID in the code. Presuming that you are using the dotenv module, then all you have to do is add require('dotenv').config() in the start and then use process.env.CLIENT_ID wherever you need it.
  • TypeError: Cannot read properties of undefined (reading 'Guilds'): This error is caused because there is nothing called as GatewayIntentBits in discord.js. A better way to provide the intents would be to require Intents in discord.js by using const { Intents, Client } = require('discord.js') and then const client = new Client({intents: [Intents.FLAGS.GUILDS]})
  • ReferenceError: GUILD_ID is not defined: This error is similar to the first error so all you have to do is type require('dotenv').config() in the top and then process.env.GUILD_ID when you use it

Hope this solved your question!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caladan