Discord使用bot制作频道

时间:2017-04-20 08:24:28

标签: javascript discord

我正在制作一个不和谐的机器人,我正在尝试使用文档中显示的here的createChannel函数。出于某种原因,我收到以下错误:

TypeError:bot.createChannel不是函数。

我的代码位于我传递消息的函数中,我已经能够创建角色并将用户添加到同一函数中的角色。这只是createChannel函数不起作用。以下是代码的相关部分。

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);
}

我还尝试过bot.addChannel和bot.ChannelCreate,因为ChannelCreate.js是包含此命令代码的文件的名称。此外,我也尝试指定通道类型并分配回调函数,但主要问题是TypeError说这根本不是一个函数。知道我做错了吗?

此外,我计划在将来的某个时候使用ServerChannel.update(),因此,在解决上一个问题后,任何有关使其工作的建议都将非常感激。

4 个答案:

答案 0 :(得分:5)

好的,经过几天的尝试和浏览文档后,我发现了解决方案。我使用的是更新版本的Discord而不是我正在阅读的文档。在较新的版本中,通道是使用服务器中的方法创建的,而不是客户端方法。所以,代码应该是:

const bot = new Discord.Client();

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

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

“text”值是您正在制作的频道类型。可以是文字或语音。

我会为遇到此问题的其他人here发布最新文档的链接。

答案 1 :(得分:5)

答案应将文档链接更新到GuildChannelManager,该链接现在负责创建新频道。

(来自文档的示例)

// 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

答案 2 :(得分:1)

@Jim Knee,我想您的答案是v11,我是discord.js的新用户,使用Visual Studio Code的自动代码功能。您可以做所有相同的事情,只是您的事情必须是这样。如果您是穷人,在执行@Jim Knee的答案时会出错,这就是您的地方!

摆脱server.createChannel(name, "text/voice"); 并将其发送到server.channels.create(name, "text/voice");

希望我至少可以提供帮助;)

我也是这里的新人

答案 3 :(得分:0)

我认为您还没有使用机器人登录。

来自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}`);
}

或者,您也可以使用令牌登录。请参阅示例文档。

相关问题