Search code examples
javascriptnode.jsdiscorddiscord.jsbots

[email protected] cant create channel


Ahhhhh I am going insane. I have asked many questions about creating channels, but i still dont manage to understand, wtf am i doing wrong???? This is the code i am using:

const Discord = require('discord.js');
const { Client, GatewayIntentBits, messageLink, InteractionResponseType, ChannelType } = require('discord.js');
const logger = require('winston');
const discord = require("discord.js");
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');
const bot = new Discord.Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildEmojisAndStickers,
        GatewayIntentBits.GuildMessageReactions,
    ],
});
//
const fs = require("node:fs");
const guild = bot.guilds.cache.get("1037081018500399154");
const path = require("node:path");
const token = "you tought"
//text
bot.on('ready', () => { // when the bot is ready and online
    console.log('bot is now online!')
    bot.user.setActivity("Servers");
});



  guild.channels.create({
    name: "hello",
    type: ChannelType.GuildText,
    // your permission overwrites or other options here
});

It throws this msg everytime i start my bot

Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'channels')

Pleas help me, i am going insane ngl. And yes, i have read the docs.

{Edit : There is usually more code, but i have just deleted the code as of now}


Solution

  • Put it in a ready event. It should be like this:

    bot.on("ready" , () => {
        const guild = bot.guilds.cache.get("1037081018500399154");
        guild.channels.create({
            name: "hello",
            type: ChannelType.GuildText,
            // your permission overwrites or other options here
        });
    });
    

    I hope to be of help to you.