2017-08-16 68 views
0

這是不和諧殭屍我試圖創建代碼:使用字符串變量訪問對象。 JavaScript的

const Discord = require('discord.js'); 
const config = require("./config.json"); 
const bot = new Discord.Client(); 

var prefix = '!'; 
var allqueues = []; 

function queue(game,pplneeded,tag){ 
    this.game = game; 
    this.pplneeded = pplneeded; 
    this.ppljoined = 1;  
    this.tag = tag; 
    allqueues.push(tag); 
} 
bot.on('message',(message) => { 
    if(message.content.startsWith(prefix+'prefix')){ 
     var member = message.mentions.members.first(); 

     prefix=(message.content).split(" ")[1]; 
     message.channel.send('The command prefix has been set to '+(message.content).split(/\s+/g)[1] + ' .'); 
} 
if(message.content == (prefix+'rules')){ 
    message.channel.send('I\'ll keep these rules short and sweet, \'cuz this is just a casual gaming server. \n No spamming. Alright, that\'s it.'); 
} 
if(message.content == (prefix+'help')){ 
    message.channel.send('Simply type **' + prefix + 'q** ***game people_needed tag*** to create a queue, and **' + prefix + 'j** ***tag*** to join a queue.'); 
    message.channel.send('Type **' + prefix + 'eq** ***tag*** to exit a queue, and **' + prefix + 'd** ***tag*** to dismantle a queue.'); 
    message.channel.send('Type **' + prefix + 'inf** ***tag*** to find info about a queue.'); 
} 
if(message.content.startsWith(prefix+'q')){ 
    var a = message.content.split(/\s+/g); 
    a[3] = new queue(a[1],a[2],a[3]); 
    message.channel.send(a[1]+' '+a[2]+' '+a[3].tag); 
    message.channel.send('A new queue was made! Type **' + prefix + 'j ' + a[3].tag + '** to join this '+a[1]+' queue!') 
    message.channel.send(allqueues); 
} 
if(message.content.startsWith(prefix+'j')){ 
    var b = message.content.split(/\s+/g); 
    if (b[2]=undefined){ 
     message.channel.send('That\'s not a valid tag!') 
     return; 
    }else{ 
     b[2].ppljoined=b[2].ppljoined+1; 
     message.channel.send(message.author + ' has joined the ' + b[2] + 'queue!~~'); 
     if (b[2].ppljoined==b[2].pplneeded){ 
      message.channel.send(b[2] + ' has all the needed people!'); 
      index = allqueues.indexOf(b[2]) 
      allqueues.splice(index, 1); 
     } 
    } 

} 
}); 

bot.login(config.token); 

的問題是,我不知道如何訪問的對象之一與「B」陣列。正如你所看到的,我嘗試使用b[3],但那是徒勞的。請幫忙。如果它可以提供一些線索,這裏是錯誤的文字,當我鍵入!q game 5 tagyoureit然後!j tagyoureit在一個不和諧的服務器,其中BOT是:

C:\Users\----\Desktop\gamebot\index.js:42 
     b[2].ppljoined=b[2].ppljoined+1; 
         ^

TypeError: Cannot read property 'ppljoined' of undefined 
    at Client.bot.on (C:\Users\----\Desktop\gamebot\index.js:42:32) 
    at emitOne (events.js:115:13) 
    at Client.emit (events.js:210:7) 
    at MessageCreateHandler.handle (C:\Users\----\Desktop\gamebot\node_modules\discord.js\src\client\websocket\packets\handlers\M 
essageCre 
ate.js:9:34) 
at WebSocketPacketManager.handle (C:\Users\----\Desktop\gamebot\node_modules\discord.js\src\client\websocket\packets\WebSocketP 
acketMa 
nager.js:102:65) 
at WebSocketConnection.onPacket (C:\Users\----\Desktop\gamebot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:325 
:35) 
    at WebSocketConnection.onMessage (C:\Users\--\Desktop\gamebot\node_modules\discord.js\src\client\websocket\WebSocketConnectio 
n.js:28 
8:17) 
    at WebSocket.onMessage (C:\Users\---- 
\Desktop\gamebot\node_modules\ws\lib\EventTarget.js:103:16) 
    at emitTwo (events.js:125:13) 
    at WebSocket.emit (events.js:213:7) 
+0

請參閱[*如何創建最小,完整和可驗證的示例*](https://stackoverflow.com/help/mcve)。 – RobG

回答

0

陣列在Javascript是零索引,這意味着數組中的第一個元素在索引0,第二個1,依此類推。既然你是b數組中尋找第二個元素,你想b[1]而不是b[2]

+0

感謝您提醒我第二部分。但是,仍然存在一個問題:'不能讀取未定義的屬性'ppljoined'。那麼你怎麼讀它? –

+0

那麼,如果沒有提供密鑰,你想檢查'b [1] == undefined'而不是'b [1] = undefined'。另外還有一個問題,每次調用message.content.split時,都會創建一個* new *對象。您可能需要存儲一個全局對象,該全局對象可爲每個標記保存鍵。這樣,每次調用bot.on時,都不會丟失「ppljoined」的值 – jlynch630

+0

保存標記的全局對象位於'allqueues'中,它是一個包含所有對象的數組。 –

0

多少元素是b數組中?在分割message.content以確保後,嘗試console.log(b)

此外,如果b[2]未定義,但代碼在if (b[2]=undefined){語句中(只有一個等號),代碼應該放入if語句中。嘗試將其更改爲if (b[2] == undefined){if (b[2]){

+0

'b'中有兩個項目,但現在我已將所有'b [2]'s更改爲'b [1]',因爲它將訪問第二個元素。即使當我修正這些問題時,當我嘗試讀取'b [1] .pplneeded'時,就會得到'undefined',就像我在嘗試讀取'b [1] .ppljoined'時一樣。 –

+0

您可以顯示'console.log(b)'的輸出並更新您使用當前代碼發佈的代碼嗎? –

+0

沒關係。我修補了一些,現在我正在測試一個可能的解決方案:創建一個全局對象'var tags:{}'然後當我想添加它時,我只是將它設置爲'tags [(tagname)] = {object stuff }'。我不確定它是否會工作。 –