Discord bot help

require('dotenv').config();

const {REST} = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Intents, Collection } = require('discord.js');

const fs = require('fs');
const path = require('path');


const client = new Client({
    intents: [Intents.Guilds,  Intents.Guilds,GuildMessages]
});

// List of all commands
const commands = [];
client.commands = new Collection();

const commandsPath = path.join(__dirname, "commands"); // E:\yt\discord bot\js\intro\commands
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for(const file of commandFiles)
{
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);

    client.commands.set(command.data.name, command);
    commands.push(command.data.toJSON());
}

client.on("ready", () => {
    // Get all ids of the servers
    const guild_ids = client.guilds.cache.map(guild => guild.id);


    const rest = new REST({version: '9'}).setToken(process.env.TOKEN);
    for (const guildId of guild_ids)
    {
        rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId), 
            {body: commands})
        .then(() => console.log('Successfully updated commands for guild ' + guildId))
        .catch(console.error);
    }
});

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

    const command = client.commands.get(interaction.commandName);
    if(!command) return;

    try
    {
        await command.execute(interaction);
    }
    catch(error)
    {
        console.error(error);
        await interaction.reply({content: "There was an error executing this command"});
    }
});

client.login(process.env.TOKEN);

when i run node . it shows

TypeError: Cannot read properties of undefined (reading 'Guilds')
    at Object.<anonymous> (C:\Users\andre\Documents\Desktop\bots\index.js:12:23)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

It’s because you named (or tried to run) it with a dot (.) and it doesn’t understand what file it needs to ‘node’

Like this:
node thing or thing.js

Instead of:
node . or node …js


This was a :ghost: deleted message.

2 Likes

Try doing this and see the outcome.

That does not matter, it’s about the Guilds. Look in the script more.

1 Like

It’s undefined. It can’t find it.


This was a :ghost: deleted message.

node . is how you run your code in JavaScript.

Do you still have issues with this to this day?

yes but i quit this project :slight_smile:

Well I made a discord bot and tutorials told me to use node [name].js and not just . On terminal

On my new computer I’ve been trying to install js and its pain, I prefer python, so much easier.

This topic was automatically closed after 7 days. New replies are no longer allowed.