Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.js – TypeError: Cannot read properties of undefined (reading 'commands')

I’m coding a Discord Bot in JavaScript, but I have this error when I launch the bot !
I don’t find because I check the "application.commands" box!
Here’s the full error message:

I don’t know how I can resolve the problem 🙁

I try to invite my bot another time, but it doesn’t work 🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error message who makes problems :

node:events:491
      throw er; // Unhandled 'error' event
      ^
TypeError: Cannot read properties of undefined (reading 'commands')
    at Client.<anonymous> (/home/container/index.js:72:51)
    at Client.emit (node:events:513:28)
    at WebSocketManager.triggerClientReady (/home/container/node_modules/discord.js/src/client/websocket/WebSocketManager.js:385:17)
    at WebSocketManager.checkShardsReady (/home/container/node_modules/discord.js/src/client/websocket/WebSocketManager.js:368:10)
    at WebSocketShard.<anonymous> (/home/container/node_modules/discord.js/src/client/websocket/WebSocketManager.js:194:14)
    at WebSocketShard.emit (node:events:513:28)
    at WebSocketShard.checkReady (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:511:12)
    at WebSocketShard.onPacket (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:483:16)
    at WebSocketShard.onMessage (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:320:10)
    at WebSocket.onMessage (/home/container/node_modules/ws/lib/event-target.js:199:18)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
Node.js v18.12.0

The default code :

const { Client, GatewayIntentBits, Message, version, MessageFlags } = require("discord.js");
const { SlashCommandBuilder} = require("@discordjs/builders");
const { EmbedBuilder } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
})



//const { REST, Routes } = require('discord.js');
const {clientId, skorflexID, guildId, token, hostRole, multiplayerAnnouncementRole, fortniteLogo, rebootLogo,  basicFortniteImage, sept, un, quatre, trois, modoID } = require('./config.json');


const announce = new SlashCommandBuilder()
    .setName("announce")
    .setDescription("This command will send a embed message to announce your hosting !")

    .addStringOption(option => option
        .setName("version")
        .setDescription("Put the version of Fortnite you want to host")
        .setRequired(true))
    .addStringOption(option => option
        .setName("radmin_ip")
        .setDescription("Put your Radmin IP here (without the 'open')")
        .setRequired(true));


const clear = new SlashCommandBuilder()
    .setName("clear")
    .setDescription("This command will delete a number of messages in one second !")
    
    .addIntegerOption(option => option
        .setName("number")
        .setDescription("Put the number of messages who will be deleted !")
        .setRequired(true))


const say = new SlashCommandBuilder()
    .setName("say")
    .setDescription("The bot will say your message")

    .addStringOption(option => option
        .setName("message")
        .setDescription("Type your message !")
        .setRequired(true))


const thumbnail = new SlashCommandBuilder()
    .setName("thumbnail")
    .setDescription("The bot send you the thumbnail of a YouTube video")

    .addStringOption(option => option
        .setName("id")
        .setDescription("You can find the ID in the link after the << watch?v= >>")
        .setRequired(true))

    


client.on("ready", async () => {
    client.guilds.cache.get("1027595307187441664").commands.create(thumbnail);
    client.guilds.cache.get(guildId).commands.fetch();

    client.guilds.cache.get(guildId).commands.cache.map(command => {
        command.delete();
    });

    console.log("Le bot est lancé et prêt à l'utilisation !");
});



client.on("interactionCreate", interaction => {
    
    if(interaction.isCommand()){
        // announce ou announcement
        if(interaction.commandName === "announce" || interaction.commandName === "announcement"){
            let version1 = interaction.options.getString("version")
            let radmin_ip1 = interaction.options.getString("radmin_ip")
            let image = basicFortniteImage;

            if( version1 != undefined && radmin_ip1 != undefined ){

                if(version1 ==="7.30" || version1 ==="7.3" || version1 ==="7.2"|| version1 ==="7.1"|| version1 ==="7"){
                    image = sept;
                }else if(version1 == "1.7.2"){
                    image = un;
                }else if(version1 =="3.5"){
                    image = trois;
                }else if(version1 =="4.5"){
                    image = quatre;
                }

                const exampleEmbed = new EmbedBuilder()
                    .setColor(0xFFA3F8)
                    .setTitle("**Multiplayer Announcement**")
                    .setDescription("<@"+interaction.user.id+"> is hosting rn in **"+ version1 + "**\n\n - To join, write this in the fortnite console : \n\n> `open "+ radmin_ip1 +"`\n\n To open the fortnite console you need to press the ² button or if it's doesn't work press ` ")
                    .setImage(image)
                    .setFooter({ text: 'Hosted with Reboot Launcher', iconURL: rebootLogo})
                    .setThumbnail(fortniteLogo)
                    .setTimestamp()
                    .setFields({name: "To join", value: 'you need to be connected on **RADMIN VPN**'})
                
                if(interaction.member.roles.cache.some(e => e.id == hostRole)){
                    interaction.channel.send("<@&"+multiplayerAnnouncementRole+">")
                    interaction.reply({embeds: [exampleEmbed]});
                }else{
                    interaction.reply({ content: "You don't have the Host role !", ephemeral: true });
                }
            }
        }

        if(interaction.commandName === "clear"){
            var number = interaction.options.getInteger("number")
            if(interaction.member.roles.cache.some(e => e.id == modoID)){
                if(number >= 1 && number <= 100){
                    interaction.channel.bulkDelete(number);
                    interaction.reply({content: number + " messages has been deleted correctly !", ephemeral: true});
                }else{
                    interaction.reply({content: "To use the clear command, your number will be over 1 and under 100 ! Please retry", ephemeral: true});
                }
            }
        }
        
        if(interaction.commandName === "say"){
            let message2 = interaction.options.getString("message");
            
            
            if(interaction.member.roles.cache.some(e => e.id == modoID)){
                interaction.reply({content: "."})
                interaction.deleteReply();
                interaction.channel.send({content: message2})
            }else if(interaction.member.roles.cache.some(i => i.id != modoID)){
                interaction.reply({content: "You are not a moderator !", ephemeral: true})
            }
            
        }
        if(interaction.commandName === "thumbnail"){
            var ytID = interaction.options.getString("id");
            
            interaction.reply({content: "."})
            interaction.deleteReply();
            interaction.channel.send({content: "https://img.youtube.com/vi/"+ ytID +"/maxresdefault.jpg"})

        }

    }
})


client.login(token);

>Solution :

it’s referring to this line here:

client.guilds.cache.get("1027595307187441664").commands.create(thumbnail);

Basically client.guilds.cache.get("1027595307187441664") is undefined and did not return a result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading