[英]How do I fix “Cannot read property 'length' of undefined” and Args Input not working in my Minesweeper Command - Discord.js
I am making a Minesweeper command using discord.js.我正在使用 discord.js 制作扫雷命令。 It is using args to collect the user's inputs for the Minesweeper board.
它使用 args 来收集用户对扫雷板的输入。 (ie a3, a3f [f for flag]).
(即 a3, a3f [f 代表标志])。 However, the command runs perfect, it shows the board with letter and numbers on the top and side.
但是,该命令运行完美,它在顶部和侧面显示带有字母和数字的板。 The main issue I am having is the command will not take those response arg inputs that I put in. This is how the user will be able to play.
我遇到的主要问题是命令不会接受我输入的那些响应 arg 输入。这就是用户将能够玩的方式。
After the round ends (without any input) It throws the error Cannot read property 'length' of undefined
.回合结束后(没有任何输入)它抛出错误
Cannot read property 'length' of undefined
。 This is somewhat referred to these lines of code.这有点提到这些代码行。
const filter = msgs => {
const param = msgs.content.toLowerCase().split(" ");
return param.length === 2 && alphabet.includes(param[0]) && parseInt(param[1], 10) < 7 && !answered.includes(param.join("")) && msgs.author.id === msg.author.id;
};
param[0] = alphabet.includes[param[0]];
param[1] = parseInt(param[1], 10) - 1;
if (board[param[1]][param[0]] === "💣") {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; i++) {
if (board[i][j] === "💣") showboard[i][j] = "💣";
}
}
I believe this is what is causing the issue of my minesweeper command.我相信这就是导致我的扫雷命令问题的原因。 I am not 100% certain but I think I might be able to fix it by defining the params to
args[0]
我不是 100% 确定,但我认为我可以通过将参数定义为
args[0]
来修复它
I have the full code here for the command: https://natebot-haste.glitch.me/lusarovisa.js我在这里有命令的完整代码: https://natebot-haste.glitch.me/lusarovisa.js
I am honestly not too sure what to do from this point, I've attempted changing the param definition and since code seems perfect, I might as well ask here.老实说,从这一点开始,我不太确定该怎么做,我尝试更改参数定义,并且由于代码看起来很完美,我不妨在这里问一下。
Since it seems to be a problem with Params being either null or undefined how about doing a check like this?由于 Params 似乎是 null 或未定义的问题,那么进行这样的检查怎么样?
const filter = msgs => {
const param = msgs.content.toLowerCase().split("");
if(param && param.length === 2) {
return alphabet.includes(param[0]) && parseInt(param[1], 10) < 7 && !answered.includes(param.join("")) && msgs.author.id === msg.author.id;
}
else {
return false
}
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.