搜索的对象阵列,以匹配输入

问题描述 投票:0回答:1

所以,我试图让一些工作,我打砖墙,我一定只是由于经验不足我。

在下面的代码,我想采取什么样的用户将在(!激活[卡名])的集换卡游戏(爆丸),并有系统从全部小写,没有空格的格式找到卡。我有一个具有设置为对象的卡单独cardlist.js文件。我试过很多不同的方式,我不断收到“____是不是一个函数”或只是不能由于某种原因cardlist文件中找到的项目。我知道,我一直在整个的尝试小时跳舞周围。

const Discord = require("discord.js");
const botconfig = require("../botconfig.json");
const cardlist = require('../cardlist.js');

module.exports.run = async (bot, message, args) => {
    //console.log("works");
    //let aUser = `${message.author}`;
    let aCard = message.content.slice(10);
    oCard = Object.filter(function(cardlist){ 
        return cardlist.name === aCard });
    if (aCard === oCard) {
        console.log('Cards match!');
     } else {
         console.log(`Cannot find ${aCard}`)
     }
    }

module.exports.help = {
    name: "activate"
}

这是使用一个命令处理程序。我有很多与它正常工作等命令。该代码工作,直到我试图让ACARD和oCard相匹配。我也曾尝试寻找匹配什么用户为卡名称提出在入门卡片列表。下面是我的cardlist.js布局

const cardlist = {
    pyrushyperdragonoid: {
        image: 'https://bakugan.wiki/wiki/images/thumb/3/3b/Hyper_Dragonoid_%28Pyrus_Card%29_265_RA_BB.png/250px-Hyper_Dragonoid_%28Pyrus_Card%29_265_RA_BB.png',
        name: 'Pyrus Hyper Dragonoid',
        faction: 'Pyrus',
        energy: 1,
        BPower: '400',
        Type: 'Evo',
        Damage: 6,
        Effect: ':redfist: : +300 :Bicon: and +3:attackicon:'
    },
    dragosfury: {
        name: 'Drago\'s Fury',
        Energy: 2,
        Type: 'Action',
        Effect: '+4:attackicon:. Fury: If you have no cards in hand, +:doublestrike:'
    }
}

因此,例如:1)用户将在命令“!激活pyrushyperdragonoid” 2)我想机器人自动剪掉了“激活!”上的输入。 (无差错完成)3)机器人应该然后采取进入和搜查的cardlist.js它和检索卡上市的所有其他部分。 4)我没有这个代码做这些事,但我要使用RichEmbed显示,一旦检索到的所有信息。

我希望这一切是有道理的!谢谢你的任何和提前的所有帮助。

javascript node.js discord.js
1个回答
0
投票

Object.filter不存在,即使它:什么是Object

既然你有一个命令处理程序,你在这里一段代码可以接收来自它只有“论据”,即不是整个消息,但仅会发生什么砰命令(这里!activate)后,这将避免勉强可以理解.slice(10)。无论如何。

你不想匹配的人类可读的名称显然("Pyrus Hyper Dragonoid"),你要匹配的密钥名称(pyrushyperdragonoid,最好不要阅读障碍)。你可以解决它像任何对象,用括号内为关键:

// aCard sounds like an object of the same nature as oCard, but it's just a string
var userInput = message.content.slice(10);
var oCard = cardlist[userInput];
// then you can test
if (oCard) {
  console.log(`You legitimately chose to activate ${oCard.name}!`);
  // here oCard is a sub-object of cardlist, such as { image: '...', name: '...', faction... }
} else {
  console.log(`No mighty beast answered to the name ${userInput}!`);
  // here oCard is undefined
}
© www.soinside.com 2019 - 2024. All rights reserved.