2017-04-20 62 views
2

我做了一個不和諧的機器人,我試圖使用在文檔中顯示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(),所以任何一旦解決了上一個問題就可以使用它的建議將不勝感激。

+1

'bot'是否包含任何方法?嘗試'console.log(bot)'並檢查輸出。 'Client'類可能有問題。 – Oen44

+0

bot是一個客戶端,你試圖從客戶端創建頻道?也許嘗試從服務器常量創建頻道 –

+0

@CyrilBeeckman爲什麼不呢?客戶可以創建渠道,你甚至在評論之前檢查過文檔嗎? – Oen44

回答

2

好吧,經過幾天的嘗試和閱讀文檔後,我發現瞭解決方案。我使用的是Discord的更新版本,而不是我正在閱讀的文檔。在新版本中,通道是使用服務器中的方法創建的,而不是客戶端方法。所以,代碼應該是:

const bot = new Discord.Client(); 

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

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

「文本」值是通道的你將做的類型。可以是文字或語音。

我會發佈一個鏈接到最新的文檔對於遇到這個問題的其他人here

0

我想你還沒有用你的機器人登錄。

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

或者,您也可以登錄與令牌來代替。請參閱示例文檔。

+0

我已使用我的機器人登錄。我只是沒有在示例中包含登錄行。正如我所說的,機器人可以爲服務器中的用戶製作和分配角色,所以它肯定是連接的。 –