使用字符串变量访问对象。 JavaScript的
这是不和谐僵尸我试图创建代码:使用字符串变量访问对象。 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)
阵列在Javascript是零索引,这意味着数组中的第一个元素在索引0
,第二个1
,依此类推。既然你是b
数组中寻找第二个元素,你想b[1]
而不是b[2]
感谢您提醒我第二部分。但是,仍然存在一个问题:'不能读取未定义的属性'ppljoined'。那么你怎么读它? –
那么,如果没有提供密钥,你想检查'b [1] == undefined'而不是'b [1] = undefined'。另外还有一个问题,每次调用message.content.split时,都会创建一个* new *对象。您可能需要存储一个全局对象,该全局对象可为每个标记保存键。这样,每次调用bot.on时,都不会丢失“ppljoined”的值 – jlynch630
保存标记的全局对象位于'allqueues'中,它是一个包含所有对象的数组。 –
多少元素是b
数组中?在分割message.content
以确保后,尝试console.log(b)
。
此外,如果b[2]
未定义,但代码在if (b[2]=undefined){
语句中(只有一个等号),代码应该放入if语句中。尝试将其更改为if (b[2] == undefined){
或if (b[2]){
'b'中有两个项目,但现在我已将所有'b [2]'s更改为'b [1]',因为它将访问第二个元素。即使当我修正这些问题时,当我尝试读取'b [1] .pplneeded'时,就会得到'undefined',就像我在尝试读取'b [1] .ppljoined'时一样。 –
您可以显示'console.log(b)'的输出并更新您使用当前代码发布的代码吗? –
没关系。我修补了一些,现在我正在测试一个可能的解决方案:创建一个全局对象'var tags:{}'然后当我想添加它时,我只是将它设置为'tags [(tagname)] = {object stuff }'。我不确定它是否会工作。 –
请参阅[*如何创建最小,完整和可验证的示例*](https://*.com/help/mcve)。 – RobG