admin管理员组

文章数量:1197775

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.

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.

Share Improve this question asked Apr 20, 2017 at 8:24 Jim KneeJim Knee 1532 gold badges2 silver badges9 bronze badges 9
  • 1 Does bot contain any methods? Try console.log(bot) and check output. Maybe there is something wrong with Client class. – Oen44 Commented Apr 20, 2017 at 8:28
  • The bot is a client, and you try to create channel from the client ? Maybe try create channel from the server constant – Cyril Beeckman Commented Apr 20, 2017 at 8:29
  • @CyrilBeeckman Why not? Client can create channels, did you even check docs before commenting? – Oen44 Commented Apr 20, 2017 at 8:30
  • @Oen44 I don't know how it works but if client can make chan, it should be allowed to create channel no ? Same like TeamSpeak or others – Cyril Beeckman Commented Apr 20, 2017 at 8:31
  • Have you logged in as the bot? bot.login('[email protected]', 'password', some_function); – turnip Commented Apr 20, 2017 at 8:31
 |  Show 4 more comments

4 Answers 4

Reset to default 12

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.

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

@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

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.

本文标签: javascriptDiscord make channel using botStack Overflow