Jim Knee
Jim Knee

Reputation: 153

Discord make channel using bot

I'm making a discord bot, and I'm trying to make use of the createChannel function shown here in the documentation. For some reason, I am getting the following error:

TypeError: bot.createChannel is not a function.

My code is within a function which I pass a message to, and I have been able to create roles and add users to roles within the same function. It's just the createChannel function that's not working. Below is the relevant portions of the code.

const bot = new Discord.Client();

function makeChannel(message){
    var server = message.guild;
    var name = message.author.username;

    server.createRole(data);
    var newrole = server.roles.find("name", name);
    message.author.addrole(newrole);

    /* The above 3 lines all work perfectly */


    bot.createChannel(server,name);
}

I have also tried bot.addChannel, and bot.ChannelCreate, since ChannelCreate.js is the name of the file which contains the code for this command. Also, I have attempted specifying channel type and assigning a callback function as well, but the main issue is the TypeError saying that this isn't a function at all. Any idea what I'm doing wrong?

Additionally, I plan to use ServerChannel.update() at some point in the future, so any advice on getting that to work once the previous problem is resolved would be greatly appreciated.

Upvotes: 2

Views: 62918

Answers (4)

ThisIsACoolBoy
ThisIsACoolBoy

Reputation: 21

@Jim Knee's I think your answer is v11, I'm new in discord.js, using Visual Studio Code's auto-code thingy. You can do all the same things except your thing must be this. If you are poor people, getting errors on doing @Jim Knee's answer, this is the place for "YOU!"

Get rid of server.createChannel(name, "text/voice"); And get it to THIS server.channels.create(name, "text/voice");

Hope I can help at least ;)

I'm just a new guy here too

Upvotes: 2

dziobaczy
dziobaczy

Reputation: 931

The answer should update documentation link to the GuildChannelManager which is now responsible for creating new channel.

(Example from docs)

// Create a new text channel
guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
  .then(console.log)
  .catch(console.error);

https://discord.js.org/#/docs/main/stable/class/GuildChannelManager

Upvotes: 8

Jim Knee
Jim Knee

Reputation: 153

Alright, after a few days of trying things and going through the docs, I have discovered the solution. I am using a more recent version of Discord than the docs I was reading were written for. In the newer version, channels are created with a method in the server, not a client method. so, the code should be:

const bot = new Discord.Client();

function makeChannel(message){
    var server = message.guild;
    var name = message.author.username;

    server.createChannel(name, "text");
}

The "text" value is the type of channel you are making. Can be text or voice.

I'll post a link to the most recent documentation for anyone else who encounters this problem here.

Upvotes: 12

turnip
turnip

Reputation: 2346

I think you have not logged in with your bot.

From the docs:

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

client.login('[email protected]', 'password', output); // you seem to be missing this

function output(error, token) {
        if (error) {
                console.log(`There was an error logging in: ${error}`);
                return;
        } else
                console.log(`Logged in. Token: ${token}`);
}

Alternatively, you can also login with a token instead. See the docs for the example.

Upvotes: -2

Related Questions