Discord make channel using bot

百般思念 提交于 2019-12-08 02:43:46

问题


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.


回答1:


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.




回答2:


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('mybot@example.com', '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.



来源:https://stackoverflow.com/questions/43514065/discord-make-channel-using-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!