[英]How to check if a string is included in an array? discord.js
Basically I am trying to make a discord bot to make trivia questions by showing an embed and awaiting the answer, I have put the answers in an array in a separate file, the possibleAnswersF
is the array that includes the answers that give you 5 points, and the possibleAnswersT
is the array that includes the answers that give you 10, and possibleAnswers is the array that includes all of the answers, I tried to use possibleAnswersF.includes(collected)
but it wouldn't work, I tried to make collected an array and use includes()
the other way but it jumped to catch()
instead.基本上,我正在尝试制作一个 discord 机器人来通过显示嵌入并等待答案来制作琐事问题,我已将答案放在单独文件中的数组中,
possibleAnswersF
是包含答案的数组,给你 5 分,并且possibleAnswersT
是包含给你 10 的答案的数组,而 possibleAnswers 是包含所有答案的数组,我尝试使用possibleAnswersF.includes(collected)
但它不起作用,我试图收集一个数组并以另一种方式使用includes()
,但它跳转到了catch()
。
const { MessageEmbed } = require('discord.js');
//const talkedRecently = new Set();
const Characters = require('../../triviacharacterlist');
const points = require('../../points.json');
const fs = require('fs');
module.exports = {
commands : ['trivia', 't'],
minArgs : 0,
maxArgs : 0,
callback : (msg, arguments, text) => {
function TriviaGuess(possibleAnswers, possibleAnswersF, possibleAnswersT, CharacterImgDir) {
const guessCharacterString = "Guess character bo3";
let pointAmt1 = 5;
let pointAmt2 = 10;
const triviaEmbed = new MessageEmbed()
.setColor('#008fff')
.setTitle(guessCharacterString)
.setImage(CharacterImgDir)
msg.channel.send(triviaEmbed)
const filter = m => possibleAnswers.some(answer => m.content.toLowerCase().includes(answer));
msg.channel.awaitMessages(filter, { max: 1, time: 15000, errors: ['time'] })
.then(collected => {
const fivePoint = `**Congrats**, ${collected.first().author} you got **5** points for guessing the character only!`;
const tenPoint = `**Congrats**, ${collected.first().author} you got **10** points for also guessing the series/game!`;
if (possibleAnswersF.includes(collected)) {
msg.channel.send(fivePoint);
points[msg.author.id] = {
points: points[msg.author.id].points + pointAmt1
};
fs.writeFile('./points.json', JSON.stringify(points), (err) => {
if (err) {
console.log(err)
}
});
}
if (possibleAnswersT.includes(collected)) {
msg.channel.send(tenPoint);
points[msg.author.id] = {
points: points[msg.author.id].points + pointAmt2
};
fs.writeFile('./points.json', JSON.stringify(points), (err) => {
if (err) {
console.log(err)
}
});
}
})
.catch(collected => msg.channel.send('too bad u took too long ya 3am'));
}
var characterRandom = 0;//Math.floor(Math.random() * 0);
if (characterRandom == 0) {
TriviaGuess(Characters.CreeperVariants, Characters.CreeperVariantsF, Characters.CreeperVariantsT, Characters.CharacterImgs.creeperimg);
}
}
}
The other file that has the arrays:具有 arrays 的另一个文件:
const CreeperVariants = [
'creeper',
'creeper minecraft',
'creeper mc'
]
const CreeperVariantsF = [
'creeper'
]
const CreeperVariantsT = [
'creeper minecraft',
'creeper mc'
]
collected
is a Collection extending Map, not a string. collected
的是扩展 Map 的集合,而不是字符串。 You would need to check collected.first().content
.您需要检查
collected.first().content
。 Use Array#some()
with String#includes
to check if any of the array elements are found in the string.使用
Array#some()
和String#includes
来检查是否在字符串中找到任何数组元素。
if (possibleAnswersF.some(ans => collected.first().content.includes(ans))) {
// Your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.