简体   繁体   中英

Alternative solution to comparing a string with file data before node-fetch

I'm making a discord bot with a pokemon lookup command that calls poke-api to find information regarding the Pokemon. I have no specific errors being returned, I'm just looking for guidance on how to reformat a certain section, since I'm new to coding. The main problem I'm having is filtering pokemon names.

 if(!args[0]) return message.reply("Enter a Pokemon name or National dex number.");
 if(isNaN(args[0])){
       let pokemon = args[0];
       pokemon.toLowerCase();
       fs.readFile('pokemonlowercase.txt', function read(err, data){
            if (err) {
                return console.log(err);
            }
            if(!data.includes(`${pokemon}`)) return message.reply(`${pokemon} is not a valid Pokemon name.`); //Does not filter out gibberish that isnt pokemon names.
            fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`) //Does this operate before my if statement?
            .then(res => res.json())
            .then(data => { //lots of defining constants from found api data, and implementing them into a discord embed.

I am trying to look through the file pokemonlowercase.txt to match a listed Pokemon name to the input pokemon (or args[0] ). The command works properly if executed with a proper Pokemon name, but returns errors from the api if the Pokemon name is a bunch of gibberish. Essentially, the bot ignores the comparison between pokemon and all 893 pokemon names within pokemonlowercase.txt . I'm using node-fetch to grab the api's information.

Do I need to delay the.fetch with async or is the argument .data.includes an incorrect usage scenario? I understand that.fetch is a promise, but there was no mention of it going first.

For more context, the full command code is here: https://sourceb.in/Z0qKSGNPw7

You could use a library called "axios" which is a promised based library. I managed to get it working without using a pokemon.txt file.

const axios = require("axios");

// pokemon command
if (command === "pokemon") {
  if (!args.length >= 1)
    return message.channel.send("Enter a Pokemon name or National dex number.");

  // request to API
  axios
    .get(`https://pokeapi.co/api/v2/pokemon/${args[0].toLowerCase()}`)
    .then((data) => data.data)
    .then((data) => {
      // pokemon exists, do something with your data
      console.log(data);
    })
    .catch((err) => {
      if (err.response.status === 404) {
        // Pokemon doesn't exist
        message.channel.send("That's not a pokemon chief");
      } else {
        // Error
        console.error(err);
      }
    });
}

PokeAPI returns a status of 404 if the pokemon doesn't exist, meaning once you request you can check if the status is of type 404, if it is then the pokemon doesn't exist if it isn't then the pokemon exists (or a error occured)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM