1
votes

I am trying to do a discord bot that creates channels among others, but I can't achieve it because my code it's supposed to create a channel but... I tried a lot of things, these are the two that haven't given me error: (I substituted the bot's token with DONOTLOOK, I won't have problems...)

OPTION 1:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.login('DONOTLOOK');

bot.on('ready', () => {
  console.log('Logged in as ${bot.user.tag}!');
});

bot.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
    var server = msg.guild;
    bot.channels.add("anewchannel", {type: 0});
  }
});

OPTION 2:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.login('DONOTLOOK');

bot.on('ready', () => {
  console.log(`Logged in as ${bot.user.tag}!`);
});

bot.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
    var server = msg.guild;
    const channel = bot.channels.add("newchannel", {type: 0});
  }
});

That don't gave me any node.js error on console, the bot answered me but haven't created the channel. I also looked two references(https://discord.com/developers/docs/resources/guild#create-guild-channel, https://discord.js.org/#/docs/main/stable/class/GuildChannelManager?scrollTo=create), no one's solutions worked, the discord.com one gave me node.js error too. The two options above are taken from the discord.js file, they gave no node.js errors but doesn't create the channel. If someone can help me please.

2

2 Answers

3
votes

The code below explains how to create a text channel from a command.

bot.on('message', msg => { //Message event listener
    if (msg.content === 'channel') { //If the message contained the command
        message.guild.channels.create('Text', { //Create a channel
            type: 'text', //Make sure the channel is a text channel
            permissionOverwrites: [{ //Set permission overwrites
                id: message.guild.id,
                allow: ['VIEW_CHANNEL'],
            }]
        });
        message.channel.send("Channel Created!); //Let the user know that the channel was created
    }
});
0
votes

discord.js v13 has changed some stuff.

Updated version:

bot.on("messageCreate", message => { // instead of 'message', it's now 'messageCreate'
    if (message.content === "channel") {
        message.guild.channels.create("channel name", { 
            type: "GUILD_TEXT", // syntax has changed a bit
            permissionOverwrites: [{ // same as before
                id: message.guild.id,
                allow: ["VIEW_CHANNEL"],
            }]
        });
        message.channel.send("Channel Created!);
})

Unrelated, but I would recommend using bot.guilds.cache.get(message.guildId) instead of fetching the message's guild directly. Using regular expressions is also by far superior to message.content === "channel"