這是不和諧殭屍我試圖創建代碼:使用字符串變量訪問對象。 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)
請參閱[*如何創建最小,完整和可驗證的示例*](https://stackoverflow.com/help/mcve)。 – RobG