Create Channel discord.js

86 views Asked by At

I need help, sending a message from discord with my bot.js I can create it but I need to do this from a function:

exports('runJS', (data) => {
    console.log(data.name);
    
    client.channels.cache.get(`ID`).send(data.name);
    
    let role = client.guilds.cache.get("id"); //member.roles.cache.has
    
    if (data != null) {
        client.guild.channels.create("││" + data.name, { 
            type: "GUILD_TEXT", // syntax has changed a bit
            parent: "1157076981159034880",
            permissionOverwrites: [
                {
                    id: role.id,
                    allow: [Permissions.FLAGS.VIEW_CHANNEL],
                },
                {
                    id: data.id,
                    allow: [Permissions.FLAGS.VIEW_CHANNEL],
                },
                {
                    id: client.guild.roles.everyone, //To make it be seen by a certain role, user an ID instead
                    //allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'], //Allow permissions
                    deny: ['VIEW_CHANNEL'] //Deny permissions , 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'
                },
            ],
        });     
    }
});

I don't know if I was very clear, but this function receives the values that I need to create the channel, this is the one that works for me with a command in the bot:

I tried replacing message.guild.channels.create with client.guild.channels.create but it didn't work, I get an error like guild is not a function or does not exist.

1

There are 1 answers

0
RogueMan On

You are trying to get the guild by using client.guild but guild does not exist on client.

Looking at your code, I assume that data is the received interaction.

To create a channel using this interaction object you can do the following:

data.guild.channels.create("││" + data.user.username, { 
        type: "GUILD_TEXT", // syntax has changed a bit
        parent: "1157076981159034880",
        permissionOverwrites: [
            {
                id: role.id,
                allow: [Permissions.FLAGS.VIEW_CHANNEL],
            },
            {
                id: data.id,
                allow: [Permissions.FLAGS.VIEW_CHANNEL],
            },
            {
                id: client.guild.roles.everyone, //To make it be seen by a certain role, user an ID instead
                //allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'], //Allow permissions
                deny: ['VIEW_CHANNEL'] //Deny permissions , 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'
            },
        ],
    }); 

I hope this answers your question