Javascript 搜索不一致消息以设置筛选器

Javascript 搜索不一致消息以设置筛选器,javascript,discord,discord.js,Javascript,Discord,Discord.js,我正在尝试搜索一条不一致的消息,寻找一个匹配的短语,然后使用该短语为一个过滤数组设置一个过滤器-当我只有一个短语要搜索时,它可以工作,但对多个短语不起作用,或者当没有输入短语时(如果没有短语匹配,我希望它没有过滤器)-感谢所有帮助 module.exports = { name: 'mission', description: 'Randomly selects a mission', execute(message, args) { let

我正在尝试搜索一条不一致的消息,寻找一个匹配的短语,然后使用该短语为一个过滤数组设置一个过滤器-当我只有一个短语要搜索时,它可以工作,但对多个短语不起作用,或者当没有输入短语时(如果没有短语匹配,我希望它没有过滤器)-感谢所有帮助

module.exports = { 
    name: 'mission', 
    description: 'Randomly selects a mission', 

     execute(message, args) { 

       let missionArray = [
           {
               name: "Incisive Attack",
               size: "Combat Patrol",
               type: "Eternal War",
               source: "BRB",
               page: "286"
               
           },
           {
            name: "Outriders Attack",
            size: "Combat Patrol",
            type: "Eternal War",
            source: "BRB",
            page: "287"
            
        },
       //more missions here
            ];
            if (message.content.match('Combat Patrol')) {
                let filter = {size:"Combat Patrol"};
            } else if (message.content.match('Incursion')) {
                let filter = {size:"Incursion"};
            } else if (message.content.match('Strike Force')) {
                let filter = {size:"Strike Force"}
            } else if (message.content.match('Onslaught')) {
                let filter = {size:"Onslaught"};
                    
                let filteredMission = missionArray.filter(function(item) {
                for (var key in filter) {
                    if (item[key] === undefined || item[key] != filter[key])
                    return false;
                }
                return true;
                });
            
            let mission = filteredMission[Math.floor(Math.random() * filteredMission.length)];
       

       return message.reply(`I have consulted the mission8Ball and you will play:\n**${mission.name}**\nMission Type: ${mission.type}\nBattle Size: ${mission.size}\nSource: ${mission.source}\nPage: ${mission.page}`); 
            }
}
};
我还想为源和类型设置过滤器


提前谢谢

这里有一个使用简单for循环解决问题的可能方法。 我还将随机选择移动到一个函数(ChooseSimession)中,以避免在“无内容”场景中出现重复的代码

module={};
module.exports={
名称:"使命",,
描述:“随机选择任务”,
执行(消息,参数){
让雷=[{
名称:“精辟攻击”,
大小:“战斗巡逻”,
类型:“永恒的战争”,
资料来源:“BRB”,
第页:“286”
},
{
名称:“领先者攻击”,
大小:“战斗巡逻”,
类型:“永恒的战争”,
资料来源:“BRB”,
第页:“287”
},
{
名称:“入侵攻击”,
大小:“入侵”,
类型:“永恒的战争”,
资料来源:“BRB”,
第页:“287”
}
];
让过滤器=[
"战斗巡逻",,
“入侵”,
"打击部队",,
“猛攻”
];
让我们选择任务=功能(任务){
让mission=missions[Math.floor(Math.random()*missions.length)];
返回消息.reply(`我已经查阅了任务8ball,您将播放:\n**${mission.name}**\n任务类型:${mission.Type}\n任务大小:${mission.Size}\n源代码:${mission.source}\n页面:${mission.page});
}

对于(让f=0;f好吧,我基本上重写了您的命令,有点过火了,但我相信这将使您更容易管理和添加新任务

解释将出现在代码中的注释中。如果您有注释,我将尝试回答。需要注意的主要问题是,如果值位于
option\u values
中创建的可能选项中,则筛选器仅汇编要筛选的数据。如果有人想按不存在的功能进行筛选,则不会将其添加到筛选器中,因此按进行筛选de>size=“aaaaaaaaaaa”
与根本不按大小过滤相同

let-ray=[
{
名称:“精辟攻击”,
大小:“战斗巡逻”,
类型:“永恒的战争”,
资料来源:“BRB”,
第页:“286”
},
{
名称:“领先者攻击”,
大小:“战斗巡逻”,
类型:“永恒的战争”,
资料来源:“BRB”,
第页:“287”
},
{//对不起,我自己做了测试
名称:“测试”,
大小:“入侵”,
类型:“短期战争”,
资料来源:“BRB”,
第页:“300”
},
{
名称:“Test2”,
大小:“入侵”,
类型:“其他类型”,
资料来源:“BRB”,
第页:“301”
}
];
让选项=[//我们的用户可以在消息中输入什么来设置他们的过滤器
大小:,“类型:,”源:
];
//我本可以把这两本词典拼凑成一本词典,但我觉得这样更容易阅读和理解
让option_values={//他们可以为我们的过滤器设置的所有可能值(全部小写!!!)
尺寸:[
“战斗巡逻”、“入侵”、“打击部队”、“猛攻”
],
资料来源:[
“brb”
],
类型:[
“永恒战争”,“其他类型”
],//继续。。。
};
//输入过滤任务数组,随机抽取一个
让我们选择Semission=函数(任务){//由用户rafaelcastrocouto提供
让mission=missions[Math.floor(Math.random()*missions.length)];
返回任务;
}
const regex=new RegExp(“[^”]+“|[\\S]+”,“g”);//我们需要分析可能包含多个单词的筛选器的参数,如“永恒战争”。您不需要完全理解这一点。我从这里得到了它https://stackoverflow.com/questions/50671649/capture-strings-in-quotes-as-single-command-argument
module.exports={
名称:"使命",,
描述:“随机选择任务”,
执行(消息,参数){
//这段代码分析了我们的参数,允许使用多个单词,如“永恒的战争”。
args=[];
message.content.match(regex.forEach)(arg=>{
如果(!arg)返回;
参数推送(参数替换(/“/g”);
});
让filter={}//创建一个信息字典,我们稍后将根据它进行筛选
对于(设i=0;i{
//set: these inputs should come back with missions
input:
!mission type: "other type" size: "incursion"

output:
@Allister, I have consulted the mission8Ball and you will play:
Test2
Mission Type: other type
Battle Size: Incursion
Source: BRB
Page: 301

input:
!mission type size: incursion type: "other type"

output:
@Allister, I have consulted the mission8Ball and you will play:
Test2
Mission Type: other type
Battle Size: Incursion
Source: BRB
Page: 301

//set: this input should only filter by eternal war due to malformed parameters
input:
!mission type size: wrong thing type: "eternal war"

output:
@Allister, Unknown parameter for size: wrong
@Allister, I have consulted the mission8Ball and you will play:
Incisive Attack
Mission Type: Eternal War
Battle Size: Combat Patrol
Source: BRB
Page: 286


//set: this input provides a correct parameter but there are no missions that match it, so it returns random
input:
!mission size: "Strike Force"

output:
@Allister, Can't find a mission matching your parameters.
@Allister, I have consulted the mission8Ball and you will play:
Incisive Attack
Mission Type: Eternal War
Battle Size: Combat Patrol
Source: BRB
Page: 286

and of course, !mission alone will just return a random mission as well.