如何检查字符串是否包含在数组中? discord.js
How to check if a string is included in an array? discord.js
基本上我试图通过显示嵌入并等待答案来制作一个不和谐的机器人来提出琐事问题,我将答案放在一个数组中的一个单独的文件中,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);
}
}
}
另一个包含数组的文件:
const CreeperVariants = [
'creeper',
'creeper minecraft',
'creeper mc'
]
const CreeperVariantsF = [
'creeper'
]
const CreeperVariantsT = [
'creeper minecraft',
'creeper mc'
]
collected
是一个 Collection extending Map,而不是一个字符串。您需要检查 collected.first().content
。使用 Array#some()
和 String#includes
检查是否在字符串中找到任何数组元素。
if (possibleAnswersF.some(ans => collected.first().content.includes(ans))) {
// Your code
}
基本上我试图通过显示嵌入并等待答案来制作一个不和谐的机器人来提出琐事问题,我将答案放在一个数组中的一个单独的文件中,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);
}
}
}
另一个包含数组的文件:
const CreeperVariants = [
'creeper',
'creeper minecraft',
'creeper mc'
]
const CreeperVariantsF = [
'creeper'
]
const CreeperVariantsT = [
'creeper minecraft',
'creeper mc'
]
collected
是一个 Collection extending Map,而不是一个字符串。您需要检查 collected.first().content
。使用 Array#some()
和 String#includes
检查是否在字符串中找到任何数组元素。
if (possibleAnswersF.some(ans => collected.first().content.includes(ans))) {
// Your code
}