2017-06-21 188 views
0

最近我一直在處理不一致的bot,這是我第一次編寫代碼,我認爲Javascript比其他選項更容易。現在,我在錯誤後通過閱讀錯誤掙扎。Javascript Discord Bot在運行時給出代碼參考錯誤

反正,讓我們來談談問題。目前,代碼如下:

const Discord = require("discord.js"); 
const client = new Discord.Client(); 
const commando = require('discord.js-commando'); 
const bot = new commando.Client(); 
const prefix="^"; 

client.on('ready',() => { 
    console.log(`Logged in as ${client.user.tag}!`); 
}); 

client.on('message', msg => { 
    let short = msg.content.toLowerCase() 

    let GeneralChannel = server.channels.find("General", "Bot") 
if (msg.content.startsWith(prefix + "suggest")) { 
    var args = msg.content.substring(8) 
    msg.guild.channels.get(GeneralChannel).send("http\n SUGGESTION:" + msg.author.username + " suggested the following: " + args + "") 
    msg.delete(); 
    msg.channel.send("Thank you for your submission!") 
    } 
}); 

,當我跑說的代碼,它返回的(我認爲)的錯誤基本上告訴我,「服務器」,在let GeneralChannel = server.channels.find("General", "Bot")是不確定的。我的問題是,我實際上不知道如何定義服務器。我假設當我定義服務器時,它也會告訴我我需要定義頻道並找到,儘管我不確定。

感謝提前:)

回答

1

首先,你爲什麼要使用letvar?無論如何,如錯誤所述,server未定義。客戶端不知道你指的是什麼服務器。這就是你的msg對象進來,它有一個屬性guild這是服務器。

msg.guild; 

其次,你想用let GeneralChannel = server.channels.find("General", "Bot")實現什麼?數組的find方法需要一個函數。你是否試圖尋找名稱爲「一般」或什麼的渠道?如果是這樣,最好以這種方式使用頻道的ID,您可以使用任何bot的服務器中的頻道(如果您嘗試將所有建議發送到不同服務器上的特定頻道)。

let generalChannel = client.channels.find(chan => { 
    return chan.id === "channel_id" 
}) 
//generalChannel will be undefined if there is no channel with the id 

如果你想送

通過這樣的假設去,你的代碼可以被重新寫到:

const Discord = require("discord.js"); 
const client = new Discord.Client(); 
const commando = require('discord.js-commando'); 
const bot = new commando.Client(); 
const prefix="^"; 

client.on('ready',() => { 
    console.log(`Logged in as ${client.user.tag}!`); 
}); 

client.on('message', msg => { 
    let short = msg.content.toLowerCase(); 

    if (msg.content.startsWith(prefix + "suggest")) { 
     let generalChannel = client.channels.find(chan => { 
      return chan.id === 'channel_id'; 
     }); 

     let args = msg.content.substring(8); 

     generalChannel.send("http\n SUGGESTION: " + msg.author.username + " suggested the following: " + args + ""); 
     msg.delete(); 
     msg.channel.send("Thank you for your submission!") 
    } 
}); 
+0

我認爲op試圖做的是將''SUGGESTION''消息發送到特定的「家庭」服務器,而不是它被觸發的那個服務器,實質上是將所有反饋發送到個人服務器。 –

+0

@DanF這是有道理的。編輯我的問題來涵蓋 – Wright

+0

我使用'let'和'var'的原因是因爲兩個不同的人在幫助我一點。一個使用'var'和一個使用'let',所以我都使用了,因爲我太懶惰了改變一個到另一個。另外,謝謝你,你做了一個了不起的工作幫助。非常感激。 –

0

不,範圍在這種情況下一個關注,但值得注意的是'let'定義了一個局部變量,而'var'定義了一個全局變量。它們是有區別的。