は、これは私が作成しようとしている不和のためのボットのためのコードです:文字列である変数を持つオブジェクトへのアクセス。 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]
を使ってみましたが、それは無駄でした。助けてください。それはいくつかの光を当てる可能性がある場合、私はボットがある不和サーバに!j tagyoureit
その後、!q game 5 tagyoureit
を入力すると、ここでエラーテキストは次のとおりです。JavaScriptで
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