使用作为字符串的变量访问对象。使用Javascript

时间:2017-08-16 02:03:08

标签: javascript bots discord discord.js

这是我想要创建的不和谐机器人的代码:

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时,会出现错误文本:

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)

3 个答案:

答案 0 :(得分:0)

Javascript中的数组是零索引的,这意味着数组中的第一个元素位于索引0,第二个1,依此类推。由于您正在寻找b数组中的第二个元素,因此您需要b[1]而不是b[2]

答案 1 :(得分:0)

b数组中有多少个元素?在拆分console.log(b)以确保后,请尝试message.content

此外,如果b[2]未定义,但代码应该已进入if语句,但if (b[2]=undefined){语句中只有一个等号(只有一个等号)。尝试将其更改为if (b[2] == undefined){if (b[2]){

答案 2 :(得分:0)

<?php require_once 'jpgraph/jpgraph.php'; require_once 'jpgraph/jpgraph_line.php'; require_once 'jpgraph/jpgraph_date.php'; $line1D = array(1497844800,1498449600,1505102400,1516597200); $line1Y = array(79.00,76.00,53.00,14.00); $line2D = array(1504584000,1507521600); $line2Y = array(9.87,9.93); $graph = new Graph(640,480); $graph->clearTheme(); $graph->SetScale('datlin'); $graph->xaxis->SetLabelAngle(60); $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8); $graph->xaxis->SetLabelFormatString('m/d/Y',true); $graph->yaxis->scale->SetGrace(5); $line1=new LinePlot($line1Y,$line1D); $line1->SetColor('#4A6EC6'); $line1->SetWeight(2); $line1->mark->SetFillColor('#4A6EC6'); $line1->mark->SetType(MARK_FILLEDCIRCLE); $graph->Add($line1); $line2=new LinePlot($line2Y,$line2D); $line2->SetColor('#4A6EC6'); $line2->SetWeight(2); $line2->mark->SetType(MARK_CIRCLE); $line2->mark->SetColor('#4A6EC6'); $graph->Add($line2); $graph->Stroke(); // display the graph ?> 是一项作业,而不是表达。将其更改为b[2] = undefined(或b[2] === undefined