我制作了一个争夺游戏命令,但由于某种原因,大写字母会使机器人崩溃



我做了一个小命令,这样,如果有人,例如,输入&scramble apple,bot就会发送"这个词是:paple;。它按预期工作,但当前的问题是;苹果;而不是";苹果";,机器人崩溃了,说它无法获取未定义的作者

module.exports = {
name: 'scramble',
execute(message, args) {
const Discord = require('discord.js')
let givenword = args.slice(0).join(" ");

function scramble(givenword) {
var word = givenword.split("")
n = word.length
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i+1));
var tmp = word[i];
word[i] = word[j]
word[j] = tmp;
}
return word.join("")
}
scrambledword = scramble(givenword)
const embed = new Discord.MessageEmbed()
.setTitle('Scramble time!')
.setColor("RANDOM")
.setDescription("The word is: " + scrambledword)
.setFooter('You have 30 seconds to try and guess it, the first person to answer correctly ')
message.channel.send({embed});
message.delete()

const filter = m => m.content.includes(givenword);
const collector = message.channel.createMessageCollector(filter, { time: 30000 });

collector.on('collect', m => {
console.log(`Collected ${m.content}`);
});

collector.on('end', collected => {
message.channel.send(`${collected.first().author} got the correct answer first! The answer was ${givenword}`);
console.log(`Collected ${collected.size} items`);
});


}
}

有人能帮我一下吗?

有两个错误:

  1. 您需要处理案例不匹配。我建议,在对单词进行加扰之前,将其小写,并在评估它们之前将响应小写
var word= givenword.toLowercase().split("")
const filter = m => m.content.toLowercase().includes(give word)
  1. 您需要处理没有做出正确猜测的情况。collected.first()未定义,因为没有赢家。我建议在打印获胜者之前先检查一下获胜者
if (collected.length <= 0) {
// send no winner message
} else {
// send the winner message
}

相关内容

最新更新